-
Notifications
You must be signed in to change notification settings - Fork 49
/
speedtestSample.py
64 lines (53 loc) · 2.4 KB
/
speedtestSample.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
#!/usr/bin/env python
# Determines the time it takes to download a 10MB pkg from the distribution point for the enrollment app to calculate download times.
# Sample code for Open Source project. used examples from https://docs.python.org/3/library/urllib.request.html and
#
# SPDX-License-Identifier: GPL-3.0-only
import sys
import time
import urllib
import ssl
import plistlib
import os
import subprocess
import commands
import datetime
########### VARIABLES THAT NEED TO BE CHANGED PER ENVIRONMENT ###########
# url is the path of a 10mb pkg to get a sample time from.
'''
url example https://jss_distrobution_point.com/SamplePackage.pkg
If you are using Enable Remote Authentication in the JSS you may need to create a non expiring download.
'''
url="https://my10mb.pkg"
######################################################################
################## DO NOT CHANGE BELOW THIS LINE ##################
lastrun = datetime.datetime.today().strftime('%Y-%m-%d')
speedtest = []
filename="/private/tmp/sample.pkg"
# reporthook function will calculate how long it takes to download the package from the save function.
def reporthook(number, blocksize, totalSize):
global starttime
if number == 0:
starttime = time.time()
return
speed = int(int(number * blocksize) / (1024 * (time.time() - starttime)))
speedtest.append(speed)
# Save function downloads the url specified and saves it to the file location specified in filename.
def save(url, filename):
ssl._create_default_https_context = ssl._create_unverified_context
urllib.urlretrieve(url, filename, reporthook)
#__main__
# run the save function passing the url and filename.
result=save(url,filename)
result = str(round(int(sum(speedtest) / float(len(speedtest))) * float(0.000976562),2))
print result
# Write the results to the plist file for the enrollment app.
sub_command = "/bin/ls -la /dev/console | /usr/bin/cut -d ' ' -f 4"
loggedInUser = subprocess.Popen(sub_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
loggedInUser = loggedInUser.communicate()[0]
loggedInUser = loggedInUser.strip()
print "logged in user is: %s" % loggedInUser
filePath = "/Users/%s/Library/Preferences/com.ibm.enrollment.plist" % loggedInUser
print filePath
sub_command = ['sudo', '-u', loggedInUser, 'defaults', 'write', filePath, 'speedTestResult', result]
ssUser = subprocess.Popen(sub_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)