Skip to content

Instantly share code, notes, and snippets.

@Ashex
Created May 20, 2015 12:14
Show Gist options
  • Save Ashex/9b78dbe03a63328bf514 to your computer and use it in GitHub Desktop.
Save Ashex/9b78dbe03a63328bf514 to your computer and use it in GitHub Desktop.
Install .NET 4
def regsearch(regpath):
r = wmi.Registry ()
result, names = r.EnumKey (
hDefKey=_winreg.HKEY_LOCAL_MACHINE,
sSubKeyName="SOFTWARE\Microsoft\NET Framework Setup\NDP"
)
return names
''' Confirm that .Net4 and the required security patch are installed
This is a really crappy check, we're pulling a list of .NET versions
from the registry and checking the version number'''
def dotnet():
dotnetchk = 0
names = regsearch("SOFTWARE\Microsoft\NET Framework Setup\NDP")
for key in names:
version = key.split('.')
if version[0] == "v4":
dotnetchk += 1
break
if dotnetchk == 0:
downfile(workdir + "\\PreReqs\\", 'http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe')
logging.info("Installing .NET 4")
subprocess.check_call(workdir + "\\PreReqs\\dotNetFx40_Full_x86_x64.exe /norestart /quiet")
logging.info(".NET 4 installed")
downfile(workdir + "\\PreReqs\\", 'http://download.microsoft.com/download/1/3/C/13CB303A-1D97-41BC-ADAC-137CBC8FF59A/NDP40-KB2656351-x64.exe')
logging.info("Installing .NET 4 Security Patch")
subprocess.check_call(os.getcwd() + "\\PreReqs\\NDP40-KB2656351-x64.exe /norestart /quiet")
logging.info(".Net 4 Security Patch Installed")
else:
logging.info(".Net 4 already installed, checking if security update is installed")
hotfixchk = 0
names = regsearch("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall")
for hotfix in names:
if "KB2656351" in hotfix:
hotfixchk += 1
break
if hotfixchk > 0:
downfile(workdir + "\\PreReqs\\", 'http://download.microsoft.com/download/1/3/C/13CB303A-1D97-41BC-ADAC-137CBC8FF59A/NDP40-KB2656351-x64.exe')
subprocess.check_call(os.getcwd() + "\\PreReqs\\NDP40-KB2656351-x64.exe /norestart /quiet")
logging.info(".Net 4 Security Patch successfully Installed")
else:
logging.info(".Net 4 Security Patch not needed!")
'''Download a file, if we get a text file we check if it's a META url redirect and handle it'''
def downfile(downdir, url):
req = urllib2.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
response = urllib2.urlopen(req)
urlsplit = os.path.split(url)
file_name = urlsplit[1]
if response.info().getheader('Content-Type') == 'text/html':
link=response.read()
response.close()
file_name = re.search('URL=(.*)">', link).group(1)
url = urlsplit[0] + '/' + file_name
if os.path.exists(downdir+file_name):
logging.info(file_name + " exists, so why download it?")
else:
logging.info('Downloading ' + file_name)
f = open(downdir+file_name, 'wb')
u = urllib2.urlopen(url)
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name, file_size)
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d Bytes [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
print status,
f.close()
return file_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment