Native SSH in Python

10,127

Solution 1

There's no native support for the SSH in Python.

All you can to is:

  • Implement the SSH/SFTP from a scratch by yourself. That's an insane task.

  • Run a command-line SFTP client (e.g. the OpenSSH sftp) from Python code.

  • Paramiko uses the LGPL license, so you might be able to take its source code and use it directly, without installing any module.

Solution 2

Another option: https://stackoverflow.com/a/1233872/524743

If you want to avoid any extra modules, you can use the subprocess module to run.

ssh [host] [command]

and capture the output.

Try something like:

process = subprocess.Popen("ssh example.com ls", shell=True,
    stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output,stderr = process.communicate()
status = process.poll()
print output

To deal with usernames and passwords, you can use subprocess to interact with the ssh process, or you could install a public key on the server to avoid the password prompt.

Share:
10,127
Joe Smart
Author by

Joe Smart

Aerospace Engineering student at the University of Liverpool. Learning Python for summer work and to help with my studies.

Updated on June 04, 2022

Comments

  • Joe Smart
    Joe Smart almost 2 years

    I have done some searching on connecting to servers and running commands on servers using SSH in Python.

    Most, if not all, recommended using Paramiko. However the servers on which the script will be running are heavily secured and it is near-impossible to install custom python libraries.

    Is there a way to connect through SSH using Python without using any libraries that aren't native to Python 2.7?