[英]How to run expect script from remote server via ssh?
I have deployed an expect script on a remote server, which I want to run via ssh. 我在远程服务器上部署了一个expect脚本,我想通过ssh运行它。
ssh user@host 'expect -d ./netopeer_expect.sh' (1)
user@host:~$ cat netopeer_expect.sh
#!/usr/bin/expect
set timeout 5
#spawn netopeer2-cli
spawn ./np2_multi_cli
expect ">"
send "listen --timeout 120\r"
expect "ru_id 0"
send "get-config -D=0 --source running --out /home/user/out.xml\r"
expect ">"
send "exit\r"
expect "$"
This code runs a modified version of the netopeer2-cli, which we call ./np2_multi_cli. 此代码运行netopeer2-cli的修改版本,我们称之为./np2_multi_cli。 This netopeer2-cli have an own shell and a prompt like >. 这个netopeer2-cli有一个自己的shell和一个像>的提示符。 It works fine when I do it in two steps 当我分两步完成时,它工作正常
ssh user@host
expect -d ./netopeer_expect.sh (2)
However, the message 但是,消息
send "get-config -D=0 --source running --out /home/user/out.xml\r"
is cut and is sent as, 被剪切并被发送为,
send "-D=0 --source running --out /home/user/out.xml\r"
From running (1) with the -d argument I see this, 从使用-d参数运行(1)我看到了,
expect: does "\[6n" (spawn_id exp3) match glob pattern ">"? 期待:“\\ u001b [6n”(spawn_id exp3)匹配glob模式“>”? no 没有
When I try to match the first >. 当我尝试匹配第一个>。 When I instead try to run (2), it looks as it should, 当我尝试运行(2)时,它看起来应该如此,
expect: does "> " (spawn_id exp4) match glob pattern ">"? 期望:“>”(spawn_id exp4)匹配glob模式“>”? yes 是
I run bash and it seems as if there are some encoding issues regarding the > character. 我运行bash,似乎有一些关于>字符的编码问题。 Any idea how to deal with this? 知道怎么处理这个吗?
BR Patrik BR Patrik
Looks as if I made a faulty call when I was running ssh. 当我运行ssh时,看起来好像打了一个错误的电话。 I forced pseudo terminal allocation it went fine, 我强制伪终端分配它很好,
ssh -t -t erusim@147.214.83.188 'expect -d ./netopeer_expect.sh'
Did some investigation and found out why ssh -t
makes a difference in patrik's answer . 做了一些调查,发现为什么ssh -t
对patrik的答案有所不同。 See the following examples: 请参阅以下示例:
According to Expect manual : 根据Expect 手册 :
Internally,
spawn
uses a pty, initialized the same way as the user's tty . 在内部,spawn
使用pty, 初始化方式与用户的tty相同 。
With -t
, ssh would allocate a pty (the same type as the local $TERM
) for the remote session, then expect
allocates a pty of the same type. 使用-t
,ssh将为远程会话分配一个pty(与本地$TERM
相同的类型),然后expect
分配相同类型的pty。
Without -t
, ssh would not allocate pty for the remote session, and expect
uses the (default?) dumb
tty which is not fully featured . 如果没有-t
,ssh就不会为远程会话分配pty,并且expect
使用(默认?) dumb
tty,它不是完全特色的 。 As a "workaround", we can explicityly set the TERM
var (eg set env(TERM) vt100
) before spawn
. 作为“变通方法”,我们可以在spawn
之前明确地设置TERM
var(例如set env(TERM) vt100
)。
Here's the command I used. 这是我使用的命令。 Just for easy copy-and-paste. 只是为了方便复制和粘贴。
[STEP 101] # cmd=' "spawn -noe bash -c {echo TERM=\$TERM | grep --color TERM}; expect eof" '
[STEP 102] #
[STEP 103] # ssh 127.0.0.1 expect -c "$cmd"
TERM=dumb
[STEP 104] # ssh -t 127.0.0.1 expect -c "$cmd"
TERM=linux
Connection to 127.0.0.1 closed.
[STEP 105] #
[STEP 106] # cmd=' "set env(TERM) vt100; spawn -noe bash -c {echo TERM=\$TERM | grep --color TERM}; expect eof" '
[STEP 107] # ssh 127.0.0.1 expect -c "$cmd"
TERM=vt100
[STEP 108] #
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.