一、for循环
功能:将端口划入同一vlan。
将10.240.210.29交换机上的1到36端口划入vlan210
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#!/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号。
1
2
3
|
root@extmail scripts$cat /var/lxh/scripts/port_vlan.list
43 210
44 310
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#!/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
|
本文转自 xoyabc 51CTO博客,原文链接:http://blog.51cto.com/xoyabc/1666333,如需转载请自行联系原作者