开发者

Getting error in expect for loop

开发者 https://www.devze.com 2023-03-10 06:10 出处:网络
Expect Script \"testscript2\": #!/usr/bin/expect set hostlist [open ./host_list] set ipaddrs [read $hostlist]

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@:~开发者_JAVA百科/.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@:~".


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

0

精彩评论

暂无评论...
验证码 换一张
取 消