Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

linux - 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.

Ex:

ssh -t [email protected] "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. My issue is that I couldn't find an option to keep the connection open if a commad is not found.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just replace the && with ;:

ssh -t [email protected] '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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...