mmmaaak
mmmaaak

Reputation: 833

Pipe output with expect spawn

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?

Upvotes: 5

Views: 6523

Answers (2)

MrBouba
MrBouba

Reputation: 41

"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

Upvotes: 4

h2oreactor
h2oreactor

Reputation: 33

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.

Upvotes: 2

Related Questions