cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
8140
Views
10
Helpful
5
Replies

Python Script to show interface name and input status never

Delboy82
Level 1
Level 1

Hi All

 

Im trying to write a script which SSH to a switch runs the show interface name and the using regex pulls back interface name and Last input never

 

unfortunately i'm struggling with re module and my regex.  I have included an example regex and interface output.

 

Any assistance would be appreciated

 

Regex = (^GigabitEthernet\d.\d.\d+)*(Last input never)

 

 

GigabitEthernet1/0/1 is down, line protocol is down (notconnect)
Hardware is Gigabit Ethernet, address is 5c5a.c7db.8401 (bia 5c5a.c7db.8401)
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)
Auto-duplex, Auto-speed, 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 3d17h, output 3d17h, output hang never
Last clearing of "show interface" counters never
Input queue: 0/2000/0/0 (size/max/drops/flushes); Total output drops: 20
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 0 bits/sec, 0 packets/sec
15579948 packets input, 3570223902 bytes, 0 no buffer
Received 40406 broadcasts (36191 multicasts)
3 runts, 0 giants, 0 throttles
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 watchdog, 36191 multicast, 0 pause input
0 input packets with dribble condition detected
46034823 packets output, 30241513330 bytes, 0 underruns
0 output errors, 0 collisions, 2 interface resets
846 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
GigabitEthernet1/0/2 is up, line protocol is up (connected)
Hardware is Gigabit Ethernet, address is 5c5a.c7db.8402 (bia 5c5a.c7db.8402)
Description: Airtame - IS MR
MTU 1500 bytes, BW 100000 Kbit/sec, DLY 100 usec,
reliability 255/255, txload 1/255, rxload 1/255
Encapsulation ARPA, loopback not set
Keepalive set (10 sec)
Full-duplex, 100Mb/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:13, output 00:00:00, output hang never
Last clearing of "show interface" counters never
Input queue: 0/2000/0/0 (size/max/drops/flushes); Total output drops: 5120
Queueing strategy: Class-based queueing
Output queue: 0/40 (size/max)
5 minute input rate 2000 bits/sec, 1 packets/sec
5 minute output rate 30000 bits/sec, 13 packets/sec
13045309 packets input, 2021942198 bytes, 0 no buffer
Received 3860583 broadcasts (3860553 multicasts)
0 runts, 0 giants, 0 throttles
25 input errors, 13 CRC, 0 frame, 0 overrun, 0 ignored
0 watchdog, 3860553 multicast, 2 pause input
0 input packets with dribble condition detected
55395593 packets output, 18496291841 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
GigabitEthernet1/0/3 is down, line protocol is down (notconnect)
Hardware is Gigabit Ethernet, address is 5c5a.c7db.8403 (bia 5c5a.c7db.8403)
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)
Auto-duplex, Auto-speed, 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 never, output never, output hang never
Last clearing of "show interface" counters never
Input queue: 0/2000/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 0 bits/sec, 0 packets/sec
30860 packets input, 4744663 bytes, 0 no buffer
Received 1536 broadcasts (597 multicasts)
0 runts, 0 giants, 0 throttles
0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
0 watchdog, 597 multicast, 0 pause input
0 input packets with dribble condition detected
128760 packets output, 116209358 bytes, 0 underruns
0 output errors, 0 collisions, 2 interface resets
18 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

1 Accepted Solution

Accepted Solutions

Hold on, not so fast! we can do that with a little tweak... :

targets = []
for m1 in re.finditer("(GigabitEthernet\d.\d.\d+).*?buffers swapped out", cli_input, re.DOTALL):
        m2 = re.search("(?P<interface>GigabitEthernet\d.\d.\d+).*Last input never", m1.group(0), re.DOTALL)
        if m2:
            targets.append(m2.group("interface"))

print(targets)

At the end of the run the list 'targets' will contain the interface names of all those with state 'Last input never'. You can then pass that list to another part of your script, iterate through it and run actions on each element.

 

cheers,

Seb.

View solution in original post

5 Replies 5

Seb Rupik
VIP Alumni
VIP Alumni

Hi there,

Try the following.It uses a named capture group so you can retrieve the interface name when you have a match:

import re

m = re.search("(?P<interface>GigabitEthernet\d.\d.\d+).*Last input never", cli_input, re.DOTALL)
if m:
    print(m.group("interface"))

'cli_input' is the string output of sh int gi1/1/1

 

 

cheers,

Seb.

Hi Thanks for the response but I was running the sh interface command and then wanting to return the interfaces and the Last Input status never for all interfaces that matched.

 

I ran the regex you provided and it returned only the first interface but that actually had traffic showing on the last input.

 

Can you provide any other advise?

 

Thanks 

Ah my mistake, I didn't notice the string contained multiple interfaces... Ok so we need to break it up per-interface then run a the final regex check:

 

import re

for m1 in re.finditer("(GigabitEthernet\d.\d.\d+).*?buffers swapped out", cli_input, re.DOTALL):
        m2 = re.search("(GigabitEthernet\d.\d.\d+).*Last input never", m1.group(0), re.DOTALL)
        if m2:
            print(m2.group(0))

 

 

...better?

 

cheers,

Seb.

Hi Thanks for your response its better but not quite what i was after i was really looking for the output of:  GigabitEthernet1/0/X   Last Input Never in to group so i could call just the interface names and then i can run configurations against the interfaces that dont have any input.

 

Thanks

Hold on, not so fast! we can do that with a little tweak... :

targets = []
for m1 in re.finditer("(GigabitEthernet\d.\d.\d+).*?buffers swapped out", cli_input, re.DOTALL):
        m2 = re.search("(?P<interface>GigabitEthernet\d.\d.\d+).*Last input never", m1.group(0), re.DOTALL)
        if m2:
            targets.append(m2.group("interface"))

print(targets)

At the end of the run the list 'targets' will contain the interface names of all those with state 'Last input never'. You can then pass that list to another part of your script, iterate through it and run actions on each element.

 

cheers,

Seb.

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: