在expect for循环中遇到错误
期望脚本“testscript2”:
#!/usr/bin/expect set hostlist [open ./host_list] set ipaddrs [read $hostlist] foreach line [split $ipaddrs \n] { spawn scp id_dsa.pub root@$line:~/.ssh set pass "abc123" expect { "yes/no" {send "yes\r"} password: {send "$pass\r"; exp_continue} } }
除了这些恼人的错误之外,上述方法都有效,这些错误实际上对文件传输的结果没有影响:
./testscript2 spawn scp id_dsa.pub root@lsvm-nagios1:~/.ssh id_dsa.pub 100% 20 0.0KB/s 00:00 spawn scp id_dsa.pub root@:~/.ssh ssh: : Name or service not known lost connection
正如您在上面所看到的,传输发生了,但 for 循环迭代了一个额外的错误host_list 中不再有服务器名称后的时间。我认为发生的情况是,最后一次 for 循环迭代时,它看到 exp_continue 语句,并且由于 host_list 中没有更多服务器名称,因此它会抛出该错误。因此是“root@:~”。
Expect Script "testscript2":
#!/usr/bin/expect set hostlist [open ./host_list] set ipaddrs [read $hostlist] foreach line [split $ipaddrs \n] { spawn scp id_dsa.pub root@$line:~/.ssh set pass "abc123" expect { "yes/no" {send "yes\r"} password: {send "$pass\r"; exp_continue} } }
The above works except there are these annoying errors which really have no effect on the outcome of the file transfer:
./testscript2 spawn scp id_dsa.pub root@lsvm-nagios1:~/.ssh id_dsa.pub 100% 20 0.0KB/s 00:00 spawn scp id_dsa.pub root@:~/.ssh ssh: : Name or service not known lost connection
As you can see above the transfer occurs but the for loop iterates one extra time after there are no more server names in host_list. I think what is happening is that the last time the for loop iterates it sees that exp_continue statement and since there are no more server names in host_list it throws that error. Hence the "root@:~".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果 host_list 文件以换行符结尾,则 [split] 将为空的最后一行提供一个额外的元素。要跳过此操作,请尝试在生成命令之前添加以下行:
if {$line == ""} continue
If the host_list file ends with a newline character, the [split] will give an extra element for the empty last line. To skip this, try adding the following line before the spawn command:
if {$line == ""} continue