Pipe output with expect spawn

对着背影说爱祢 提交于 2019-12-01 07:34:16

问题


I have script for copying some files by ssh to other server. I'm using tar for compressing files to on farchive and decompress it from stdout on other machine.

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. I tried to find any solution, but there no any suitable way for me. Can you give me an advice for this question?


回答1:


"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



回答2:


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

.i.e:

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

You got the idea.



来源:https://stackoverflow.com/questions/20587478/pipe-output-with-expect-spawn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!