Created
November 28, 2015 21:19
-
-
Save jterstriep/96445adca4ce04c4d918 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # issuing basic ssh commands with paramiko | |
| import paramiko | |
| HOST = 'site.com' | |
| USER = 'user' | |
| PASSWORD = 'xxx' | |
| PRIVATEKEY = '/home/%s/.ssh/id_rsa' % USER | |
| ssh = paramiko.SSHClient() | |
| # Cause new hosts to be automatically added to the known_hosts file. | |
| # I'm not sure how to handle this in robust manner when the process is | |
| # running a service. | |
| ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
| # Automatically load the known_hosts file | |
| ssh.load_system_host_keys() | |
| # Loads and decrypts the private key file | |
| pkey = paramiko.RSAKey.from_private_key_file(PRIVATEKEY, password=PASSWORD) | |
| # Connect parameters to HOST, I assume this is a lazy connection | |
| ssh.connect(HOST, username=USER, pkey=pkey) | |
| # runs remote command | |
| stdin, stdout, stderr = ssh.exec_command('cat > /tmp/blahh') | |
| stdin.write('this is output to host 2') | |
| # closing stdin seems to close command, is there a better way? | |
| stdin.close() | |
| # success! | |
| stdin, stdout, stderr = ssh.exec_command('cat /tmp/blahh') | |
| print stdout.read() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment