ssh-subprocess is a small Python module that provides subprocess -like API for executing commands remotely over SSH. The module depends on OpenSSH for the SSH functionality and requires non-interactive (e.g. public key) authentication.
The module also supports SCP file transfer for uploading and downloading files and directories.
First establish connection towards remote server:
import ssh_subprocess
ssh = ssh_subprocess.Ssh(host='hostname', user='joe', host_key_checking='no')
To execute command:
result = ssh.call('echo "Hello world!" > message')
just like call()
in subprocess, call()
in ssh-subprocess
returns the exit status of the command. In this case it is the exit
status of echo
command. Also check_call()
variant is
available. It raises subprocess.CalledProcessError
in case of
non-zero exit status.
If you want to read the output printed by the remotely executed command:
message = ssh.check_output('cat message')
print message
This will print out the contents of the remote text file:
Hello world!
Lastly, popen()
allows also writing to stdin
of remotely executed
command:
p = ssh.popen('cat', stdout=subprocess.PIPE, stdin=subprocess.PIPE)
out, err = p.communicate('Hello world!\n') # write 'Hello world!\n' to stdin of 'cat'
print out
See the documentation of subprocess for more information.
Files and directories can be uploaded and downloaded by using upload()
and download()
:
ssh.upload('myfile', '/tmp/myfile.txt')
ssh.download('/home/joe/myfiles', '.', recursive=True)