forked from Kibbles/polyboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyboot.py
142 lines (120 loc) · 4.93 KB
/
polyboot.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
#!/usr/bin/env python
# ----------------------------------------------------------------------------------------------------------------------
# Polyboot
# ========
# A simple program to reboot or factory-reset Polycom phones using CURL commands to interact with the phone's web UI.
# Adjust the authentication password below accordingly.
#
# Usage
# ~~~~~
# ./polyboot.py (-s / -f) (ip address / file) (reboot / factory) (admin PW)
#
# Reboot (single phone /IP): polyboot.py -s 127.0.0.1 reboot 1234
# Factory Reset (single IP): polyboot.py -s 127.0.0.1 factory 1234
#
# Reboot (IP list, one per line): polyboot.py -f iplist.txt reboot 1234
# Factory Reset (IP list): polyboot.py -f iplist.txt factory 1234
# ----------------------------------------------------------------------------------------------------------------------
from subprocess import Popen
from sys import argv
from base64 import b64encode
from time import sleep
# --[ Configure these to your liking ] ---------------------------------------------------------------------------------
# Timeout between connections for a list of addresses
timeout = 0.5
# Number of phones in a list to process before pausing (to allow server to catch up with registrations etc)
batch_size = 40
# Pause duration after each batch
batch_timeout = 60
# -----------------------------------------------------------------------------------------------------------------------
# Auth string glued in front of password
auth_string = "Polycom:"
# Help text
help = '''Usage:
------
polyboot.py (-f [ip address file] or -s [single IP address]) [reboot / factory] (admin pw)
ex.: polyboot.py -s 127.0.0.1 reboot 456
'''
# Rebooting the phone
def reboot(ip):
reboot_curl = ['curl',
'-k',
'https://' + str(ip) + '/form-submit/Reboot',
'-X',
'POST',
'-H',
'Authorization: Basic ' + b64encode(admin_password),
'-H',
'Content-Length: 0',
'-H',
'Content-Type: application/x-www-form-urlencoded',
'-H',
'Cookie: Authorization=Basic ' + b64encode(admin_password)]
Popen(reboot_curl, shell=False, stdin=None, stdout=None, stderr=None, close_fds=True)
return
# Factory-resetting the phone
def factory_reset(ip):
factory_curl = ['curl',
'-k',
'https://' + str(ip) + '/form-submit/Utilities/restorePhoneToFactory',
'-X',
'POST',
'-H',
'Authorization: Basic ' + b64encode(admin_password),
'-H',
'Content-Length: 0',
'-H',
'Content-Type: application/x-www-form-urlencoded',
'-H',
'Cookie: Authorization=Basic ' + b64encode(admin_password)]
Popen(factory_curl, shell=False, stdin=None, stdout=None, stderr=None, close_fds=True)
return
if len(argv) < 5 or len(argv) > 5:
print('ERROR: Incorrect arguments supplied (got ' + str(len(argv)- 1) + ', expected 4)')
print(help)
# Multi-address (file) mode
elif argv[1] == '-f':
filename = argv[2]
admin_password = auth_string + argv[4]
try:
with open(filename) as f:
for index, line in enumerate(f):
ip = line.strip()
if argv[3] == 'reboot':
reboot(ip)
print('Reboot instruction sent to address: ' + ip)
sleep(timeout)
if (index % batch_size == 0) and (index > 1):
print('Pausing for ' + str(batch_timeout) + ' seconds between batches.')
sleep(batch_timeout)
elif argv[3] == 'factory':
factory_reset(ip)
print('Factory reset instruction sent to address: ' + ip)
sleep(timeout)
if (index % batch_size == 0) and (index > 1):
print('Pausing for ' + str(batch_timeout) + ' seconds between batches.')
sleep(batch_timeout)
else:
print('ERROR: ' + argv[3] + ' is an invalid operation flag.')
print(help)
break
except Exception as error:
print('ERROR: File couldn\'t be opened.')
print error
# Single-IP mode
elif argv[1] == '-s':
admin_password = auth_string + argv[4]
if argv[3] == 'reboot':
ip = argv[2]
reboot(ip)
print('Reboot instruction sent to address: ' + ip)
elif argv[3] == 'factory':
ip = argv[2]
factory_reset(ip)
print('Factory reset instruction sent to address: ' + ip)
else:
print('ERROR: ' + argv[3] + ' is an invalid operation flag.')
print(help)
else:
print('Unknown mode. Use either -s or -f.')
print(help)