使用期望脚本选择多个密码
pick multiple password using expect script
这是我的脚本:
set pwfl [open "/home/arul/Arul/BPGrep/Rest/Test/p1" r]
set pswd [split [read "$pwfl"] "\n"]
foreach pw $pswd
log_file [exec date]_Int_Push_FTP.log
spawn ftp oc0151528004 21
set timeout 30
expect "Name (*:*):" {send "arul\r\n"}
expect "*assword:" {send "$pw\r\n"}
expect "ftp>" {send "bye\r\n"}
expect "ftp>" {send "exit\r\n"}
出现这样的错误:
wrong # args: should be "foreach varList list ?varList list ...?
command"
while executing
"foreach pw $pswd" (file "Int_Push_FTP_11Jul.expect" line 4)
解决您眼前的问题:
foreach pw $pswd {
log_file ...
...
expect "ftp>" {send "exit\r"}
expect eof
}
您没有向 foreach
提供 command
参数
代码审查的其他一些要点:
- 不要转义
$pswd
变量:结果不是列表内容,而是文字字符串
$pswd
- 你只需要在发送命令中使用
\r
到"hit enter",而不是\r\n
这种读取文件行的方法将产生一个尾随空元素的列表。
- 假设文件中的最后一个字符是换行符(任何行为良好的文本文件都应该如此),那么
- 当您在换行符上拆分文件内容时,尾随换行符后的空字符串将成为最后一个列表元素。
- 因此您在尝试发送空密码的循环中获得了额外的一次迭代。
要克服这个问题,可以:
set pswd [split [read -nonewline $pwfl] \n]
或
while {[gets $pwfl pw] != -1} {
# your foreach loop body here
}
- 别忘了
close $pwfl
这是我的脚本:
set pwfl [open "/home/arul/Arul/BPGrep/Rest/Test/p1" r]
set pswd [split [read "$pwfl"] "\n"]
foreach pw $pswd
log_file [exec date]_Int_Push_FTP.log
spawn ftp oc0151528004 21
set timeout 30
expect "Name (*:*):" {send "arul\r\n"}
expect "*assword:" {send "$pw\r\n"}
expect "ftp>" {send "bye\r\n"}
expect "ftp>" {send "exit\r\n"}
出现这样的错误:
wrong # args: should be "foreach varList list ?varList list ...? command" while executing
"foreach pw $pswd" (file "Int_Push_FTP_11Jul.expect" line 4)
解决您眼前的问题:
foreach pw $pswd {
log_file ...
...
expect "ftp>" {send "exit\r"}
expect eof
}
您没有向 foreach
command
参数
代码审查的其他一些要点:
- 不要转义
$pswd
变量:结果不是列表内容,而是文字字符串
$pswd - 你只需要在发送命令中使用
\r
到"hit enter",而不是\r\n
这种读取文件行的方法将产生一个尾随空元素的列表。
- 假设文件中的最后一个字符是换行符(任何行为良好的文本文件都应该如此),那么
- 当您在换行符上拆分文件内容时,尾随换行符后的空字符串将成为最后一个列表元素。
- 因此您在尝试发送空密码的循环中获得了额外的一次迭代。
要克服这个问题,可以:
set pswd [split [read -nonewline $pwfl] \n]
或
while {[gets $pwfl pw] != -1} { # your foreach loop body here }
- 别忘了
close $pwfl