在Directoy中期望Script SCP最新文件

时间:2018-10-04 10:49:53

标签: bash ssh scripting expect scp

我正在尝试从远程计算机中获取最新文件,但是出现以下错误;

  

无法读取“(ssh root@1.1 .....” ls -t / test / * txt | head   -1“)”:没有此类变量

我的期望脚本;

SELECT CAST(CEL_slt.DATE as date)

我应该如何从带有期望脚本的远程计算机中获取最新文件?

2 个答案:

答案 0 :(得分:0)

$(...)是shell语法。要在Tcl / expect中执行相同的功能,请使用exec命令。

spawn scp -r root@$remote_ip:/test/[exec ssh root@$remote_ip "ls -t /test/*txt | head -1"] /mypath

它不必是一行,为了便于维护,请将其拆分

set latest [exec ssh root@$remote_ip "ls -t /test/*txt | head -1"]
spawn scp -r root@$remote_ip:/test/$latest /mypath

但是,我怀疑您正在使用期望发送密码,或者:

spawn ssh root@$remote_ip "ls -t /test/*txt | head -1"
expect "password"
send "$passwd\r"
expect eof
# parse $expect_out(buffer) to extract the file

但是,如果您的生活更加轻松, 您设置了ssh密钥身份验证,并且完全避免了期望:

ssh-keygen
ssh-copy-id root@$remote_ip
latest=$(ssh root@$remote_ip "ls -t /test/*txt | head -1")
scp -r root@$remote_ip:/test/$latest /mypath

答案 1 :(得分:-1)

scp -r root @ $ remote_ip:ssh root@$remote_ip ls /test/* -1td | head -1 / mypath/。