Last active
March 2, 2021 17:54
-
-
Save kgilpin/085c5d3921bbc863965b9f377b4d41cf to your computer and use it in GitHub Desktop.
SSH connection from Python
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
import sys | |
import os | |
import appmap | |
#start the recorder | |
r = appmap.Recording() | |
with r: | |
#run the SSH demo code | |
import sshdemo | |
sshdemo.Demo().runDemo() | |
#write recorded AppMap to disk | |
with open("sshdemo.appmap.json", "w+") as mapfile: | |
mapfile.write(appmap.generation.dump(r)) | |
mapfile.flush() |
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
import sys | |
import os | |
import paramiko | |
import select | |
class Demo: | |
def runDemo(self): | |
username = 'test' | |
password = 'test' | |
command = 'hostname' | |
ssh = paramiko.SSHClient() | |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
try: | |
ssh.connect("localhost", 22, username, password) | |
except Exception as e: | |
print("Unable to SSH: %s" % e) | |
sys.exit() | |
stdin, stdout, stderr = ssh.exec_command(command) | |
for line in stdout: | |
print("Hostname: %s" % line) | |
ssh.close() | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment