Skip to content

Instantly share code, notes, and snippets.

@kgilpin
Last active March 2, 2021 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgilpin/085c5d3921bbc863965b9f377b4d41cf to your computer and use it in GitHub Desktop.
Save kgilpin/085c5d3921bbc863965b9f377b4d41cf to your computer and use it in GitHub Desktop.
SSH connection from Python
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()
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