Created
December 22, 2017 17:07
-
-
Save rodnaxel/c1f42b0d926d43f7a61e2d8fcb0285c7 to your computer and use it in GitHub Desktop.
Example 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 argparse | |
import paramiko | |
host = '192.168.1.66' | |
user = 'pi' | |
passw = 'raspberry' | |
port = 22 | |
def create_argparse(): | |
p = argparse.ArgumentParser(description='Remote control Raspberry Pi') | |
p.add_argument('--host', action='store', dest='host', default=host, help='host') | |
p.add_argument('--user', action='store', dest='user', default=user, help='username') | |
p.add_argument('--passw', action='store', dest='passw', default=passw, help='password') | |
return p | |
def get_args(): | |
parser = create_argparse() | |
return parser.parse_args() | |
args = get_args() | |
try: | |
client = paramiko.SSHClient() | |
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
client.connect(hostname=args.host, username=args.user, password=args.passw, port=port) | |
stdin, stdout, stderr = client.exec_command('ls -l') | |
print(stdout.read()) | |
finally: | |
client.close() | |
# Exec SSH command | |
# stdin, stdout, stderr = client.exec_command('pidof CompassNew') | |
# ans = stdout.read() | |
# if ans: | |
# pid = int(ans.decode()) | |
# print(pid) | |
# stdin, stdout, stderr = client.exec_command('kill -9 {0}'.format(pid)) | |
# print(stdout.read()) | |
# print(stdout.read().decode().split('\n')) | |
# client.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment