|
| 1 | +#!/usr/bin/python3 |
| 2 | +import logging |
| 3 | +import queue |
| 4 | +import sshcontroller |
| 5 | +import threading |
| 6 | +import time |
| 7 | + |
| 8 | +logging.basicConfig(format="[%(levelname)s] %(message)s", level=logging.INFO) |
| 9 | + |
| 10 | +HOST_IP = "93.184.216.34" # an IPv4 or IPv6 address |
| 11 | +KEY_PWD = "" |
| 12 | +SSH_PWD = "" |
| 13 | + |
| 14 | + |
| 15 | +def demo_key(): |
| 16 | + ssh_controller = sshcontroller.SSHController( |
| 17 | + host=HOST_IP, |
| 18 | + user="olivier", |
| 19 | + key_path="~/.ssh/id_rsa", # if omitted, look in agent and in ~/.ssh |
| 20 | + key_password=KEY_PWD, # optional |
| 21 | + key_type="rsa", # rsa (default), dsa, ecdsa or ed25519 |
| 22 | + port=22, # 22 is the default |
| 23 | + ) |
| 24 | + |
| 25 | + ssh_controller.connect() |
| 26 | + |
| 27 | + return_code, output = ssh_controller.run( |
| 28 | + command="echo 'Hello world!' > /tmp/hello.txt", |
| 29 | + display=True, # display output, false by default |
| 30 | + combine_stderr=False, # combine stderr and stdout, false by default |
| 31 | + capture_output=True, # return output, false by default |
| 32 | + timeout=10, # command timeout in seconds, 600s by default |
| 33 | + ) |
| 34 | + logging.info(f"return code: {return_code}, output: {output}") |
| 35 | + |
| 36 | + print(f"hello.txt exists: {ssh_controller.exists('/tmp/hello.txt')}") |
| 37 | + print(f"bonjour.txt exists: {ssh_controller.exists('/tmp/bonjour.txt')}") |
| 38 | + |
| 39 | + ssh_controller.get("/tmp/hello.txt", "/tmp/bonjour.txt") |
| 40 | + |
| 41 | + with open("/tmp/bonjour.txt", 'r') as bonjour: |
| 42 | + for line in bonjour: |
| 43 | + print(line, end='') |
| 44 | + |
| 45 | + ssh_controller.disconnect() |
| 46 | + |
| 47 | + |
| 48 | +def demo_pwd(): |
| 49 | + ssh_controller = sshcontroller.SSHController( |
| 50 | + host=HOST_IP, |
| 51 | + user="root", |
| 52 | + ssh_password=SSH_PWD |
| 53 | + ) |
| 54 | + output = queue.Queue() |
| 55 | + stop_event_sleep = threading.Event() |
| 56 | + stop_event_ping = threading.Event() |
| 57 | + kwargs_sleep = { |
| 58 | + "command": "echo 'thread sleep: sleeping for 10s' && sleep 10s", |
| 59 | + "display": True, |
| 60 | + "stop_event": stop_event_sleep, |
| 61 | + } |
| 62 | + kwargs_ping = { |
| 63 | + "command": "echo 'thread ping: starting ping' && ping localhost", |
| 64 | + "display": True, |
| 65 | + "capture_output": True, |
| 66 | + "stop_event": stop_event_ping, |
| 67 | + } |
| 68 | + |
| 69 | + def wrapper(kwargs): |
| 70 | + return output.put(ssh_controller.run(**kwargs)) |
| 71 | + |
| 72 | + ssh_controller.connect() |
| 73 | + |
| 74 | + thread_sleep = threading.Thread( |
| 75 | + target=ssh_controller.run, name="thread_sleep", kwargs=kwargs_sleep) |
| 76 | + thread_ping = threading.Thread( |
| 77 | + target=wrapper, name="thread_ping", args=(kwargs_ping, )) |
| 78 | + |
| 79 | + thread_ping.start() |
| 80 | + thread_sleep.start() |
| 81 | + |
| 82 | + try: |
| 83 | + thread_sleep.join() |
| 84 | + except KeyboardInterrupt: |
| 85 | + logging.info("KeyboardInterrupt") |
| 86 | + finally: |
| 87 | + logging.info("Stopping threads") |
| 88 | + stop_event_sleep.set() |
| 89 | + stop_event_ping.set() |
| 90 | + time.sleep(2) |
| 91 | + |
| 92 | + return_code, ping_output = output.get() |
| 93 | + logging.info(f"thread ping return code: {return_code}") |
| 94 | + logging.info(f"thread ping output length: {len(ping_output)}") |
| 95 | + |
| 96 | + ssh_controller.disconnect() |
| 97 | + |
| 98 | + |
| 99 | +demo_key() |
| 100 | +demo_pwd() |
0 commit comments