简体   繁体   English

如何通过ssh从远程服务器运行expect脚本?

[英]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 "\" (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 -tpatrik的答案有所不同。 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.

相关问题 如何使用ssh和expect在远程服务器中运行功能? - How to run function in remote server using ssh and expect? 如何使用expect和bash脚本在远程服务器中运行本地脚本 - how to run a local script in remote server using expect and bash script 通过ssh连接到远程并运行带有期望的git命令 - Connect to remote via ssh and run git commands with expect 如何通过SSH bash脚本在远程服务器上cd? - How to cd on remote server via SSH bash script? 如何使用 nohup 通过 ssh 在远程服务器上执行本地 bash 脚本 - How to execute a local bash script on remote server via ssh with nohup 在Linux中,如何使用ssh和Expect在远程服务器执行期间监视shell脚本的进度 - In Linux , how to monitor the progress of shell script during execution in remote server using ssh and expect 我如何在服务器中运行Expect脚本以SSH并在bash脚本中设置值 - How can I run an expect script to SSH in a server and set a value inside a bash script 如何通过Shell脚本在远程服务器上连接和运行Shell脚本 - How to connect and run shell script on remote server, via shell script 如何使用外壳程序脚本从远程服务器SSH读取.json文件 - How to read a .json file from a remote server SSH with a shell script 如何通过SSH运行Shell脚本,以使远程计算机的环境与本地计算机的环境相似? - How can I run a shell script via SSH such that the environment of the remote computer is similar to that of the local computer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM