我的目的是控制两台远程机器之间的文件传输。如果我可以使用
key-pair
,那就容易多了,但我不能选择选项,只能传递密码。
环境:
Local(Mac)
、Remote_source(Linux)
、Remote_target(Linux)
这是我的expect.sh。当我运行它时,会弹出
source@source's password:
,然后是 target@target's password:
,没有其他内容。没有文件传输,没有标准输出,没有错误消息,只是断开连接。
#!/usr/bin/expect
set timeout -1
spawn scp -3 scp://remote_source_user@host:port/home/source/test.txt scp://remote_target_user@host:port/home/target/
expect "remote_source_user@host's password:"
send "SOURCE_PASSWORD\r"
expect "remote_target_user@host's password:"
send "TARGET_PASSWORD\r"
expect eof
当我更改密码序列(意味着先发送目标密码而不是先发送源密码。)时,会发生错误,所以我猜它正在工作,但我不知道内部发生了什么。
我已经尝试过
sshpass
,但它无法使用多个密码。
我用
-v flag
调试了一下,发现Connection工作正常,但问题出在目录上。
# in debugging mode
Sink: scp: home/username/test.txt: No such file or directory
一开始似乎不认识
/
home/username/test.txt。
所以代码应该如下所示:
#!/usr/bin/expect
set timeout -1
spawn scp -3 scp://remote_source_user@host:port/~/test.txt scp://remote_target_user@host:port/~/
expect "remote_source_user@host's password:"
send "SOURCE_PASSWORD\r"
expect "remote_target_user@host's password:"
send "TARGET_PASSWORD\r"
expect eof
不要明确写入您的主目录
/home/username/
,而是使用 /~/
。
# The Result
debug1: Sending command: scp -v -r -f ~/test.txt
Sending file modes: C0000 00 test.txt
Sink: C0000 00 test.txt
在端口号后使用单个 / 会被解释为 相对于用户的主目录。
如果您想要绝对路径,请在端口号后使用双//,例如:
scp -3 scp://<username>@<host>:<port>//path/to/file
这适用于源和目的地。