Cisco Support Community
cancel
Showing results for 
Search instead for 
Did you mean: 

Automatically Set Port Descriptions

In preparing for CiscoLive! in San Diego, I am provisioning our access layer 3560-E switches.  Since things have a tendency to change a lot at an event like CiscoLive! I thought it would be best to make sure our port descriptions are always up-to-date when it comes to reflecting what devices are connected.  To help me do that, I wrote up this small EEM applet policy.  It will update the port's description based on the CDP neighbor learned on that port.  This policy requires EEM 3.2, so you're looking at 12.2(55)SE or higher for the 3560s.  It will also work on 3750s and ISR G2 routers running 15.x code.

event manager applet update-port-description

event neighbor-discovery interface regexp GigabitEthernet.* cdp add

action 1.0 cli command "enable"

action 2.0 cli command "config t"

action 3.0 cli command "interface $_nd_local_intf_name"

action 4.0 cli command "description $_nd_cdp_entry_name:$_nd_port_id"

The result of this will be a description like the following on switch ports:

description SDCC_IDF_1.11:TenGigabitEthernet0/1
Version history
Revision #:
1 of 1
Last update:
‎05-19-2012 11:30 PM
Updated by:
 
Labels (1)
Everyone's tags (4)
Comments
New Member

A very pretty script!!

Will this script change the port´s description every 180 seconds?

Cisco Employee

No, it changes the port description when a new CDP neighbor connects to it.

New Member

I like it!

Have made an improved version, please comment and/or improve further:

  • Trim domain name
  • Short interface name (Te/Gi/Fa)
  • Write config changes, and post a syslog message

regexp in the event are for the uplink module on 3750-X/3560-X, adapt to your own need.

event manager applet auto-update-port-description authorization bypass

description "Auto-update port-description based on CDP neighbors info"

action 0.0 comment "Event line regexp: Deside which interface to auto-update description on"

event neighbor-discovery interface regexp .*GigabitEthernet[1-9]/1/[1-4]$ cdp add

action 1.0 comment "Trim domain name"

action 1.1 string trimright "$_nd_cdp_entry_name" ".your.domain.name"

action 1.2 string trimright "$_string_result" ".another.domain.name"

action 1.3 set _host "$_string_result"

action 2.0 comment "Convert long interface name to short"

action 2.1 string first "Ethernet" "$_nd_port_id"

action 2.2 if "$_string_result" eq 7

action 2.21 string replace "$_nd_port_id" 0 14 "Gi"

action 2.3 elseif "$_string_result" eq 10

action 2.31 string replace "$_nd_port_id" 0 17 "Te"

action 2.4 elseif "$_string_result" eq 4

action 2.41 string replace "$_nd_port_id" 0 11 "Fa"

action 2.5 end

action 2.6 set _int "$_string_result"

action 3.0 comment "Actual config of port description"

action 3.1 cli command "enable"

action 3.2 cli command "config t"

action 3.3 cli command "interface $_nd_local_intf_name"

action 3.4 cli command "description $_host:$_int"

action 3.5 cli command "do write"

action 4.0 syslog msg "EEM script updated description on $_nd_local_intf_name and saved config"

Description would be something like: my-test-sw:Gi1/1/1 instead of my-test-sw.my.domain.name:GigabitEthernet1/1/1

Question: When (and why) are "action exit" required at the end of an applet? Could not find any good information on this.

Regards

Tommy V

New Member

Improved even further, please comment and/or improve further:

  • Trim domain name - now with regexp
  • Verifiy switch or router remote end. I don't want this script to update decription on AP's and so on.
  • Do not update description if remote host and interface is unchanged. This will retain additional information in description like circuit id, vendor and so on even after a link failure
  • If description is changed, show old description in syslog message

regexp in the event are for the uplink module on 3750-X/3560-X, adapt to your own need.

event manager applet auto-update-port-description authorization bypass
description "Auto-update port-description based on CDP neighbors info"
action 0.0 comment "Event line regexp: Deside which interface to auto-update description on"

event neighbor-discovery interface regexp .*GigabitEthernet[1-9]/1/[1-4]$ cdp add

action 1.0 comment "Verify CDP neighbor to be Switch or Router"

action 1.1 regexp "(Switch|Router)" $_nd_cdp_capabilities_string

action 1.2 if $_regexp_result eq 1

action 2.0 comment "Trim domain name"

action 2.1 regexp "^([^\.]+)\." $_nd_cdp_entry_name match host

action 3.0 comment "Convert long interface name to short"

action 3.1 string first "Ethernet" "$_nd_port_id"

action 3.2 if "$_string_result" eq 7

action 3.21 string replace "$_nd_port_id" 0 14 "Gi"

action 3.3 elseif "$_string_result" eq 10

action 3.31 string replace "$_nd_port_id" 0 17 "Te"

action 3.4 elseif "$_string_result" eq 4

action 3.41 string replace "$_nd_port_id" 0 11 "Fa"

action 3.5 end

action 3.6 set int "$_string_result"

action 4.0 comment "Check old description if any, and do no change if same host:int"

action 4.1 cli command "enable"

action 4.11 cli command "config t"

action 4.2 cli command "do show interface $_nd_local_intf_name | incl Description:"

action 4.21 set olddesc "<none>"

action 4.22 set olddesc_sub1 "<none>"

action 4.23 regexp "Description: ([a-zA-Z0-9:/\-]*)([a-zA-Z0-9:/\-\ ]*)" "$_cli_result" olddesc olddesc_sub1

action 4.24 if "$olddesc_sub1" eq "$host:$int"

action 4.25 syslog msg "EEM script did NOT change desciption on $_nd_local_intf_name, since remote host and interface is unchanged"

action 4.26 exit 10

action 4.27 end

action 4.3 cli command "interface $_nd_local_intf_name"

action 4.4 cli command "description $host:$int"

action 4.5 cli command "do write"

action 4.6 syslog msg "EEM script updated description on $_nd_local_intf_name from $olddesc to Description: $host:$int and saved config"

action 5.0 end

action 6.0 exit 1

Description would be something like: my-test-sw:Gi1/1/1 instead of my-test-sw.my.domain.name:GigabitEthernet1/1/1

Does someone have a better way to shorten interface names?

Regards

Tommy V

Cisco Employee

Thanks for the mod!

Why not do something like this to shorten the port names:

action 3.1 string range $_nd_port_id 0 1

action 3.6 set int "$_string_result"

I'm trying to run this on a stack of 3750-X's running 15.0(2)SE, but I'm getting the following errors-

002407: Apr  9 16:41:33.564 CDT: %HA_EM-3-FMPD_UNKNOWN_ENV: fh_parse_var: could not find environment variable: _nd_cdp_capabilities_string

002408: Apr  9 16:41:33.564 CDT: %HA_EM-3-FMPD_ERROR: Error executing applet auto-update-port-description statement 1.1

Any suggestions on where to start looking?



Cisco Employee

In looking at the code, this variable should be set and set even in the case of capabilities being empty.  If you enable "debug event manager detector nei" do you see any messages about capabilities when the event is triggered?

I do-

002611: Apr 10 09:05:41.302 CDT: ND Update CDP Notification Event for STACK.corp.com on Gi1/0/17

002612: Apr 10 09:05:41.302 CDT: ND Update CDP Notification Event for STACK.corp.com on Gi2/0/17

002613: Apr 10 09:05:41.302 CDT: ND Update CDP Notification Event for STACK.corp.com on Gi1/0/16

002614: Apr 10 09:05:41.302 CDT: ND Update CDP Notification Event for STACK.corp.com on Gi2/0/16

002615: Apr 10 09:05:41.310 CDT: fh_fd_nd_event_match: num_matches = 0

002616: Apr 10 09:05:41.310 CDT: fh_fd_nd_event_match: num_matches = 0

002617: Apr 10 09:05:41.310 CDT: fh_fd_nd_event_match: num_matches = 0

002618: Apr 10 09:05:41.310 CDT: fh_fd_nd_event_match: num_matches = 0

Please let me preface with something that could be causing the problem. I copied the script and then added 'event none'. Is that the correct procedure?

Cisco Employee

Hi Collin,

No, this variable will not be set for the none event detector.  It will only be set for the neighbor discovery event detector.

Mike

If I remove the event none and run it (event manager run [applet name]) I get this

EEM policy update-port-description not registered with event none Event Detector

Cisco Employee

The policy in the comments as well as the policy in this doc are designed to only be run on the reception of a new CDP neighbor event.  They cannot be run manually as they are designed.

OK. Stupid question then Joe :-) How do I register it so a new CDP neighbor event changes the description?

Cisco Employee

If you take either of these examples and simply configure them on your device as-is, then they will be listening for CDP events.  You can test by clearing your CDP neighbor table.

Thanks Joe...it's slowly starting to sink in. I cleaned up the mess I was making. I cleared the cdp table. I see in the log that it sees a new event but it still does not name the port.

015051: Apr 10 13:38:19.867 CDT: ND Add CDP Notification Event for STACK.corp.com on Gi2/0/16

015052: Apr 10 13:38:19.867 CDT: Event type = CDP ADD

015053: Apr 10 13:38:19.867 CDT: Interface spec is a regexp

015054: Apr 10 13:38:19.867 CDT: Interface did not Match regexp

015055: Apr 10 13:38:19.867 CDT: nd_match_one_event: ret=FALSE

015056: Apr 10 13:38:19.867 CDT: fh_fd_nd_event_match: num_matches = 0

Cisco Employee

The regexp in the comment would not have matched.  You want something like:

event neighbor-discovery interface regexp .*GigabitEthernet[1-9]/[0-9]/[0-9]+$ cdp add

It works for routers (ASR1001) but fails on switches (remote 3750-X stack to be specific).

If you see something obvious can you give me a hint (ie what line to look at)?

Sorry for the last post Joe, your regex above got the switches working. I greatly appreciate your help (as always).

New Member

Joseph Clarke wrote:

                       

Thanks for the mod!

Why not do something like this to shorten the port names:

action 3.1 string range $_nd_port_id 0 1

action 3.6 set int "$_string_result"

                   

Hmmmm....well....

That gives "Gi" for "GigabitEthernet", but it also removes the interface number part (1/1/1)......

Regards,

Tommy V

New Member

Collin:

The event "event neighbor-discovery interface regexp .*GigabitEthernet[1-9]/1/[1-4]$ cdp add" controls what the script should act on. In this case cdp add events from the interfaces specified in the regexp. Regexp in my examples are adapted to only include the uplink/network modules on a 3750-X or a stack of 3750-X.

.*GigabitEthernet allows for interfaces GigabitEthernet with zero or more characters in front. Thus also including TenGigabitEthernet. Remembering the port numbering in a 3750 stack, the [1-9]/1/[1-4]$ part of the regexp says "switch 1-9, module 1, port 1-4". This matches the network/uplink module.

To match "any" Gigabit ports, you could use the regexp from Joseph: .*GigabitEthernet[1-9]/[0-9]/[0-9]+$

This "reads": Any interface with zero or more characters in front of GigabitEthernet, switch 1-9, modules 0-9, port 0-9999999 (the + sign equals that the digits in the square brackets in front could be repeated zero or more times).

Or you could use the regexp from the first post: GigabitEthernet.*

This reads:  Any GigabitEthernet interface with zero or more characters following.

Understanding the power of regexp could be a bit challenging, but the it's worth it

(somewhat of a regexp newbie my self )

Regards,

Tommy V

New Member

Joseph,

Any inprovements in regexp for applets i EEM v4.0?

Tried using something like:

action 3.1 regexp "(^[a-zA-Z]{2})[a-zA-Z]*([0-9/]*)" "$_nd_port_id" short_int sub1 sub2

to shorten portnames and get the interface number part, but it seems like EEM v3.2 does not support curly bracket quantifiers?

I was a little confused on how it grabbed the info but I think I understand it now. Regex is one of things (for me) where I learn it, then forget it. I don't use it enough for it to stick   I have a question for you. I'm trying to AP's as well. I added

action 1.1 regexp "(Switch|Router|Trans-Bridge)" $_nd_cdp_capabilities_string

but it doesn't see the AP's. Is there another place I should be looking?

Cisco Employee

Unfortunately, no.  CLI-based EEM uses the built-in basic regexp engine, and that is not likely to change.  If you use Tcl, you get full Tcl 8.3.4 regexp support, though.

Cisco Employee

You may want to print the capabilities string just to make sure EEM is capturing the whole thing.  You could also try matching on "AIR" in the $_nd_cdp_platform.

New Member

....or you could remove the following lines to allow the script to run on any CDP speaking neighbor:

action 1.1 regexp "(Switch|Router)" $_nd_cdp_capabilities_string

action 1.2 if $_regexp_result eq 1

action 5.0 end

Cisco Employee

Debugs can be enabled which will display the environmental variables that are published and what values those variables are set to. 

debug event manager policydir       

That may be helpful in determining what the capabilities string is set to for an AP.

Mike

Bingo that did it!

Very helpful. I'll probably use that except on access layer switches (dang IP Phones).

New Member

Hi,

I've tried the short script on post 1 on my C3560 switches. At first the script didn't work until I realized I had to change GigabitEthernet to FastEthernet since I have no gig ports on my 3560s.

The script now works but I am only getting the MAC of the IP Phones connected to the switchports and hostname of router connected to the switch. For some reason I don't get the mac of windows servers connected to the switch or Wireless access Points.

I've also tried the other two extended scripts from this thread without modification (only GigabitEthernet to FastEthernet) and didn't get any resutls.

Does anyone have a a working script for C3560-48 or C3560-24 switches?

I am running IOS c3560-ipservicesk9-mz.122-55.SE6.bin on all my switches.

Thanks in advance.

Zeek

Zeek-

This script does not pull the MAC address of non-Cisco devices and update the description. You could try and install the CDP client on the server. I'm not sure how that would look in CDP (a good thing to lab when I get some time).

http://www.tallsoft.com/cdpmonitor.htm

The extended scripts won't work with the 3560 because of the interface numbering scheme. Use the OP script.

Cisco Employee

Collin is right, but you could use the MAC Address event detector as well to create an applet that matches on learning a MAC address on a port.  Use this to set the initial description, then if CDP overwrites it you have more details.  You ED line would be something like:

event mat interface regexp ".*Ethernet.*" type add

Then you can use $_mat_intf_name to get the interface on which the event occurred and $_mat_mac_address to get the learned MAC address.

New Member

Got it, Thank you both for your replies.

New Member

Hi,

I would be VERY happy, if somebody could post the final EEM script.

:-)

Thanks!

Knut

Here you go-

event manager applet auto-update-port-description authorization bypass

description "Auto-update port-description based on CDP neighbor info"

event neighbor-discovery interface regexp .*GigabitEthernet[1-9]/[0-9]/[0-9]+$ cdp add

action 0.0  comment "Event line regexp: Deside which interface to auto-update description on"

action 1.0  comment "Verify CDP neighbor to be Switch or Router"

action 1.1  regexp "(Switch|Router|AIR)" "$_nd_cdp_capabilities_string"

action 1.2  if $_regexp_result eq "1"

action 2.0  comment "Trim domain name"

action 2.1  regexp "^([^\.]+)\." "$_nd_cdp_entry_name" match host

action 3.0  comment "Convert long interface name to short"

action 3.1  string first "Ethernet" "$_nd_port_id"

action 3.2  if $_string_result eq "7"

action 3.21 string replace "$_nd_port_id" 0 14 "Gi"

action 3.3  elseif $_string_result eq 10

action 3.31 string replace "$_nd_port_id" 0 17 "Te"

action 3.4  elseif $_string_result eq 4

action 3.41 string replace "$_nd_port_id" 0 11 "Fa"

action 3.5  end

action 3.6  set int "$_string_result"

action 4.0  comment "Check old description if any, and do no change if same host:int"

action 4.1  cli command "enable"

action 4.11 cli command "config t"

action 4.2  cli command "do show interface $_nd_local_intf_name | incl Description:"

action 4.21 set olddesc "<none>"

action 4.22 set olddesc_sub1 "<none>"

action 4.23 regexp "Description: ([a-zA-Z0-9:/\-]*)([a-zA-Z0-9:/\-\ ]*)" "$_cli_result" olddesc olddesc_sub1

action 4.24 if $olddesc_sub1 eq "$host:$int"

action 4.25 syslog msg "EEM script did NOT change desciption on $_nd_local_intf_name, since remote host and interface is unchanged"

action 4.26 exit 10

action 4.27 end

action 4.3  cli command "interface $_nd_local_intf_name"

action 4.4  cli command "description $host:$int"

action 4.5  cli command "do write"

action 4.6  syslog msg "EEM script updated description on $_nd_local_intf_name from $olddesc to Description: $host:$int and saved config"

action 5.0  end

action 6.0  exit 1

!

New Member

great! thanks Collin!

New Member

Can I use NetMRI to run this script?

Cisco Employee

This is an EEM applet that's designed to be run directly on an IOS device.  It can be deployed using an external NMS, but execution will happen locally per device.

New Member

Hi

the EEM script works very well, but Wireless Access Points (1252) are not discovered correctly. It seems to me that they propagate their capability as "Trans-Bridge" instead of "AIR". So I changed "Switch|Router|AIR" in action 1.1 to "Switch|Router|Trans-Bridge" but it was not working. I got an error message referring to action 4.4 related to pasing the hostname. Any hint would be appreciated. Thanks! Knut

New Member

Hi,

       script is working well for the newly connected device. I want to use the script for giving Interface description for the existing setup. please help.

New Member

Hi Ramz,

You have to trigger a CDP event for the script to run. The easiest way to do this, is to shut/no shut the ports.

Or you could maybe do a "no cdp enable"/"cdp enable" if you don't want to affect traffic. I have not tested this.

Regards

Tommy V

New Member

Hi Tommy,

            I think we can go ahead with the second option "no cdp enable"/"cdp enable". but the command should run only on the interfaces where the cisco devices are connected. can we incorporate the same in the same EEM script with a delay of 180+ seonds between the commands("no cdp enable"/"cdp enable").

event manager applet update-port-description
event neighbor-discovery interface regexp GigabitEthernet.* cdp add
action 1.0 cli command "enable"
action 2.0 cli command "config t"
action 3.0 cli command "interface $_nd_local_intf_name"
action 4.0 cli command "description $_nd_cdp_entry_name:$_nd_port_id"

New Member

Ramz,

Since the script triggers every time CDP info is added (as in changed or just connected), disabling CDP on a port and enabling it again will trigger the same CDP info. The script would then change the description. It should anyhow be pretty safe to test.

I short: Yes

Regards

Tommy V

New Member

You can also use the "clear cdp table" and it will trigger the script.

New Member

Of cause! Stupid me

Cisco Employee

You can use the CDP update events by changing the type to "all".  CDP messages are sent periodically, so that should trigger.  IT will, though, trigger multiple configuration changes.

New Member

Hi,

          cdp "all" & "update" are making multiple configuration changes and it is populating in my logging. Is it possible to add an event only once for clearing cdp table as soon as add the EEM script. Rest of the time it should check for CDP "add" event only.

New Member

Rantz,

Several possible way to do this.

While the applet is "installed" and using the "cdp add" event do one of the following to add any neighbors:

1) shut/no shut pr interface

2) Change the hostname of remote switch/router

3) "no cdp enable"/"cdp enable" pr interface

4) In enable mode, do a "clear cdp table"

All of these will create a "cdp add" event, and thus trigger the script to run.

Regards

Tommy V

Cisco Employee

It sounds like what would be easiest for you is to configure the applet with an "add" event, then just do a clear on the CDP table.  You'd only need to do that once.  After that, the applet will notice all new neighbors.

New Member

Thanks all for your support and suggestions.... it worked by clearing the CDP table...

New Member

i'm trying to implement this on a 4507 with IOS 15.1(1)SG1

the problem seem to be that only the activ sup is having the CDP table

F1K1-bb(config-applet)#event neighbor-discovery interface regexp .*Ethernet[1-9]/[0-9]/[0-9]+$ cdp add
%ERROR: Standby doesn't support this command
% Incomplete command.

 

Mod Ports Card Type                              Model         
---+-----+--------------------------------------+------------------
 1    24  1000BaseX (SFP)                        WS-X4624-SFP-E
 2    24  1000BaseX (SFP)                        WS-X4624-SFP-E
 3     6  Sup 6L-E 10GE (X2), 1000BaseX (SFP)    WS-X45-SUP6L-E
 4     6  Sup 6L-E 10GE (X2), 1000BaseX (SFP)    WS-X45-SUP6L-E 
 5    48  10/100/1000BaseT Premium POE E Series  WS-X4648-RJ45V+E
 6    48  10/100/1000BaseT Premium POE E Series  WS-X4648-RJ45V+E
 7    48  10/100/1000BaseT Premium POE E Series  WS-X4648-RJ45V+E

 

Cisco Employee

You get this error when you try to configure this on the active sup of a dual-sup 4500?