一、for循环
功能:将端口划入同一vlan。
将10.240.210.29交换机上的1到36端口划入vlan210
#!/usr/bin/expect -f #set HOST [lindex $argv 0] #set PORT [lindex $argv 0] #set vlan [lindex $argv 1] set vlan 210 for {set PORT 1 } {$PORT <= 36} {incr PORT} { spawn telnet 10.240.210.29 sleep 1 expect "Username:" send "admin\r" expect "Password:" send "E7qsZy0A02yu\r" sleep 1 send "sys\r" sleep 1 send "int GigabitEthernet 1/0/$PORT\r" sleep 1 send "undo shutdown\r" sleep 1 send "port access vlan $vlan\r" sleep 1 send "loopback-detection enable vlan 1 to 4094\r" sleep 1 send "stp edged-port\r" sleep 1 send "quit\r" sleep 1 send "quit\r" sleep 1 send "quit\r" interact }
二、while循环结合自定义文件
功能:将端口划入不同的vlan
将10.240.210.7交换机上的43、44端口分别划入vlan210、vlan310
将端口与vlan号按一下格式写入port_vlan.list文件,第一列为端口号,第二列为vlan号。
root@extmail scripts$cat /var/lxh/scripts/port_vlan.list 43 210 44 310
#!/usr/bin/expect -f #set HOST [lindex $argv 0] #set PORT [lindex $argv 0] #set vlan [lindex $argv 1] set file "/var/lxh/scripts/port_vlan.list" set fd [open $file r] while {[gets $fd line]>=0 } { set PORT [lindex $line 0] set vlan [lindex $line 1] spawn telnet 10.240.210.7 sleep 1 expect "Username:" send "admin\r" expect "Password:" send "E7qsZy0A02yu\r" sleep 1 send "sys\r" sleep 1 send "int GigabitEthernet 1/0/$PORT\r" sleep 1 send "undo shutdown\r" sleep 1 send "port access vlan $vlan\r" sleep 1 send "loopback-detection enable\r" sleep 1 send "loopback-detection action semi-block\r" sleep 1 send "stp edged-port enable\r" sleep 1 send "quit\r" sleep 1 send "quit\r" sleep 1 send "quit\r" interact } close $fd exit 0
原文:http://xoyabc.blog.51cto.com/7401264/1666333