One, for loop
Function: The port is crossed into the same VLAN.
1 to 36 ports on the 10.240.210.29 switch are crossed into the vlan210
#!/usr/bin/expect -f#set host [lindex $argv 0] #set PORT [lindex $argv 0] #set vlan [lindex $argv 1]set vlan 210for {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}
Second, while loop with custom files
Function: The port is crossed into different VLAN
43, 44 ports on the 10.240.210.7 switch are divided into vlan210, vlan310
Writes the port and VLAN number in a format to the Port_vlan.list file, the first column is the port number, and the second column is the VLAN number.
[Email protected] Scripts$cat/var/lxh/scripts/port_vlan.list 43 21044 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 $fdexit 0
Expect bulk change switch port properties under Linux