Created
February 4, 2019 19:43
-
-
Save grafuls/83f593b05c8649ee2a02ec60a10b46a9 to your computer and use it in GitHub Desktop.
Example for remote execution with paramiko
This file contains hidden or 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 paramiko | |
import argparse | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description='Execute ls on remote host') | |
parser.add_argument('--host', dest='host', type=str, default=None, help='Remote host') | |
parser.add_argument('--user', dest='user', type=str, default=None, help='Remote user') | |
parser.add_argument('--passwd', dest='passwd', type=str, default=None, help='Remote pass') | |
_args = parser.parse_args() | |
ssh = paramiko.SSHClient() | |
ssh.load_system_host_keys() | |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
ssh.connect(_args.host, username=_args.user, password=_args.passwd, look_for_keys=False, allow_agent=False) | |
transport = ssh.get_transport() | |
channel = transport.open_session() | |
channel.setblocking(1) | |
command = "ls -a" | |
stdin, stdout, stderr = ssh.exec_command(command) | |
for line in stdout.readlines(): | |
print(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment