SSH authentication with Python and paramiko

paramiko home page
SSH2 protocol for python.
 

import os
import paramiko
import sys
 
try:
	ssh = paramiko.SSHClient()
	paramiko.util.log_to_file("./errors.log")
	ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
	ssh.load_host_keys(os.path.expanduser(os.path.join("~", ".ssh", "known_hosts")))
	ssh.connect('myserver.com', username='user_name', password='pass_word')
	ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('ls /home/username/')
	print "outputn", ssh_stdout.read()
	error = ssh_stderr.read()
	print "errorsn", error, len(error)
except Exception, e:
	print "username or password is wrong or the server is unreachable"
	sys.exit(1)
%d bloggers like this: