Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Execute multiple dependent commands individually with Paramiko and find out when each command finishes

I am writing a program in Python which must communicate through SSH with a physical target, and send to this targets some commands automatically (it is for testing).

I start by doing this with Paramiko and everything was perfect until I have to send several commands and when for example the second one must be execute in the context of the first (for example the first one makes cd /mytargetRep and the second one is ./executeWhatIWant). I can't use exec_command to do so, because each exec_command starts a new session.

I try to use a channel with invoke_shell(), but I have an other problem with this one: I don't know when command execution is ended by doing this. I can have some very short (in time) command execution, and some other are really more longer so I need to know when the command execution is over.

I know a workaround it to use exec_command with a shell logic operations such as && or using ;. For example exec_command("cd /mytargetRep && ./executeWhatIWant"). But I can't do that, because it must also be possible to execute some commands manually (I have a minimalist terminal where I can send commands), so for example, the user will make cd /mytargetRep then ./executeWhatIWant and not cd /mytargetRep && ./executeWhatIWant.

So my question is: is there a solution by using Paramiko to send several commands in a same SSH session and be able to know the end of the command execution?

Thanks

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

It seems that you want to implement an interactive shell, yet you need to control individual commands execution. That's not really possible with just SSH interface. "shell" channel in SSH is black box with an input and output. So there's nothing in Paramiko that will help you implementing this.

If you need to find out when a specific command finishes or where an output of a specific command ends, you need to use features of a shell.

You can solve that by inserting a unique separator (string) in between and search for it in the channel output stream. With a common *nix shells something like this works:

channel = ssh.invoke_shell()
channel.send('cd /mytargetRep
')
channel.send('echo unique-string-separating-output-of-the-commands
')
channel.send('./executeWhatIWant
')

Though I do not really think that you need that very often. Most commands that are needed to make a specific commands working, like cd or set, do not really output anything.

So in most cases you can use SSHClient.exec_command and your code will be a way simpler and more reliable:
Execute multiple commands in Paramiko so that commands are affected by their predecessors

Even if you need to use something seemingly complex like su/sudo, it is still better to stick with SSHClient.exec_command:
Executing command using "su -l" in SSH using Python


For a similar question (for "exec" channel), see:
How to get each dependent command execution output using Paramiko exec_command


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...