Created
April 27, 2011 10:18
-
-
Save ghawkgu/944017 to your computer and use it in GitHub Desktop.
Python ssh client sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import paramiko | |
hostname = 'localhost' | |
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.connect(hostname, port, username, password) | |
stdin, stdout, stderr = s.exec_command('ifconfig') | |
print stdout.read() | |
s.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import paramiko | |
hostname = '192.168.1.1' | |
port = 22 | |
username = 'foo' | |
pkey_file = '/home/foo/.ssh/id_rsa' | |
if __name__ == "__main__": | |
key = paramiko.RSAKey.from_private_key_file(pkey_file) | |
s = paramiko.SSHClient() | |
s.load_system_host_keys() | |
s.connect(hostname, port, pkey=key) | |
stdin, stdout, stderr = s.exec_command('ifconfig') | |
print stdout.read() | |
s.close() |
Clean sample code. Thanks a lot
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you so much, it is the first one that worked