r/networking•
derek

Python script to print *useful* CDP neighbor information; one line per neighbor

I wanted a way to print a CDP brief on NXOS switches to list each neighbor's local interface, neighbor interface, hostname, and IP addresses (mgmt or otherwise).

Requires python on box; tested with N7710 running 8.3.2 and N950(4|8) running 7.0(3)I7(6).

Example from N9K: https://i.imgur.com/s74rgI9.png

Github: https://github.com/derek-shnosh/network-code/tree/master/python

Ninj-edit: Supports multiple neighbors per interface for situations where dumb-switches are forwarding CDP information blindly.

Posted Oct 15, 2019, 8:24 PM UTC · edited

11245
nomodz4real•

le gasp! NXOS can run python?

Man I need to test this out.

Thanks for coming up with this tool and delivering knowledge to my eyeballs XD

15

So can Nexus 3/5/7k

2†
Hatcherboy•

So can isr4k i hear. Just getting in to network automation...... pretty frickin cool if u ask me!

1
nomodz4real•

I agree!

1
ValarDoheres•

Pretty cool. This is actually the output I have always argued should be printed by show cdp neighbors commands. Give me the extra junk if I add details. Good work!

5
uluscum•

Are you running something at the ToR that can also run python? Why not use telemetry or SNMP to do this? ( ‘not a jerk objection. Cool script. I just rolled out a whole management/telemetry cloud thing and maybe having distrusted scripts would have been better...dammit.)

3
JasonDJCCNP / FCNSP / MCITP / CICE•

I think the intent is to have something accessible while you're in the thick of it actually logged into switches.

"show cdp neigh" often gives shitty results that you can't always |inc because the stuff you're looking for isn't on one line thanks to a hostname that's 1 character too long.

And then getting the real useful info is a pain in the ass because you do a show cdp neigh gi1/2/3 and get a full screen worth of useless junk when you're just looking for a management IP.

And then you're on Nexus and remember you have to type show cdp neigh int gi1/2/3 which just pisses you off more because Cisco, with their billions of dollars, can't make one fucking CLI consistent between any two given platforms. And then again you realize you're on Nexus and the interface numbering is Et1/1 because they finally got a good idea when it comes to something (interface naming) and only applied it to that one platform.

"Oh la dee dah...we're Cisco...nobody ever got fired for buying us HEH HEH. More certified engineers than any other vendor HEH HEH. You'll have to learn a totally different environment for firewalls, switches, datacenter switches, high-end routers, voice platforms, SDWAN, and wireless, and when you think you know one, we'll buy out a competitor and switch to it's syntax, but not really!"

Fucking Dell makes a better Cisco switch than Cisco.

28
redex93•

gold, nobody ever got fired for buying Cisco 😂

5
JasonDJCCNP / FCNSP / MCITP / CICE•

Let me just rip + replace my vendor-proprietary "single pane of glass" management platform for the third time this decade.

Oh, this SPOG only handles my switches and wireless. Need another one for my firewalls. Oh well, dual panes of glass. N+1, I guess...

Oh we're getting SDWAN?

Oh new firewalls? Hol' up, new SPOG.

7
suddenlyredditCCNP / CCDP, EIEIO•

This hit home right now. Jesus we're in multi-pane-of-glass, screw it, let's just expand the desk space so we can have more panes.

Hang in there man.

1
derekOPshnosh.(io|net)•

You hit the nail in the head regarding my intent with this script. 👍

2
Letmeholleratya•

Let the rage flow. Their CLI is terribly inconsistent. No idea why they dont use the NXOS style interface referencing in IOS... makes so much more sense.

2
JasonDJCCNP / FCNSP / MCITP / CICE•

About the only things consistent with Cisco is the rate at which their quality and support decreases, and the rate at which the cost and complexity of their licensing increases.

And the turnover of their sales staff. Used to be being a Cisco SE was a dream job, now they are churning them faster than an Amish prostitute churns butter.

1
OhMyInternetPoliticsModerator | Sr. Netowrk Diplomat•

Come to the Juniper side! We have a consistent CLI, a brilliant API, and... even cake!

** If by cake you mean annoying bugs

1
Titanium-Ti•

you can easily do this with sed and grep and vsh It will work on anything that is not a N5k.

2
Poulito•

‘Easily’ is a relative term. Mind putting up an example 1-liner so I can get an idea of what you consider easy?

7
Titanium-Ti•

something like this with a little more work to clean up the output, I only spent ~15 minutes on it most of which was due to there being no xml output for individual neighbor's detailed output so I did it this way instead:

edit: i'm not going to make this work with reddit formatting so here is a pastebin

https://pastebin.com/sGZuMEMN

edit2: cleaned up a little: https://pastebin.com/mZyft4Hn

3
derekOPshnosh.(io|net)•

This is actually pretty cool and taught me a few things about sed, thanks for sharing.

While your example provides a similar function and could be further tweaked to provide a similar output (shortening interface types, etc), I'd still prefer the python method (on supported platforms) as the code is more readable and malleable, in my opinion.

Also as stated in other replies, my script was the result of a self-imposed learning challenge that I thought might be worth sharing; not so much a "look at this cool thing that nothing else can do." There's almost always more than one way to accomplish a task.

1

I LOVE YOU DEREK

2
Titanium-Ti•

I am glad you gained insights from it.

I am not sure why people want this to be a competition. (if it is, the official command loses and we all win)

1
Hatcherboy•

Gotta agree with u on that one! Ios upgrade script uppost is rad...... but 9*, 6 |, 12 / is alot!

1
RussianToCollusion•

Mind putting up an example 1-liner

If you want the script to fit in one line it's going to be cryptic by default

-5
Poulito•

I got the idea from the comment that what u/Derek has done was over-complicated and u/Titanium-TI had a solution that was easier and more elegant. Did I misread that?

2
Titanium-Ti•

Neither solution is elegant. Both are over complicated, but useful. Easy will depend on the person doing it, for example I have a text file with several dozen useful commands like this that I use regularly.

2
RussianToCollusion•

You asked for a one liner. Rarely is a one-liner script going to be easy to read

-6
derekOPshnosh.(io|net)•

Sure it can be done, but that wouldn't have taught me much about python.

4
Titanium-Ti•

I do it this way all the time because I find it much more portable than python. It is also easier for me to just paste it into the cli rather than installing a script. I guess I could write a one liner to install the script so python might be just as easy but I think less devices support python than support sed.

I also agree that the best way to learn something like python is to find a fun/useful problem to solve and then solve it with python, and I find that usually what you learn is more valuable than the resulting tool you created :)

4
phsikotic•

just started using "sh cdp nei det | begin (name)" to get the IP after using sh cdp nei. I like this

2
derekOPshnosh.(io|net)•

If you're on NX-OS you can get pretty creative with filtering output, try this to find the neighbor your looking for and include the following six lines of output...

show cdp nei det | inc {name} next 6

You can even step through filters, this will find all lines with {name} and then further filter down those lines to lines that contain {mdf}.

show cdp nei | inc {name} ignore-case | inc mdf ignore-case

Then there's grep, sed, etc. that you can get super creative with. I used this in a recent project to find access switches that didn't have the proper code on them.

cli alias name aswneedsupgrade sh cdp nei det | i 16.3.[^8] prev 10 | i Device|Software | sed 's/Device ID:\([^.]*\).*/\1/g; s/^.*\(Version [0-9.]*\).*/ >> \1/g' | sed 'N;s/\(ASW.*\)\n/\1/' | sort

Typing aswneedsupgrade prints out a list of switches that aren't on 16.3.8 code, what version they're actually running, and what interface they are connected to.

4
phsikotic•

Wow that 'next' filter... have been saying there should be something like that. Thank you

2
wilhouse•

Has anyone tried this on an IOS XE device? I just wish we would get the ability to | jsonpretty on all devices because as an automation engineer that would just make me life 100% easier.

1
derekOPshnosh.(io|net)•

I don't currently have an IOS-XE device to test with, nor eveNG/VIRL to lab with. Next time I have either, and time to geek out, I'll try to adapt the script for that platform.

1
wilhouse•

Yeah I've been trying to convert your script over to something using netmiko but not having any luck.

1
RussianToCollusion•

Why did you reinvent the wheel and write this?

Sorry but rewriting a script that's already widely available on github seems like a waste of time.

-11
derekOPshnosh.(io|net)•

Admittedly didn't do a quick search, this was a python learning experience for me... not so much "can this be done", but "can I make this happen" with a goal of understanding the how/why.

That being said, I'll check for the widely avail script when I'm not on mobile... if you have a link so I could compare and learn, I'd greatly appreciate it.

9
ghost_of_napoleonI like to move bits ¯\_(ツ)_/¯•

not so much "can this be done", but "can I make this happen" with a goal of understanding the how/why.

I completely understand this sentiment and do the same myself.

Thanks for sharing this too. Nice to learn from others also learning.

4
derekOPshnosh.(io|net)•

u/RussianToCollusion I was unable to find anything (with a cursory search) that hits all the intent points that u/JasonDJ made here, mainly;

...the intent is to have something accessible while you're in the thick of it actually logged into switches...

Would you mind providing a link?

3
[deleted]•

[deleted]

1