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]" exitOh 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:
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.
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.
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.
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.
Post a Comment