CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: Throw Down The Gauntlet:
need a program that can parse show cdp neig into cvs format

 



achilder
New User

Feb 7, 2011, 10:39 AM

Post #1 of 4 (24284 views)
need a program that can parse show cdp neig into cvs format Can't Post

Can someone help me with a perl script to do the following task:

I have output from cdp neigh det from a buch of cisco routers that looks like this:

router1#sh cdp neigh det
-------------------------
Device ID: myrouter
Entry address(es):
IP address: 10.1.1.1
Platform: WS-C6509, Capabilities: Trans-Bridge Switch
Interface: GigabitEthernet0/19, Port ID (outgoing port): 4/8
Holdtime : 123 sec

Version :
WS-C6509 Software, Version McpSW: 7.1(2) NmpSW: 7.1(2)
Copyright (c) 1995-2002 by Cisco Systems


advertisement version: 2
VTP Management Domain: 'EXP'
Native VLAN: 6
Duplex: full
Management address(es):
IP address: 10.1.1.1

router1#




router2#sh cdp neigh det
-------------------------
Device ID: myrouter
Entry address(es):
IP address: 10.1.1.1
Platform: WS-C6509, Capabilities: Trans-Bridge Switch
Interface: GigabitEthernet1/0/19, Port ID (outgoing port): 4/8
Holdtime : 129 sec

Version :
WS-C6509 Software, Version McpSW: 7.1(2) NmpSW: 7.1(2)
Copyright (c) 1995-2002 by Cisco Systems


advertisement version: 2
VTP Management Domain: 'EXP'
Native VLAN: 201
Duplex: full
Management address(es):
IP address: 10.1.1.1

-------------------------
Device ID: myrouter
Entry address(es):
IP address: 10.1.1.1
Platform: WS-C6509, Capabilities: Trans-Bridge Switch
Interface: GigabitEthernet1/0/19, Port ID (outgoing port): 9/1
Holdtime : 128 sec

Version :
WS-C6509 Software, Version McpSW: 7.1(2) NmpSW: 7.1(2)
Copyright (c) 1995-2002 by Cisco Systems


advertisement version: 2
VTP Management Domain: 'EXP'
Native VLAN: 201
Duplex: full
Management address(es):
IP address: 10.1.1.1

router2#



router3#sh cdp neigh det
-------------------------
Device ID: johnsrouter
Entry address(es):
IP address: 10.2.2.2
Platform: cisco WS-C6506, Capabilities: Router Switch
Interface: GigabitEthernet1/0/20, Port ID (outgoing port): GigabitEthernet2/2
Holdtime : 121 sec

Version :
Cisco Internetwork Operating System Software
IOS (tm) c6sup2_rp Software (c6sup2_rp-PK9SV-M), Version 12.2(17d)SXB11a, RELEASE SOFTWARE (fc1)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2006 by cisco Systems, Inc.


advertisement version: 2
VTP Management Domain: ''
Native VLAN: 201
Duplex: full
Management address(es):

-------------------------
Device ID: bobsrouter
Entry address(es):
IP address: 10.3.3.3
Platform: WS-C6509, Capabilities: Trans-Bridge Switch
Interface: GigabitEthernet1/0/10, Port ID (outgoing port): 8/4
Holdtime : 130 sec

Version :
WS-C6509 Software, Version McpSW: 7.1(2) NmpSW: 7.1(2)
Copyright (c) 1995-2002 by Cisco Systems


advertisement version: 2
VTP Management Domain: 'EXP'
Native VLAN: 201
Duplex: full
Management address(es):
IP address: 10.3.3.3

router3#



This is a short sample but my file will end up with 1000's of entries like this. What I am trying to do

is this:


Parse the file line by line.
Look for Device ID:
Copy text in Device ID after the : to the newline into a new file appended with a comma
continue down the file looking for address: and copy the value after the : and before the newline into

the file wher the device id went on the same line with the associated device with an appened comma.
continue down the file and search for Platform: and copy the value between the : and newline to the file

from above after the IP address



Basically I am trying to end up with a comma delimted file. If the above data was run through my

theoretical script it would lookslike this:

myrouter, 10.1.1.1, WS-C6509, Capabilities: Trans-Bridge Switch
myrouter, 10.1.1.1, WS-C6509, Capabilities: Trans-Bridge Switch
myrouter, 10.1.1.1, WS-C6509, Capabilities: Trans-Bridge Switch
johnsrouter, 10.2.2.2, cisco WS-C6506, Capabilities: Router Switch
bobsrouter, 10.3.3.3, WS-C6509, Capabilities: Trans-Bridge Switch





Caveats:

once a device ID is found in the line by line only and ip address and platform within 15 lines should be

used for creating that record. The reason for this is I may end up getting back cdp information that is

non consistent. if this caveat is not in place it could throw off all record below the incomplete one.

sometimes there will be multiple IP address on seperate lines. These should all be placed in the same

record.


The output for IP address is inconsistent. sometimes it is IP address: and sometimes address: Need to

accomadate both

capital letters in the the search need to be irrelevant






here is an example of what a record with multiple ips would look like:

Port (Our Port): 2/5
Device-ID: testbed
Device Addresses:
IP Address: 172.16.0.3
IP Address: 172.16.0.3
Holdtime: 126 sec
Capabilities: TRANSPARENT_BRIDGE SWITCH
Version:
Cisco Internetwork Operating System Software
IOS (tm) C3500XL Software (C3500XL-C3H2S-M), Version 12.0(5)WC10, RELEASE SOFTWARE (fc1)
Copyright (c) 1986-2004 by cisco Systems, Inc.
Platform: cisco WS-C3548-XL
Port-ID (Port on Neighbors's Device): GigabitEthernet0/8
VTP Management Domain: EXP
Native VLAN: unknown
Duplex: full






This would be an example of an alternate format I need to account for:


wonky# sh cdp neigh det

CDP neigbors information

Port : 25
Device ID : wonky1
Address Type : IP Address : 192.168.1.1
Platform : HP 8000M
Capability : S
Device Port : D2
Version : Revision C.09.28 /sw/code/build/vgro(c09)

wonky#







I would creatley appreciate any help that anyone can provide with scripting his up.


BillKSmith
Veteran

Feb 10, 2011, 12:42 PM

Post #2 of 4 (24209 views)
Re: [achilder] need a program that can parse show cdp neig into cvs format [In reply to] Can't Post

I am not clear about your alternate form for IP address. Your text and example seem to disagree. Do you mean only that the key-word "IP Address" is not always at the start of the line?



The attached code appears to meet all your other requirements. Please test it.
Good Luck,
Bill
Attachments: achilder.pl (1.56 KB)


achilder
New User

Feb 10, 2011, 5:14 PM

Post #3 of 4 (24196 views)
Re: [BillKSmith] need a program that can parse show cdp neig into cvs format [In reply to] Can't Post

Hey Bill, Thanks for this. That is exactly what I was meaning, that the formating is sometimes different with IP address imbedded in the line. I have also discovered running through a few more of these that Device Id: is sometimes Device-Id: In addition there is sometimes an IP address listed under the last section Management address(es): so an ip would be caught twice in the script under these condidtions. Not sure how that would affect output.

I just tried to pull down your script and run it against a file and got the following:
bash-2.03$ chmod 700 cdpformat.pl
bash-2.03$ cdpformat.pl -i cdp discovery.txt -o cleanedcdplist
.//cdpformat.pl: use: command not found
.//cdpformat.pl: use: command not found
.//cdpformat.pl: use: command not found
.//cdpformat.pl: use: command not found
.//cdpformat.pl: use: command not found
.//cdpformat.pl: Readonly::Scalar: command not found
.//cdpformat.pl: line 7: syntax error near unexpected token `=>'
.//cdpformat.pl: line 7: `Readonly::Scalar my $FIELD => '
bash-2.03$


BillKSmith
Veteran

Feb 11, 2011, 2:06 PM

Post #4 of 4 (24124 views)
Re: [achilder] need a program that can parse show cdp neig into cvs format [In reply to] Can't Post

Sorry, I forgot the line that is needed to make a perl script executable in unix. You can run it "as is" on any operating system by typing at a command (or shell) prompt:


Code
  

perl cdpformat.pl -i discovery.txt -o cleanedcdplist.txt

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives