Skip to content

Commit 9264a84

Browse files
committed
the parallelism update
1 parent 1b1f009 commit 9264a84

File tree

5 files changed

+37
-21
lines changed

5 files changed

+37
-21
lines changed

libs/net.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from netaddr import IPAddress
33
import netifaces as ni
44
from libs.phone import *
5+
import csv
56

67
def getIfIPs():
78
print("Getting interface IPs.")

libs/phone.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import urllib3
2-
from bs4 import BeautifulSoup
3-
import csv
2+
from bs4 import BeautifulSoup
43
import requests
54
import base64
65
import re
@@ -54,23 +53,27 @@ def auth(self):
5453
self.session = requests.session()
5554
self.session.adapters.pop("https://", None)
5655
self.session.mount("https://", CustomSSLContextHTTPAdapter(ctx))
56+
self.basicAuth = ('Polycom', self.pw)
5757
authstring = bytes(f"Polycom:{self.pw}", encoding="utf-8")
5858
# Check if password works
5959
resp = self.session.get(endpointJs, verify=False)
6060
js = resp.text
6161
authType = re.search(r'type: .*', js, re.MULTILINE).group(0)
6262
authType = self.charReplace(["'", '"', ","], authType)
63+
authType = authType.replace('type: ', '')
6364
authType = authType.strip().lower()
65+
authEndpoint = authType.replace('url: ', '')
6466
authEndpoint = re.search(r'url: .*.htm', js, re.MULTILINE).group(0)
6567
authEndpoint = self.charReplace(["'", '"', ","], authEndpoint)
68+
authEndpoint = authEndpoint.replace('url: ', '')
6669
authEndpoint = endpointBase + authEndpoint.strip()
6770
if authType == 'get':
68-
resp = self.session.post(authEndpoint, auth=('Polycom', self.pw), verify=False)
71+
resp = self.session.get(authEndpoint, auth=self.basicAuth, verify=False)
6972
if authType == 'post':
70-
resp = self.session.post(authEndpoint, auth=('Polycom', self.pw), verify=False)
71-
if "INVALID" in resp.text:
72-
return False
73-
if resp.status_code == 200:
73+
resp = self.session.post(authEndpoint, auth=self.basicAuth, verify=False)
74+
if "INVALID" in resp.text or "Failed" in resp.text:
75+
return False
76+
elif resp.status_code == 200:
7477
# return the session to simplify usage later.
7578
self.session.cookies = resp.cookies
7679
if not self.session.cookies:
@@ -84,7 +87,7 @@ def parseNames(self):
8487

8588
configKeys = {}
8689
endpoint = f'https://{self.ip}/provConf.htm'
87-
resp = self.session.get(endpoint, cookies=self.session.cookies, verify=False)
90+
resp = self.session.get(endpoint, auth=self.basicAuth, cookies=self.session.cookies, verify=False)
8891
soup = BeautifulSoup(resp.text, 'xml')
8992
for index, key in self.paramKeys.items():
9093
tag = soup.find('input', {"paramName": key})
@@ -108,7 +111,7 @@ def setProvisioning(self):
108111
if self[key]:
109112
#Only pull values we do have
110113
data[index] = self[key]
111-
resp = self.session.post(f'https://{self.ip}/form-submit', cookies=self.session.cookies, verify=False, data=data)
114+
resp = self.session.post(f'https://{self.ip}/form-submit', auth=self.basicAuth, cookies=self.session.cookies, verify=False, data=data)
112115
if "CONF_CHANGE" in resp.text:
113116
return True
114117
else:

main.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
from libs.net import *
33
from libs.phone import *
4+
from joblib import Parallel, delayed
45

56
### CSV Headers
67
### mac,pw,servertype,serverurl,serveruser,serverpass,tries,retrywait,tagsnua
@@ -13,15 +14,19 @@ def main():
1314
epilog='Alexankitty 2024')
1415
parser.add_argument('csvfile', help="CSV File of all MACs and Passwords for the phones to provision")
1516
parser.add_argument('-ip', '--ip-address', dest='ipaddress', help="IP Address in CIDR notation to scan for phones")
17+
parser.add_argument('-p', '--parallel-jobs', dest='jobs', help='Sets the limit on the number of phones that can be done simultaneously')
1618

1719
args = parser.parse_args()
1820
iparr = []
21+
jobs = 5
1922

2023
if not args.ipaddress:
2124
#Grab all interface IPs if the IP CIDR is not supplied
2225
iparr = getIfIPs()
2326
else:
2427
iparr.append(args.ipaddress)
28+
if args.jobs:
29+
jobs = args.jobs
2530

2631
phoneIPs = scanNetwork(iparr)
2732
phones = parseCsv(args.csvfile)
@@ -30,22 +35,28 @@ def main():
3035
phoneArr = phonetuple[0]
3136
failures = phonetuple[1]
3237
if phoneArr:
33-
for phone in phoneArr:
34-
try:
35-
#todo: make parallel
36-
session = phone.auth()
37-
except requests.exceptions.ConnectionError as e:
38-
failures.append(f'{phone["ip"]} {phone["mac"]}: {e.args[0].reason}')
38+
results = Parallel(n_jobs = jobs)(delayed(phoneHandler)(phone) for phone in phoneArr)
39+
#for phone in phoneArr:
40+
#phoneHandler(phone)
41+
for result in results:
42+
if result == True:
3943
continue
40-
if not session:
41-
failures.append(f'{phone["ip"]} {phone["mac"]}: Authentication failed')
42-
continue
43-
if not phone.setProvisioning():
44-
failures.append(f'{phone["ip"]} {phone["mac"]}: Configuration failed')
44+
failures.append(result)
4545
for failure in failures:
4646
print(failure)
4747
if not failures:
4848
print("All phones configured successfully. :)")
4949

50+
def phoneHandler(phone):
51+
try:
52+
session = phone.auth()
53+
except requests.exceptions.ConnectionError as e:
54+
return f'{phone["ip"]} {phone["mac"]}: {e.args[0].reason}'
55+
if not session:
56+
return f'{phone["ip"]} {phone["mac"]}: Authentication failed'
57+
if not phone.setProvisioning():
58+
return f'{phone["ip"]} {phone["mac"]}: Configuration failed'
59+
return True
60+
5061
if __name__=="__main__":
5162
main()

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ requests
55
cryptography
66
bs4
77
pyOpenSSL
8-
lxml
8+
lxml
9+
joblib

start.bat

Whitespace-only changes.

0 commit comments

Comments
 (0)