Joe Smart
Joe Smart

Reputation: 763

Native SSH in Python

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?

Upvotes: 2

Views: 13605

Answers (2)

Martin Prikryl
Martin Prikryl

Reputation: 202642

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.

Upvotes: 4

Samuel
Samuel

Reputation: 3801

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.

Upvotes: 3

Related Questions