February 1, 2012

Show "free" (not used for a while) ports on Cisco switch with one command

Reasonably common task I come accross is to find free ports on switches (ports that havent been used for sometime). Sometimes its very easy where you can use an NMS or similar to give you a pretty output to show ports and their last input/output. However in my experience this is the exception rather than the rule. Heres a couple of examples using the pipe "|" to parse the output down a bit to only show us interfaces we want. All example shown have been tested on 2900, 3750 and 6500 switches running IOS 12.2+ (you should always check the switches uptime before taking these outputs to mean anything). Thanks to Steve B for suggesting "i proto" rather than "i GigabitE" to catch all flavours of interface.

This example shows all interfaces followed by the last input/output no matter if they are used or not.

Command: show int | i proto|Last in

Example of output

m00nie_cat#show int | i proto|Last in
GigabitEthernet1/1 is down, line protocol is down (notconnect)
  Last input 6w6d, output 6w6d, output hang never
GigabitEthernet1/2 is down, line protocol is down (notconnect)
  Last input 21w1d, output 21w1d, output hang never
GigabitEthernet1/3 is up, line protocol is up (connected)
  Last input 00:00:00, output 00:00:24, output hang never
GigabitEthernet1/4 is up, line protocol is up (connected)
  Last input 00:00:58, output 00:00:24, output hang never
GigabitEthernet1/5 is down, line protocol is down (notconnect)
  Last input never, output never, output hang never

This example is my favourite. Shows only admin down or unconnected ports that have no input/input for +6 weeks

Command: show int | i proto.notconnect|proto.administratively down|Last in. [6-9]w|Last in.[0-9][0-9]w|[0-9]y|disabled|Last input never, output never, output hang never

Example of output

m00nie_cat#$ in.* [6-9]w|Last in.*[0-9][0-9]w|[0-9]y|Last input never, output never, output hang never
  Last input never, output never, output hang never
GigabitEthernet1/1 is down, line protocol is down (notconnect)
  Last input 6w6d, output 6w6d, output hang never
GigabitEthernet1/2 is down, line protocol is down (notconnect)
  Last input 21w1d, output 21w1d, output hang never
GigabitEthernet1/5 is down, line protocol is down (notconnect)
  Last input never, output never, output hang never

There are some exceptions here where a port that is unconnected but used more recently than 6 weeks. These ports will show as below. They wont have any input/output info directly below them. I find for a very quick simple way to see free ports this works pretty well without having to mess about with other scripts. It might also show other port types (SVI, portchannel etc) that are down or have no input/output but this is quite rare in my experience.

Small caveat output

GigabitEthernet2/8 is down, line protocol is down (notconnect)
  Last input never, output never, output hang never
GigabitEthernet2/17 is down, line protocol is down (notconnect)
GigabitEthernet2/18 is down, line protocol is down (notconnect)
GigabitEthernet2/20 is down, line protocol is down (notconnect)

The 2nd command above isnt exactly easy to type/remember but used with an alias its pretty good!

m00nie_cat#conf t
Enter configuration commands, one per line.  End with CNTL/Z.
m00nie_cat(config)#al
m00nie_cat(config)#ali
m00nie_cat(config)#alias ex
m00nie_cat(config)#alias exec free show int | i proto.*notconnect|proto.*administratively down|Last in.* [6-9]w|Last in.*[0-9][0-9]w|[0-9]y|Last input never, output never, output hang never
m00nie_cat(config)#^Z
m00nie_cat#free

GigabitEthernet1/1 is down, line protocol is down (notconnect)
  Last input 6w6d, output 6w6d, output hang never
GigabitEthernet1/2 is down, line protocol is down (notconnect)
  Last input 21w1d, output 21w1d, output hang never
GigabitEthernet1/5 is down, line protocol is down (notconnect)
  Last input never, output never, output hang never

Some more info about the pipe command and regex can be found in [this post].

m00nie :D