Skip to content

Instantly share code, notes, and snippets.

@ddutt
Created September 8, 2020 06:40
Show Gist options
  • Save ddutt/72f1dacb975d5202a4ccecac2c7a41b7 to your computer and use it in GitHub Desktop.
Save ddutt/72f1dacb975d5202a4ccecac2c7a41b7 to your computer and use it in GitHub Desktop.
def ssh2_ssh(host, port=22, user='vagrant', password='vagrant'):
# Make socket, connect
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
# Initialise
session = Session()
session.handshake(sock)
session.userauth_password(user, password)
# Public key blob available as identities[0].blob
# Channel initialise, exec and wait for end
channel = session.open_session()
channel.execute(command)
channel.wait_eof()
channel.close()
channel.wait_closed()
# Print output
output = b''
size, data = channel.read()
while size > 0:
output += data
size, data = channel.read()
# Get exit status
output = output.decode("utf-8").strip()
# print(f'{host}, {output}, {channel.get_exit_status()}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment