使用期望语言在命令输入中使用括号

时间:2017-03-09 17:53:27

标签: expect

我正在尝试自动更改多个cisco交换机上的交换机配置。我一直到配置部分,当发送config.txt变量时,输出结尾有大括号,不是有效命令。这是输出,下面我将发布代码。 我要做的就是登录我的交换机并运行以下命令 configure replace flash:/ thenameofconfig 该脚本将遍历设备列表中的多个开关,并应用正确的配置o

IL-test-SW1 #configure replace flash:/ il-test-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 $user@host
#    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

}

似乎无法摆脱configure replace命令末尾的{}。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

想出来。是以下行

设置主机名[split [read $ f]“\ n”]

需要: 设置主机名[split [read -nonewline $ f]“\ n”]