我使用expect通过scp将文件发送到不同的位置(一家公司的分支机构),因为WAN链接太慢我必须将文件发送到一个服务器,然后执行命令(通过ssh) )在该服务器中通过LAN重新传输原始文件(到其他系统)。现在,问题是我在两个脚本上使用了expect / ssh / scp(原始连接执行初始连接,第二个通过LAN发送文件,两者都使用ssh来执行此操作)。它们都返回相同的错误消息,因此我的(原始)脚本存在是因为在第二个脚本上发现了错误。
bash脚本在另一端执行脚本:
#!/bin/sh
for i in `cat banches.lst`
do
./execute-on-branch.exp $i "/bin/bash /tmp/retransmit.sh"
done
execute-on-branch.exp具有:
#!/usr/bin/expect -f
set host [lindex $argv 0]
set dir [lindex $argv 1]
set timeout 30
set files [ glob $dir/* ]
set scp "scp $files root@$host:/tmp/$dir/"
spawn {*}$scp
set pass "blahblahblah"
expect {
"password: " {
send "$pass\r";
expect "Permission denied, please try again." {exit 22}
}
"Connection refused" {exit 111; exp_continue}
"Connection timed out" {exit 111; exp_continue}
"connection lost" {exit 111; exp_continue}
"lost connection" {exit 111; exp_continue}
"(yes/no)? " {send "yes\r" ; set timeout -1 ; exp_continue}
-re "." { exp_continue }
eof { exit }
}
interact
retransmit.sh有:
#!/bin/bash
for i in `cat /tmp/ips.lst`
do
/tmp/send.exp $i files/
done
send.exp有:
#!/usr/bin/expect -f
set dir [lindex $argv 0]
set host [lindex $argv 1]
set timeout 30
set files [ glob $dir/* ]
set scp "scp $files root@$host:/root/$dir/"
spawn {*}$scp
set pass "blahblahblah"
expect {
"password: " {
send "$pass\r";
expect "Permission denied, please try again." {exit 22}
}
"Connection refused" {exit 111}
"Connection timed out" {exit 111}
"connection lost" {exit 111}
"lost connection" {exit 111}
"(yes/no)? " {send "yes\r" ; set timeout -1 ; exp_continue}
-re "." { exp_continue }
eof { exit }
}
interact
如果我(转发时)得到了一个" Conection拒绝"或者"连接丢失"第一个期望脚本存在是因为它应该作用于第一个服务器而不是局域网上的服务器,但它可以区分它。
有没有办法让第二个期望赶上"连接丢失"但不打印,所以第一个看不到它?
提前感谢!
直流//