cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
5156
Views
5
Helpful
9
Replies

EEM Script Replacement for Old Show Int Link Command

andrewdours
Level 1
Level 1

Not sure why, but Cisco did away with the "show interface link" command.  We use it all the time as various switch cables are plugged in but never removed.  This command would show us the last time the interface was active in a table sort of like show int status.  We can make a decision with this data as to whether we could safely allocate this port for another device.  I started working on a EEM applet that would step through the interfaces on a switch using "show interface" and provide the output from just 2 lines.

 

So for this show interface output, I just want to see 2 lines (underlined and bold)

GigabitEthernet1/0/1 is up, line protocol is up (connected)
Hardware is Gigabit Ethernet, address is a8b4.5601.b694 (bia a8b4.5601.b694)
Description: J317
MTU 1500 bytes, BW 1000000 Kbit/sec, DLY 10 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation ARPA, loopback not set
Keepalive set (10 sec)
Full-duplex, 1000Mb/s, media type is 10/100/1000BaseTX
input flow-control is off, output flow-control is unsupported
ARP type: ARPA, ARP Timeout 04:00:00
Last input 00:02:56, output 00:00:00, output hang never
Last clearing of "show interface" counters 2w3d
Input queue: 0/375/0/0 (size/max/drops/flushes); Total output drops: 0
Queueing strategy: Class-based queueing
Output queue: 0/40 (size/max)
5 minute input rate 0 bits/sec, 0 packets/sec
5 minute output rate 33000 bits/sec, 17 packets/sec
9592 packets input, 1965975 bytes, 0 no buffer
Received 7358 broadcasts (7301 multicasts)
0 runts, 0 giants, 0 throttles
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 watchdog, 7301 multicast, 0 pause input
0 input packets with dribble condition detected
1321044 packets output, 336466693 bytes, 0 underruns
0 output errors, 0 collisions, 2 interface resets
0 unknown protocol drops
0 babbles, 0 late collision, 0 deferred
0 lost carrier, 0 no carrier, 0 pause output
0 output buffer failures, 0 output buffers swapped out

 

I was hoping to have the output be very simple and maybe get these on the same line eventually.

GigabitEthernet1/0/1 is up, line protocol is up (connected)     Last input 00:02:56, output 00:00:00

GigabitEthernet1/0/2 is down, line protocol is down (notconnect)     Last input never, output never

...etc.

 

I haven't made it very far in the process.  Documentation is very limited.

 

Any ideas?  Not all switches have the same interface.  Some are Gi, Te, Fa, etc.  Anyone ever used any of the built in interface variables?  Maybe there is a way to pull this data from the show int status table?

 

Thanks!

1 Accepted Solution

Accepted Solutions

This is what you want:

event manager applet SHOW_INT_LINK
event cli pattern "(sh|sho|show)\s+(int|inte|inter|interf|interfa|interfac|interface)\s+(link)" enter
action 001.1 cli command "enable"
action 001.2 puts "\n\nShow Interface Link (List shows ONLY notconnect ports)\n------------------------------------------------------"
action 001.3 cli command "show int status | i notconnect"
action 002.1 foreach _line "$_cli_result" "\n"
action 002.2 regexp "^([A-Z][a-z][0-9][^[:space:]]+)" "$_line" match _intf
action 002.3 if $_regexp_result eq "1"
action 003.1 cli command "show interface $_intf"
action 004.1 foreach _iline "$_cli_result" "\n"
action 004.2 regexp "^(.*Last input.*)\r$" "$_iline" match _lastup
action 004.3 if $_regexp_result eq "1"
action 004.4 puts "$_intf -$_lastup"
action 004.5 end
action 099.1 end
action 099.2 end
action 099.3 end

View solution in original post

9 Replies 9

Seb Rupik
VIP Alumni
VIP Alumni

Hi there,

you need two regex strings. The first to match various interface names is:

\S.*Ethernet\d{1,3}\/\d{1,3}(\/\d{1,3}){0,1}

...and the second to match the 'Last input' line could be:

Last input.*$

You then need to save these matches to variables:

event manager applet some_applet
 action 01 syslog msg "Starting EEM Test"
 action 02 set test_file "test.txt"
 action 03 cli command "enable"
 action 04 cli command "show int gi1/0/1"
 action 05 regexp "\S.*Ethernet\d{1,3}\/\d{1,3}(\/\d{1,3}){0,1}" "$_cli_result" intf
 action 06 regexp "Last input.*$" "$_cli_result" lastup

 

The following will create the variables $intf and $lastup which correspond to the output of show int gi1/0/1 .

You will probably notice the above applet only works for an interface at a time and doesn't/ wouldn't work if you just tried regex'ing sh interface .

 

Personally if you wanted to do glean this output peridically I would look at scheduling a python script via cron. Python using netmiko will ssh you onto the switch prompt where you will be able to scrape the output into more managebale data structures.

 

Take a look at this script:

https://github.com/sebrupik/srupik-apic-em-tools/blob/master/frozenPony/src/oneLinerSimple.py

 

...to give you a starting point.

See this post for how to run it:

https://community.cisco.com/t5/routing/basic-python-script/td-p/3227660

 

cheers,

Seb.

 

 

 

 

I've actually gotten this far. I should have placed that in the original post. The problem is, I don't want to define static interface names in the script. We have so many different switches with different module configurations that this would just not work. I want to be able to read from show int status to get a list of interfaces on the switch and then take that data to pull the requirements from show interface "x". Is there a way to regexp show int status and just get only the interface name column (not the whole line), send that data to a file on flash, open the file from flash, and perform a "for each" line of the file?



Just a funny question. Who at Cisco holds the keys to the developer guide for EEM? Someone wrote the code to do this stuff. Where is all the documentation? Just a little frustrating. I'm not a code master, but I've written scripts and programs in C, Java, Visual Basic, etc. I just go to the internet and get the information I need to get the job done. Pretty easy...



Thanks,



Andrew


As far as I know EEM doesn't have for loops, so it is not possible to iterate through lines in a file, which was why mysupplied applet was so limited. You might be able to create a TCL script (which is supported by IOS), but personally I would be leaning towards a python script at this point.

 

The best document available is:

https://www.cisco.com/c/en/us/td/docs/ios-xml/ios/eem/configuration/15-mt/eem-15-mt-book.html

 

...but there are plenty of bespoke solutions on this forum if you know what to search for!

 

Sometimes it is best to call in our resident expert on these maters.. @Joe Clarke can you help?

 

cheers,

Seb.

You can use loops and conditionals in EEM applets provided you have EEM 3.0 or higher (which these days everyone should have).  You can iterate over the "show int status" lines:

 

cli command "show int status | inc notconn"

foreach line "$_cli_result" "\n"

 regexp "^([A-Z][a-z][0-9][^[:space:]]+)" $line match intf

 if $_regexp_result eq "1"

  puts "Interface is $intf; do your 'show int' stuff here"

 end

end

Thanks @Joe Clarke , that's a nice applet! :)

Nice Joe!



I managed to get this working (one minor issue). The last line of output is a duplicate and I can't quite figure it out.



event manager applet SHOW_INT_LINK

event cli pattern "(sh|sho|show)\s+(int|inte|inter|interf|interfa|interfac|interface)\s+(link)" enter

action 001.1 cli command "enable"

action 001.2 puts "\n\nShow Interface Link (List shows ONLY notconnect ports)\n------------------------------------------------------"

action 001.3 cli command "show int status | i notconnect"

action 002.1 foreach _line "$_cli_result" "\n"

action 002.2 regexp "^([A-Z][a-z][0-9][^[:space:]]+)" "$_line" match _intf

action 002.3 if $_regexp_result eq "1"

action 002.4 end

action 003.1 cli command "show interface $_intf"

action 004.1 foreach _line "$_cli_result" "\n"

action 004.2 regexp "^(.*Last input.*)\r$" "$_line" match _lastup

action 004.3 if $_regexp_result eq "1"

action 004.4 puts "$_intf -$_lastup"

action 004.5 end

action 099.1 end

action 099.2 end

!



Show Interface Link (List shows ONLY notconnect ports)

------------------------------------------------------

Gi1/0/2 - Last input never, output never, output hang never

Gi1/0/8 - Last input never, output never, output hang never

Gi1/0/16 - Last input never, output never, output hang never

Gi1/0/19 - Last input never, output never, output hang never

Gi1/0/27 - Last input never, output never, output hang never

Gi1/0/28 - Last input never, output never, output hang never

Gi1/0/29 - Last input never, output never, output hang never

Gi1/0/31 - Last input never, output never, output hang never

Gi1/0/32 - Last input never, output never, output hang never

Gi1/0/37 - Last input never, output never, output hang never

Gi1/0/39 - Last input never, output never, output hang never

Gi1/0/45 - Last input never, output never, output hang never

Gi1/0/47 - Last input never, output never, output hang never

Gi2/0/10 - Last input never, output never, output hang never

Gi2/0/14 - Last input never, output never, output hang never

Gi2/0/17 - Last input never, output never, output hang never

Gi2/0/22 - Last input never, output never, output hang never

Gi2/0/25 - Last input never, output never, output hang never

Gi2/0/28 - Last input never, output never, output hang never

Gi2/0/29 - Last input never, output never, output hang never

Gi2/0/30 - Last input never, output never, output hang never

Gi2/0/32 - Last input never, output never, output hang never

Gi2/0/33 - Last input never, output never, output hang never

Gi2/0/34 - Last input never, output never, output hang never

Gi2/0/35 - Last input never, output never, output hang never

Gi2/0/38 - Last input never, output never, output hang never

Gi2/0/39 - Last input never, output never, output hang never

Te3/0/3 - Last input never, output never, output hang never

Te3/0/4 - Last input never, output never, output hang never

Te4/0/1 - Last input never, output never, output hang never

Te4/0/2 - Last input never, output never, output hang never

Te4/0/3 - Last input never, output never, output hang never

Te4/0/4 - Last input never, output never, output hang never

Te4/0/4 - Last input never, output never, output hang never



Thanks,



Andrew




This is what you want:

event manager applet SHOW_INT_LINK
event cli pattern "(sh|sho|show)\s+(int|inte|inter|interf|interfa|interfac|interface)\s+(link)" enter
action 001.1 cli command "enable"
action 001.2 puts "\n\nShow Interface Link (List shows ONLY notconnect ports)\n------------------------------------------------------"
action 001.3 cli command "show int status | i notconnect"
action 002.1 foreach _line "$_cli_result" "\n"
action 002.2 regexp "^([A-Z][a-z][0-9][^[:space:]]+)" "$_line" match _intf
action 002.3 if $_regexp_result eq "1"
action 003.1 cli command "show interface $_intf"
action 004.1 foreach _iline "$_cli_result" "\n"
action 004.2 regexp "^(.*Last input.*)\r$" "$_iline" match _lastup
action 004.3 if $_regexp_result eq "1"
action 004.4 puts "$_intf -$_lastup"
action 004.5 end
action 099.1 end
action 099.2 end
action 099.3 end

Thanks Joe!



I see the fix in the removal of action 002.4 and the addition of action 009.3.



Andrew


Hi Joe ,

 

I have been trying to shutdown the interface using EEM applet , if we are using above applet it is listing the interface for which Last Input 

Never for instance. How EEM to get it work to disable the ports in that case.

 

Could you please help with this query.

 

Regards

Sanjay

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: