-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-ssh.py
92 lines (80 loc) · 3.28 KB
/
test-ssh.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# use paramiko to create a ssh connection and test it
import argparse
import paramiko
import time
from paramiko import SSHClient, AutoAddPolicy, RSAKey
from io import StringIO, BytesIO
import os
def ssh_connect_private_key(ip_address, username, ssh_key, ssh_port,passphrase=None, timeout=None):
try:
ssh_client = SSHClient()
ssh_client.set_missing_host_key_policy(AutoAddPolicy())
private_key = StringIO(ssh_key)
ssh_client.connect(
hostname=ip_address,
username=username,
pkey=RSAKey.from_private_key(private_key,password=passphrase),
passphrase=passphrase,
look_for_keys=False,
timeout=timeout,
port=ssh_port
)
print("Connected to ssh client using key")
return ssh_client
except Exception as ex:
print("Error connecting to ssh client using key. Exception: %s",str(ex))
return None
def ssh_connect_password(ip_address, username, password, ssh_port, timeout=None):
try:
ssh_client = SSHClient()
ssh_client.set_missing_host_key_policy(AutoAddPolicy())
ssh_client.connect(
hostname=ip_address,
username=username,
password=password,
look_for_keys=False,
timeout=timeout,
port=ssh_port
)
print("Connected to ssh client using password")
return ssh_client
except Exception as ex:
print("Error connecting to ssh client using password. Exception: %s",str(ex))
return None
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Automation script for secops cli')
parser.add_argument('-ip','--ip_address', action='store', help='ip_address',default=None)
parser.add_argument('-u', '--username', help='username', action='store', default=None)
parser.add_argument('-p', '--port', help='port', action='store', default=22)
parser.add_argument('-k', '--key', help='key', action='store', default=None)
parser.add_argument('-ph', '--passphrase', help='passphrase', action='store', default=None)
parser.add_argument('-pw', '--password', help='password', action='store', default=None)
args, unknown = parser.parse_known_args()
if args.ip_address:
print("IP address: %s" % args.ip_address)
if args.username:
print("Username: %s" % args.username)
if args.port:
print("Port: %s" % args.port)
# check if key exists on disk
if args.key:
if not os.path.exists(args.key):
print("Key file does not exist")
exit(1)
else:
print("Reading key from file")
with open(args.key, 'r') as f:
args.key = f.read()
print("SSH key found")
if not args.ip_address or not args.username:
print("Please provide ip_address and username")
exit(1)
if args.password:
ssh_client = ssh_connect_password(args.ip_address, args.username, args.password, args.port)
else:
ssh_client = ssh_connect_private_key(args.ip_address, args.username, args.key, args.port, args.passphrase)
if ssh_client is not None:
print("SSH connection successful")
ssh_client.close()
else:
print("SSH connection failed")