expect - 循环在除了脚本变量从文本文件中获取值
问题描述
我有以下脚本:
#!/usr/bin/expect -f
# Set variables
set hostname [lindex $argv 0]
set username [lindex $argv 3]
set password [lindex $argv 1]
set devname [lindex $argv 4]
set enablepassword [lindex $argv 2]
set conft 0
# Log results
log_file -a ~/scripts/ExpectScripts/9120CiscoAPs/results.log
# Announce which device we are working on and at what time
send_user "\n"
send_user ">>>>> Working on $hostname $devname @ [exec date] <<<<<\n"
send_user "\n"
# Don't check keys
set timeout 30
spawn ssh -o StrictHostKeyChecking=no -o ConnectTimeout=30 $username\@$hostname
expect "User:"
send "$username\n"
expect "Password:"
send "$password\n"
expect "*>"
set f [open "aprebootlist2.txt"]
set aps [split [read $f] "\n"]
close $f
foreach ap $aps {
send "config ap reset $ap\n"
expect {
"*n)" {send "y\n"}
"*invalid." {send "\r"}
}
expect eof
close
}
send "\r"
expect "*>"
send "logout\n"
expect "*(y/N)"
send "n\r"
exit
这应该登录到思科无线控制器并基本上运行命令“ap config reset apvariable”很多次。我试图创建一个循环,从文本文件中获取 ap 名称的变量。该脚本登录到控制器,读取文件中的名字,然后失败并出现以下错误。我究竟做错了什么?错误是:
Cisco Controller) >send: spawn id exp6 not open
while executing
"send "config ap reset $ap\n""
("foreach" body line 2)
invoked from within
"foreach ap $aps {
send "config ap reset $ap\n"
expect {
"*n)" {send "y\n"}
"*invalid." {send "\r"}
}
expect eof
..."
(file "./rebootapcommands2.exp" line 29)
解决方案
推荐阅读
- python - 将边界框区域转换为遮罩并将其保存为 PNG 文件
- python - 如何聚合,即根据查询的某个属性获取查询集
- javascript - 知道 forEach 循环何时完成
- javascript - 使用@react-native-firebase/messaging 收到通知后应用程序崩溃
- javascript - 如何在 Jest 中测试异步 XMLHttpRequest 回调?
- react-native - 如何在 React Native 的类组件中调用钩子?
- c - 在哈希表中安装键值时出现分段错误
- css - 更改rails中的标题字体
- odoo-13 - create_from_ui() 接受 2 个位置参数
- php - 即使设置了 PHPSESSID 并且 session_start() 已被调用 PHP,会话也不活动