Created
September 8, 2020 06:40
-
-
Save ddutt/72f1dacb975d5202a4ccecac2c7a41b7 to your computer and use it in GitHub Desktop.
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
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