Skip to content

Instantly share code, notes, and snippets.

@rdkls
Last active September 6, 2018 11:19
Show Gist options
  • Select an option

  • Save rdkls/e665d1d33792f9cc96f52721543539b1 to your computer and use it in GitHub Desktop.

Select an option

Save rdkls/e665d1d33792f9cc96f52721543539b1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Connect to a host with specified public key
# Failure will raise paramiko.ssh_exception.BadHostKeyException w00p w00p!
import paramiko
import base64
host = 'localhost'
port = '8022'
keytype = 'ssh-rsa'
host_key_b64 = 'AAAAB3NzaC1yc2EAAAADAQABAAABAQDKwtRsDG4PpErhl0orhYZ6GLb/xPk81hUm7QWDMbAt3BKz1GrTnbZ0VZjmDE/joXVs6cNt9UvFvpVymwbx2IdqY9qN4DrWpZtzQ4l1asgcGGVbzX0wj2r6ZJbm9AhPW9WDZ4Ke/Hwbs/MxxKkEuQYRJekfnFTO1zRu1xyptwuLCS6P+Y79W+EiFLV8/9jZHjRlcpD+Fi4K0NSluDOrXw6Zn5XqXPSAYYkAOQnFGTfZuOGu5iyK1KVEGO7YS1WCAqnmyVF1RqJI1ehEUqjhqd8UYlD0Uq7KlPuA+EIzsDdEZ9vhMEbPBf0tgh9Lt+3UaTFNTGsdoDSdWuM13v+BR463'
username = 'root'
password = 'root'
key = paramiko.RSAKey(data=base64.b64decode(host_key_b64))
client = paramiko.SSHClient()
#client.load_system_host_keys() # will cause to load from known_hosts - without this or get_host_keys().add() there will be NO known hosts
client.get_host_keys().add('[%s]:%s' % (host, port), keytype, key)
client.connect(host, port=port, username=username, password=password)
stdin, stdout, stderr = client.exec_command('ls')
for line in stdout:
print('... ' + line.strip('\n'))
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment