Search This Blog

Tuesday, January 11, 2011

Expect, telnet and Dell switch

Recently I've tried to find a way to obtain MAC addresses of computers attached to a Dell Switch. I've found that i.e. show bridge address-table ethernet 1/g3 returns  MAC addresses behind the port 3. I found small problem - it's not so easy to query all ports (or a chosen set of ports). Therefore, I decided to use expect. You can find the script below.
It connects to a switch run the command and finally print a mac address in the format with ':' between number doublets.
However, there are two issues. I don't know how to avoid sending everything to the standard output. Moreover, if there are more than one MAC address connected to the port (i.e. virtual machines), only first address will be print on the bottom of the output. To be precise the address  appears in the line 6 of output (see line started with set temp_mac).


#!/usr/bin/expect -f

set timeout -1

set machine [lindex $argv 0]
set port [lindex $argv 1]
set command "show bridge address-table ethernet 1/g$port\n"

#Connect to the server
spawn telnet $machine 

expect "User:"
exp_send "admin\r"
expect "Password:"
exp_send "myXEN\r"
expect "?*>"
exp_send "enable\r"
expect "?*#"
exp_send $command
expect "?*#"
set temp_mac [ lindex [ lindex [ split $expect_out(0,string) "\n"] 6] 1]
exp_send "exit\r"
exp_send "quit\r"

puts "\n"

# Creating mac address in DHCP format (with ':')
set mac [ string range $temp_mac 0 1 ]
append mac "."
append mac [ string range $temp_mac 2 6 ]
append mac "."
append mac [ string range $temp_mac 7 11 ]
append mac "."
append mac [ string range $temp_mac 12 13 ]

puts  "$machine/$port:  [ string map  {. :} $mac]"

exit
Oh one more thing. To make a list of all following it's good to run the script in loop and the following one should be good base to start with.

for ((i=1;i<48;i++)) do mac-dell.expect esw44-1 $i| grep "esw44-1/$i"; done

4 comments:

rozei said...

Why not to use SNMP, which is dedicated for getting data from network devices? Take a look at this link. MIB .1.3.6.1.2.1.17.4.3.1.2 works with most devices, the only problem(?) is, that it returns MAC in dec not hex, so they need to be converted.

If smth is unclear, and you wish to use SNMP, feel free to ask a question.

Wawrzek said...

You are right. I was thinking about SNMP, but my knowledge in that are is limited and that week I was working with expect, so decided to polish that area first.

rozie said...

SNMP is simple. And - if you have access to network devices - is pretty necessary to use. I can't imagine making traffic, error, link UP/DOWN statistics or monitoring without it. Main reason is efficiency - SNMP is very fast, comparing to telnet - but also often MIBs (especially basic one, like traffic, errors on interfaces) are the same on different devices. OTOH telnet CLI varies not only from vendor to vendor, but often from model to model or sometimes even between firmware versions.

Wawrzek said...

SNMP is one of the next target on the list of things to standardize/improve/set up. AFAIK it's going to be helpful in monitoring many things, we would like to monitor.