Creating and maintaining multiple ssh sessions in Python

As soon as my program was launched, it opens any number of ssh sessions (user-defined) and runs certain commands on servers unlimited (while true loop) or until the user leaves. For efficiency reasons, I want to create each session only once, and then execute the commands until the user logs out.

How can i do this in python? I came across a cool method in another post that used subprocess to run a command and write its STDOUT. How can I start a session first and then run loopback commands?

Any links to process-like materials in Python will also be appreciated. This is my first python application.

+4
source share
4 answers

Ignoring Python for now, you can multiplex ssh sessions by adding this

 ControlMaster auto ControlPath /tmp/ssh_mux_%h_%p_%r ControlPersist 1h 

into your ~/.ssh/config file. After connecting to the machine once, the ssh session on this computer will remain open, and subsequent commands will be executed on this machine almost instantly after that. You can then use the Python subprocess to invoke ssh and execute commands on this computer as needed, and the session will be reused without doing anything special.

You can also invoke ssh with the -F flag pointing to an alternative configuration file if you do not want the session to multiplex the default behavior (or you are deploying to other users who might not have it as the default).

+6
source

OPTION 1: You can reuse the ssh process by redirecting input using PIPE.

Here is an example:

 [(Z) </tmp> ]% touch input_file [(Z) </tmp> ]% tailf input_file | ssh <remote_host> 

Now try to write something to a file

 [(Z) </tmp> ]% echo "date" >> /tmp/input_file 

Here is a way to use this in Python using the subprocess module.

 import subprocess SSH_CMD = "cat -| /usr/bin/ssh -o PasswordAuthentication=no -T -x %s " HOSTNAME = "127.0.0.1" s = subprocess.Popen(SSH_CMD%HOSTNAME , shell=True, close_fds=True, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE) 

This starts a subprocess that can be reused. Note that close_fds=True is required due to a known bug ( http://bugs.python.org/issue2320 ).

 >>> REMOTE_CMD = "date" >>> s.stdin.write( REMOTE_CMD + ... "\necho 'remote command completed with exit code = '$?\n") >>> s.stdout.readline() 'Thu Feb 16 20:01:36 PST 2012\n' >>> s.stdout.readline() 'remote command completed with exit code = 0\n' 
Line

echo 'remote command completed with exit code = '$?\n used to know that the remote command has completed and is being written to s.stdout. It is also useful to know the exit code of the remote command.

To use the same subprocess to execute another remote command:

 >>> REMOTE_CMD = "uptime" >>> s.stdin.write( REMOTE_CMD + ... "\necho 'remote command completed with exit code = '$?\n") >>> s.stdout.readline() ' 20:02:17 up 28 days, 9:15, 48 users, load average: 0.01, 0.02, 0.05\n' >>> s.stdout.readline() 'remote command completed with exit code = 0\n' 

Returning to your question, once you create the ssh subprocess, you can continue to send remote commands. Once the user completes, you can kill the subprocess.

 >>> s.kill() 

OPTION 2: I never used this, but ssh has the ControlMaster option to reuse ssh. Check the man page for ssh_config (5)

+4
source

Try using the pexpect module. It allows you to open and maintain ssh sessions, which you can reuse to send multiple commands. You can send any commands and expect certain outputs, based on which you can perform other logical operations.

0
source

Tried to mix it with multithreaded?

-one
source

Source: https://habr.com/ru/post/1396961/


All Articles