forked from Kerwood/Ubiquiti-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscoverUbiquiti.py
executable file
·160 lines (121 loc) · 3.89 KB
/
DiscoverUbiquiti.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/python
# -------------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 42):
# <[email protected]> wrote this script. As long as you retain this notice you
# can do whatever you want with this stuff. If we meet some day, and you think
# this stuff is worth it, you can buy me a beer in return.
#
# - Patrick Kerwood @ LinuxBloggen.dk
# -------------------------------------------------------------------------------
import paramiko
import subprocess
import argparse
import re
sshname = "admin"
sshpass = "passw0rd"
macs = [
"f0:9f:c2",
"44:d9:e7",
"04:18:d6",
"80:2a:a8",
"00:15:6d",
"24:a4:3c",
"dc:9f:db",
"68:72:51",
"00:27:22",
"fc:ec:da",
"74:83:c2",
"18:e8:29",
"78:8a:20",
"b4:fb:e4"
]
class bcolors:
OKGREEN = '\033[92m'
FAIL = '\033[91m'
ENDC = '\033[0m'
HEADER = '\033[93m'
def ping_sweep(network):
print
print ("Ping sweep in progress...")
nmap = subprocess.check_output("nmap -n -sP --send-ip %s" % network, shell=True)
nsplit = []
nsplit = nmap.split("\n")
for line in nsplit:
if "done" in line:
print(line)
def print_ubnt():
print
print ("Ubiquiti Devices\n")
arp = subprocess.check_output("arp -a", shell=True)
splitted = []
splitted = arp.split("\n")
FORMAT = '%-16s %-18s %-16s %-18s %-12s %-45s'
print FORMAT % ('IP', 'MAC', 'Model', 'Hostname', 'Version', 'Status')
colorcount = 1
for line in splitted:
bool = False
for mac in macs:
if mac in line:
bool = True
if bool:
ip = re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', line, re.I).group()
mac = re.search(r'([0-9A-F]{2}[:-]){5}([0-9A-F]{2})', line, re.I).group()
model = ""
version = ""
hostname = ""
status = ""
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, username=sshname, password=sshpass, allow_agent=False)
stdin, stdout, stderr = client.exec_command('mca-cli <<EOF\ninfo\nquit\nEOF')
if stdout.channel.recv_exit_status() == 0:
for line in stdout:
if "Model" in line:
model = line.rsplit(None, 1)[-1]
elif "Version" in line:
version = line.rsplit(None, 1)[-1]
elif "Hostname" in line:
hostname = line.rsplit(None, 1)[-1]
elif "Status" in line or "Inform" in line:
status = line.rsplit(None, 2)[-2] + " " + line.rsplit(None, 1)[-1]
if hostname == "":
stdin, stdout, stderr = client.exec_command('uname -a')
for line in stdout:
hostname = line.split(None, 2)[1]
else:
stdin, stdout, stderr = client.exec_command('uname -a')
for line in stdout:
hostname = line.split(None, 2)[1]
stdin, stdout, stderr = client.exec_command('cat /etc/version')
for line in stdout:
version = line.strip('\n')
stdin, stdout, stderr = client.exec_command('cat /etc/board.info')
for line in stdout:
if "board.name" in line:
model = line.rsplit('=', 1)[-1].strip('\n')
client.close()
except paramiko.AuthenticationException:
status = "%sAuthentication failed!%s" % (bcolors.FAIL, bcolors.ENDC)
except:
status = "%sError trying to connect!%s" % (bcolors.FAIL, bcolors.ENDC)
if colorcount % 2 == 0:
print FORMAT % (ip, mac, model, hostname, version, status)
else:
colorFormat = bcolors.HEADER + FORMAT + bcolors.ENDC
print colorFormat % (ip, mac, model, hostname, version, status)
colorcount += 1
parser = argparse.ArgumentParser()
parser.add_argument("-n", "--network", help="Make a ping sweep on subnet Eg. -n 10.0.0.0/24")
parser.add_argument("-u", "--user", help="Specify the SSH username")
parser.add_argument("-p", "--password", help="Specify the SSH paswword")
args = parser.parse_args()
if args.network:
ping_sweep(args.network)
if args.user:
sshname = args.user
if args.password:
sshpass = args.password
print_ubnt()
print