简体   繁体   English

两个命令或命令管道命令-Spawn Expect

[英]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). 我正在尝试将期望脚本执行到bash脚本中,但是当我尝试不使用“ | tee -a xxx.log ”(其中xxx.log是比我想写的文件来记录ssh会话)执行外壳时, 。

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 但是,当我尝试添加“ tee -a”命令来保存ssh会话时,问题是

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 :( 我试图以此方式更改“ comlog”变量,但不起作用:(

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

Does anyone know another way to save the ssh session started from expect? 有谁知道另一种保存从期望开始的ssh会话的方法? Or how can I send those two commands in same spawn command. 或如何在同一个spawn命令中发送这两个命令。

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 另外使用here-doc可以对引用有很大帮助

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

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

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