2017-03-09 69 views
0

我试图自动化多个Cisco交换机上交换机配置的更改。我一直到配置部分,并在发送config.txt变量时,输出在最后有大括号,并且不是有效的命令。这里是输出和下面我将发布的代码。 所有我想要做的就是登录到我的开关和运行以下命令 配置取代闪存:/ thenameofconfig 该脚本将遍历设备列表中的多个交换机和应用基于Ø使用期望语言的命令输入中的大括号

IL-测试的正确配置-SW1#配置取代闪存:{}

/IL-测试SW2-CONFG}代码:

#!/usr/bin/expect -f 

# Set up various other variables here ($user, $password) 
set username user 
set password user 
set enablepassword user 

# Log results 
#log_file -a ~/results.log 
log_file -a /tmp/results.log 







# Get the list of hosts, one per line ##### 
set f [open "device-list.txt"] 
set hostname [split [read $f] "\n"] 
close $f 

# Get the commands to run, one per line 
set f [open "configs.txt"] 
set configs [split [read $f] "\n"] 
close $f 

# Iterate over the hosts 
foreach host $hostname { 
# Don't check keys 
spawn ssh -o StrictHostKeyChecking=no $username\@$hostname 
# spawn ssh [email protected] 
# expect "password:" 
# send "$password\r" 
# Allow this script to handle ssh connection issues 
expect { 
timeout { send_user "\nTimeout Exceeded - Check Host\n"; exit 1 } 
eof { send_user "\nSSH Connection To $hostname Failed\n"; exit 1 } 
"*#" {} 
"*assword:" { 
send "$password\n" 
} 
} 

# If we're not already in enable mode, get us there 
expect { 
default { send_user "\nEnable Mode Failed - Check Password\n"; exit 1 } 
"*#" {} 
"*>" { 
send "enable\n" 
expect "*assword" 
send "$enablepassword\n" 
expect "*#" 
} 
} 

# Let's go to configure mode 
send "conf t\n" 
expect "(config)#" 
send "shell processing full\n" 
expect "(config)#" 
send "end\n" 
expect "*#" 

#Iterate over the commands 
foreach config $configs { 
expect "*#" 
send "$configs\n" 
expect "\[no]:" 
send "y\n" 
expect "*#" 
}  
    # Tidy up 
# expect "*#" 
# send "exit\r" 
# expect eof 
# close 
send "exit\n" 
expect ":~\$" 
exit 

} 

似乎无法摆脱{}在配置结束代替命令。

任何帮助将不胜感激。

回答

0

想通了。在下列行

集主机名[分割[阅读$ F] “\ n”]

需要是: 集主机名[分割[阅读-nonewline $ F] “\ n”]