首页 > 解决方案 > 循环在除了脚本变量从文本文件中获取值

问题描述

我有以下脚本:

#!/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)

标签: expect

解决方案


推荐阅读