-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssh_client.py
39 lines (31 loc) · 1.26 KB
/
ssh_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import argparse
import paramiko
from paramiko.client import SSHClient
pr = argparse.ArgumentParser()
pr.add_argument('--host', type=str, required=True)
pr.add_argument('--port', type=int, required=True)
pr.add_argument('--username', type=str, required=False)
pr.add_argument('--password', type=str, required=False)
args = pr.parse_args()
def ssh_client():
try:
client = SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(args.host, port=args.port, username=args.username, password=args.password, timeout=float(5))
print("'Connection established'\n"+"#"*10+"\n" + args.host+":"+str(args.port))
except paramiko.ssh_exception.AuthenticationException:
print("'Authentication failed | Check required inputs'")
exit()
while True:
try:
commands = str(input("Execute >> "))
stdin, stdout, stderr = client.exec_command(command=commands)
outlines = stdout.readlines()
resp = ''.join(outlines)
print(resp)
except KeyboardInterrupt:
client.close()
print("\n'Connection terminated'")
return False
ssh_client()