我在远程服务器上部署了一个期望脚本,我想通过 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 "$"
此代码运行 netopeer2-cli 的修改版本,我们称之为 ./np2_multi_cli。这个 netopeer2-cli 有一个自己的 shell 和一个类似 > 的提示。当我分两步完成时它工作正常
ssh user@host
expect -d ./netopeer_expect.sh (2)
然而,消息
send "get-config -D=0 --source running --out /home/user/out.xml\r"
被剪切并发送为,
send "-D=0 --source running --out /home/user/out.xml\r"
从使用 -d 参数运行 (1) 我看到了这一点,
当我尝试匹配第一个 > 时。当我尝试运行 (2) 时,它看起来应该如此,
我运行 bash,似乎有一些关于 > 字符的编码问题。知道如何处理这个问题吗?
BR
帕特里克
最佳答案
做了一些调查,发现了为什么 ssh -t
对 patrik's answer 有影响。请参阅以下示例:
根据 Expect manual :
使用 -t
,ssh 将为远程 session 分配一个 pty(与本地 $TERM
相同的类型),然后 expect
分配一个相同类型的 pty。
如果没有 -t
,ssh 将不会为远程 session 分配 pty,并且 expect
使用(默认?)dumb
tty,它是 不是全功能 。作为“解决方法”,我们可以在 TERM
之前明确设置 set env(TERM) vt100
var(例如 spawn
)。
这是我使用的命令。只是为了方便复制和粘贴。
[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] #
关于bash - 如何通过ssh从远程服务器运行expect脚本?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56544454/