简体   繁体   English

带有预期生成的管道输出

[英]Pipe output with expect spawn

I have script for copying some files by ssh to other server. 我有用于通过ssh将某些文件复制到其他服务器的脚本。 I'm using tar for compressing files to on farchive and decompress it from stdout on other machine. 我正在使用tar将文件压缩到farchive上,并从其他计算机上的stdout解压缩它。

set timeout -1

# user info
set port [lindex $argv 0]
set login [lindex $argv 1]
set password [lindex $argv 3]
set host [lindex $argv 2]

#tar info
set sdir [lindex $argv 4]
set ddir [lindex $argv 5]

spawn tar cf - $sdir | ssh -p $port $login@$host tar xf - -C $ddir

expect "*?(yes/no)" {
    send "yes\r"
}

expect "*?assword" {
    send "$password\r"
}

expect "#" {
    send "ls $ddir -la\r"
}

expect "#" {
    send "exit\r"
}

interact

But '|' 但是“ |” doesn't work with spawn. 不适用于spawn。 I tried to find any solution, but there no any suitable way for me. 我试图找到任何解决方案,但是没有适合我的方法。 Can you give me an advice for this question? 您能给我一个关于这个问题的建议吗?

"spawn" the shell “产生”外壳

set timeout -1

# user info
set port [lindex $argv 0]
set login [lindex $argv 1]
set password [lindex $argv 3]
set host [lindex $argv 2]

#tar info
set sdir [lindex $argv 4]
set ddir [lindex $argv 5]

spawn $env(SHELL) 

expect "#" {
    send "(tar cf - \$sdir) | (ssh -p \$port \$login@\$host tar xf - -C \$ddir)\r"
} 

expect -re "(yes/no)" {
    send "yes\r"
}

expect -re "assword" {
    send "$password\r"
}

expect "#" {
    send "exit\r"
}

interact

If you can't setup keys, you can put the "tar ... | ssh ..." line into another script and call it with spawn, just pass the required parameter 如果您无法设置密钥,则可以将“ tar ... | ssh ...”行放入另一个脚本中并使用spawn进行调用,只需传递所需的参数即可

.ie: .IE:

spawn anotherscript.bash $sdir $port $login $host

You got the idea. 你明白了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM