Paramiko

  • ssh2 package for python
  • ssh2 (Secure Shell 2) is a network protocol for secure data communication.
  • best-known application is access to shell accounts on Unix-like systems.
import paramiko, time

ssh = paramiko.SSHClient()
# Don't worry about known key
# Don't put this in if you're worried about security.
ssh.set_missing_host_key_policy(
    paramiko.AutoAddPolicy())
ssh.connect(some.computer.somewhere, username=myusername)

# Do some sftp
ftp = ssh.open_sftp()
ftp.mkdir("new_remote_directory")
ftp.put("local_directory/some_file", "new_remote_directory")
ftp.pull("another_remote_directory/another_file", "local_directory")
ftp.close()

# Execute some remote commands
channel = ssh.get_transport().open_session()
channel.execute_command("some command here")
# Wait for it to finish
while not channel.exit_status_ready():
    time.sleep(1)
stdout = channel.makefile("rb")
output = stdout.readlines()