expect script 运行 while 循环内处理完一行后退出

Expect script ran inside of while loop exits after processing one line

我有一个 while 循环:

#!/bin/bash

doit="/pathtocommand"
file="/pathtosourcefile"

while read -r username password; do

$doit "$username" "$password"

done < $file

而我的 while 循环命令 ($doit) 是一个 expect 脚本。

#!/usr/bin/expect -f 

## Set up variables to be passed in as command line arguments
#set username [lindex $argv 0];
#set password [lindex $argv 1];
lassign $argv username password

spawn telnet 192.168.100.101 106
expect "200 PWD Server ready"    
send "USER user\r"
expect "300 please send the PASS"
send "PASS password\r"
expect "200 login OK, proceed"

## Use the line below for passwords that do not have to be enclosed    with quotes
send "SETACCOUNTPASSWORD $username PASSWORD $password\r"

# Use the line below for a password that must be quoted ie one that    contains a $ or a ! by escaping the double quotes
#send "SETACCOUNTPASSWORD $username PASSWORD \"$password\"\r"

expect "200 OK"
send "quit\r"
interact

expect 脚本应该 运行 与我的文件中的行数一样多。但是它在处理完第一行后停止。我相当有信心它在 expect 脚本中有一些东西,因为将命令更改为 echo 之类的东西是有效的。

如果我调试脚本,我会看到:

+ doit=/pathtocommand
+ file=/pathtofile
+ read -r username password
+ /pathtofile 0100    01000100
spawn telnet 192.168.100.101 106
Trying 192.168.100.101...
Connected to 192.168.100.101.
Escape character is '^]'.
200 PWD Server ready  
USER user
300 please send the PASS
PASS pass
200 login OK, proceed
SETACCOUNTPASSWORD 0100 PASSWORD 01000100
200 OK
quit
+ read -r username password

在我看来,脚本试图重新开始,但随后就退出了。谁能帮忙?我在 c运行ch 中让这个东西工作。我以前用 SSH 做过这个没问题。不确定它是 telnet 还是什么。

Expect 脚本中的interact 命令从标准输入读取。由于标准输入被重定向到文件,expect 将从文件中读取,下次 shell 的 while 循环将没有任何内容可供读取。

如果您希望 expect 与终端交互,您应该将其输入重定向回 /dev/tty

$doit "$username" "$password" </dev/tty