Expect bulk change switch port properties under Linux

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.