您的位置:首页 > 其它

Expect script to read file line by line

2014-12-26 17:47 357 查看
I have an expect script which is run inside a bash script. I am trying to ssh to a jump server then from the jump server telnet to each router in a file list and run the same command. I tried many ways but down not read the router list file for some reason.

I get the following error:

wrong # args: should be "read channelId ?numBytes?" or "read ?-nonewline? channelId"

while executing

"read "

Code:
/usr/local/bin/expect << EOF

 

### ssh to jump server

spawn /opt/SBCssh/bin/ssh
test@192.168.1.254

match_max 100000

expect "*?assword:*"

send "${password}\r"

send "\r"

expect "*$"

 

### read router list file into variable

set fd [open hostlist r]

set hosts [read $fd]

close $fd

 

### From jump server telnet to each router in hostlist

set data [split $hosts "\n"]

foreach line $data {

spawn telnet ${line}

expect "*login: "

send "${username}\r"

expect "Password: "

send "${password}\r"

expect "*#"

send "term len 0\r"

expect "*#"

log_file /tmp/${line}.output

send "show hard\r"

expect "*#"

log_file

send "exit\r"

}

 

### Exit jump server

set timeout 5

send "exit\r"

EOF
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: