简体   繁体   中英

Two commands or command pipe command - Spawn Expect

I'm trying to execute a expect script into bash script, but when I try to execute the shell without " | tee -a xxx.log " (where xxx.log is a file than I want to write to record the ssh session).

This code works fine:

comlog="ssh $USR@192.168.228.20"
expect -c"
    spawn \"$comlog\"
    expect \"Password:\"
    send \"$PASS\r\"
    interact
"

But when I try to add the "tee -a" command to save the ssh session the issue is

invalid command name "ssh"
while executing

This is the complete command where I obtain the error message

 comlog="ssh $USR@192.168.228.20 | tee -a /home/xxx.log"
 expect -c"
    spawn \"$comlog\"
    expect \"Password:\"
    send \"$PASS\r\" #Already Obteined
    interact
 "

I tried to change the "comlog" var as this ways but doesn't work :(

 cssh $USR@192.168.228.20 \| tee -a /home/xxx.log

Does anyone know another way to save the ssh session started from expect? Or how can I send those two commands in same spawn command.

If you want to put shell metacharacters like the pipe, you'll have to spawn a shell to handle them. Also using a here-doc can help a lot with quoting

comlog="ssh $USR@192.168.228.20 | tee -a /home/xxx.log"
expect <<"END_EXPECT"
spawn sh -c "$comlog"
... rest of expect script
END_EXPECT

You can capture the expect session output this way:

 comlog="ssh $USR@192.168.228.20"
 expect -c"
    spawn \"$comlog\"
    expect \"Password:\"
    send \"$PASS\r\" #Already Obteined
    interact
 " >/home/xxx.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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