在expect for循环中遇到错误

发布于 2024-11-12 06:05:57 字数 764 浏览 1 评论 0原文

期望脚本“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2024-11-19 06:05:57

如果 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文