-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
127 lines (106 loc) · 3.59 KB
/
main.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
import threading
from Queue import Queue
import paramiko
url = ''
which_command = raw_input("Which mode? set-inform, info, or set-default [info]: ")
if which_command == "set-inform":
url = raw_input("Enter your set-inform URL [http://p01.hostifi.net:8080/inform]: ")
username_ = raw_input("Enter device username [ubnt]: ")
password_ = raw_input("Enter device password [ubnt]: ")
subnet_ = raw_input("Enter subnet [192.168.1.0]: ")
if url != '':
url = url
else:
url = "http://p01.hostifi.net:8080/inform"
if which_command != '':
which_command = which_command
else:
which_command = 'info'
if username_ != '':
USERNAME = username_
else:
USERNAME = 'ubnt'
if password_ != '':
PASSWORD = password_
else:
PASSWORD = 'ubnt'
if subnet_ != '':
SUBNET = subnet_
else:
SUBNET = '192.168.1.0'
SUBNET = SUBNET[:-1]
PORT = 22
COMMAND = url
hostnames = []
ips = range(1,254)
for i in ips:
hostnames.append(SUBNET + str(i))
def info(hostname, output_q):
print "Starting..."
print hostname
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, port=PORT, username=USERNAME, password=PASSWORD)
stdin, stdout, stderr = client.exec_command("mca-cli <<EOF\ninfo\nquit\nEOF")
print stdout.read()
except:
client.close()
finally:
client.close()
def set_default(hostname, output_q):
print "Starting..."
print hostname
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, port=PORT, username=USERNAME, password=PASSWORD)
stdin, stdout, stderr = client.exec_command("mca-cli <<EOF\nset-default\nquit\nEOF")
print stdout.read()
except:
client.close()
finally:
client.close()
def set_inform(hostname, output_q):
print "Starting..."
print hostname
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname, port=PORT, username=USERNAME, password=PASSWORD)
stdin, stdout, stderr = client.exec_command("mca-cli <<EOF\nset-inform %s\nquit\nEOF" % (COMMAND))
print stdout.read()
except:
client.close()
finally:
client.close()
if __name__ == "__main__":
output_q = Queue()
try:
# Start thread for each router in routers list
for hostname in hostnames:
if which_command == "set-inform":
my_thread = threading.Thread(target=set_inform, args=(hostname, output_q))
if which_command == "info":
my_thread = threading.Thread(target=info, args=(hostname, output_q))
if which_command == "set-default":
my_thread = threading.Thread(target=set_default, args=(hostname, output_q))
my_thread.start()
# Wait for all threads to complete
main_thread = threading.currentThread()
for some_thread in threading.enumerate():
if some_thread != main_thread:
some_thread.join()
# Retrieve everything off the queue - k is the router IP, v is output
# You could also write this to a file, or create a file for each router
except:
pass
while not output_q.empty():
my_dict = output_q.get()
for k, val in my_dict.iteritems():
print k
print val
wait = raw_input("Press any key to exit...")