简体   繁体   English

使用伪终端通过 ssh 执行命令:如果找不到命令,请不要关闭连接

[英]Executing a command thru ssh using pseudo terminal: do not close connection if command is not found

I want to write a small script to automate some commands in python for unix and linux, thru pexpect.我想编写一个小脚本来自动化 python 中的一些命令,用于 unix 和 linux,通过 pexpect。

Ex:前任:

ssh -t sk@192.168.225.22 "uname -r && some_cmd_that_exist_or_not && exec /bin/sh"

The problem, is that I don't know from the beginning what type of the OS the machine has, and in some cases the commands are not available.问题是我从一开始就不知道这台机器的操作系统是什么类型的,在某些情况下命令不可用。

In this case the connection is closed and a "command not found is returned".在这种情况下,连接被关闭并返回“找不到命令”。 I want to continue executing even if a command is not found.即使找不到命令,我也想继续执行。

After that I retrive the output using pyexpect.之后,我使用 pyexpect 检索 output。 My issue is that I couldn't find an option to keep the connection open if a commad is not found.我的问题是,如果找不到逗号,我找不到保持连接打开的选项。

Just replace the && with ;只需将&&替换为; :

ssh -t sk@192.168.225.22 'uname -r; some_cmd_that_may_not_exist; exec /bin/sh'

The && is a short-circuiting operator, so it will stop executing commands as soon as any of the commands fail. &&是一个短路运算符,因此一旦任何命令失败,它将停止执行命令。 If you want to run them all, just don't use it.如果您想全部运行它们,请不要使用它。

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

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