-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_ssh.py
45 lines (33 loc) · 1005 Bytes
/
get_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
import socket
import sys
from prettytable import PrettyTable
def get_ssh(host, port, timeout=5):
try:
# open socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
# connect to host
sock.connect((host, port))
# receive banner
banner = sock.recv(1024).decode().strip()
# close socket
sock.close()
return banner
except Exception:
return None
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python3 get_ssh.py <host1> <host2> ... <hostN>")
sys.exit(1)
hosts = sys.argv[1:]
# iterate standard and non-standard ports
ports = [22, 2200, 2222]
# create table
table = PrettyTable()
table.field_names = ["Host", "Port", "Banner"]
for host in hosts:
for port in ports:
banner = get_ssh(host, port)
if banner:
table.add_row([host, port, banner])
print(table)