Skip to content

Instantly share code, notes, and snippets.

@mike-zhang
Forked from ghawkgu/ssh_client.py
Last active May 3, 2017 15:55
Show Gist options
  • Save mike-zhang/5610096 to your computer and use it in GitHub Desktop.
Save mike-zhang/5610096 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import paramiko
hostname = '192.168.1.1'
port = 22
username = 'foo'
password = 'xxxYYYxxx'
if __name__ == "__main__":
#paramiko.util.log_to_file('paramiko.log')
s = paramiko.SSHClient()
#s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # use this
s.connect(hostname, port, username, password)
stdin, stdout, stderr = s.exec_command('pwd')
print stdout.read()
s.close()
import paramiko
hostname = '192.168.1.1'
port = 22
username = 'foo'
#pkey_file = '/home/foo/.ssh/id_rsa'
pkey_file = r'C:\Users\Administrator\.ssh\id_rsa'
if __name__ == "__main__":
key = paramiko.RSAKey.from_private_key_file(pkey_file)
s = paramiko.SSHClient()
s.load_system_host_keys()
#s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname,port,username,pkey=key)
stdin, stdout, stderr = s.exec_command('pwd')
print stdout.read()
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment