import os import subprocess # dir: string -> directory where installer exist # args: string -> which includes all parameters with space delimiter def spm_install(dir, args): import re import _winreg import time import os os.chdir(dir) argslist = args.split() fromURL='https://packages.one.comodo.com/portal/packages/spm/Hillstone%20Secure%20Connect/x64/hill.cer'## Here mention the download url ##import os ##if not os.path.exists(dir): ## os.makedirs(dir) import ctypes class disable_file_system_redirection: _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection def __enter__(self): self.old_value = ctypes.c_long() self.success = self._disable(ctypes.byref(self.old_value)) def __exit__(self, type, value, traceback): if self.success: self._revert(self.old_value) import subprocess with disable_file_system_redirection(): import urllib Down_path=os.environ['PROGRAMDATA'] fileName = fromURL.split('/')[-1] DownTo = os.path.join(Down_path, fileName) def downloadFile(DownTo, fromURL): try: with open(DownTo, 'wb') as f: f.write(urllib.urlopen(fromURL).read()) if os.path.isfile(DownTo): return '{} - {}KB'.format(DownTo, os.path.getsize(DownTo)/1000) except: return 'Please Check URL or Download Path!' if __name__=='__main__': downloadFile(DownTo, fromURL ) import shutil try: shutil.copy2(DownTo, dir) ## print("%s is copied to %s"%(DownTo, dir)) ## print "File copied successfully" except Exception as err : print err fp=fromURL.split('/')[-1] filename=fp #Give the certificate name with extension import os import subprocess cmd=os.path.join(dir,filename) cmd1='certutil -addstore "TrustedPublisher" '+fp process= subprocess.Popen(cmd1,shell=True, stdout=subprocess.PIPE) result=process.communicate() #process1= subprocess.Popen(trust_publisher,shell=True, stdout=subprocess.PIPE) ## result=process.communicate() ## ret=process.returncode ## ## if ret==0: ## print filename+" is installed to trusted root certificate succesfully" ## ## else: ## print filename+" is not installed to trusted root certificate" try: subprocess.check_call(argslist) print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: print 'retcode' + str(e.returncode) + 'retcode' # dir: string -> directory where installer exist # args: string -> which includes all parameters with space delimiter def spm_uninstall(dir, args): os.chdir(dir) argslist = args.split() try: subprocess.check_call(argslist) print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: print 'retcode' + str(e.returncode) + 'retcode' # dir: string -> directory where installer exist # args: string -> which includes all parameters with space delimiter def spm_update(dir, args): import re import _winreg import time import os os.chdir(dir) argslist = args.split() #It uninstalls the older version which will match with the registry unins=["Hillstone Secure Connect"] os.chdir(dir) argslist = args.split() blacklist=[] for i in unins: blacklist.append(i) if 'PROGRAMFILES(X86)' in os.environ.keys(): uninstallkey='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall' reg_list=[(_winreg.HKEY_LOCAL_MACHINE,uninstallkey,_winreg.KEY_WOW64_32KEY | _winreg.KEY_ALL_ACCESS), (_winreg.HKEY_LOCAL_MACHINE,uninstallkey,_winreg.KEY_WOW64_64KEY | _winreg.KEY_ALL_ACCESS), (_winreg.HKEY_CURRENT_USER,uninstallkey,_winreg.KEY_WOW64_32KEY | _winreg.KEY_ALL_ACCESS), (_winreg.HKEY_CURRENT_USER,uninstallkey,_winreg.KEY_WOW64_64KEY | _winreg.KEY_ALL_ACCESS)] else: uninstallkey='SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall' reg_list=[(_winreg.HKEY_LOCAL_MACHINE,uninstallkey,_winreg.KEY_READ,_winreg.KEY_ALL_ACCESS), (_winreg.HKEY_CURRENT_USER,uninstallkey,_winreg.KEY_READ,_winreg.KEY_ALL_ACCESS)] strun=[] list=[] for i in reg_list: reg_key=i[0] sub_key=i[1] Value=i[2] ## reg = _winreg.HKEY_LOCAL_MACHINE ## k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, i[1]) reg =_winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, i[1], 0, i[2]) i=0 while True: try: key_value=_winreg.EnumKey(reg,i) path=os.path.join(sub_key,key_value) Hkey=_winreg.OpenKey(reg_key,path,0,Value) try: key,dis_name=_winreg.QueryValueEx(Hkey,'DisplayName') inlist=[key.strip(), path, sub_key] list.append(inlist) except: pass i+=1 except: break for c in list: for i in range(0,len(blacklist)): if blacklist[i] in c[0]: try: RawKey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, c[1],0,Value) (value, type) = _winreg.QueryValueEx(RawKey,"UninstallString") strun.append(value) Uninstr=re.findall('".*"',value) unin_str=value if "C:\Program" in unin_str: command='"'+unin_str+'" '+"/S" unarglist= command.splitlines(True) unarglist2= ''.join(unarglist) else: command="MsiExec.exe /X"+unin_str+" /qn" unarglist2 = command.split() try: subprocess.check_call( unarglist2) print blacklist[i] +'older Version are removed' time.sleep(60) except subprocess.CalledProcessError as e: print blacklist[i] +'older Version does not exist, uninstaller returncode:' + str(e.returncode); except: pass time.sleep(60) ## print ("copying and installing trusted certificates") fromURL='https://packages.one.comodo.com/portal/packages/spm/Hillstone%20Secure%20Connect/x64/hill.cer'## Here mention the download url import ctypes class disable_file_system_redirection: _disable = ctypes.windll.kernel32.Wow64DisableWow64FsRedirection _revert = ctypes.windll.kernel32.Wow64RevertWow64FsRedirection def __enter__(self): self.old_value = ctypes.c_long() self.success = self._disable(ctypes.byref(self.old_value)) def __exit__(self, type, value, traceback): if self.success: self._revert(self.old_value) import subprocess with disable_file_system_redirection(): import urllib Down_path=os.environ['PROGRAMDATA'] fileName = fromURL.split('/')[-1] DownTo = os.path.join(Down_path, fileName) def downloadFile(DownTo, fromURL): try: with open(DownTo, 'wb') as f: f.write(urllib.urlopen(fromURL).read()) if os.path.isfile(DownTo): return '{} - {}KB'.format(DownTo, os.path.getsize(DownTo)/1000) except: return 'Please Check URL or Download Path!' if __name__=='__main__': downloadFile(DownTo, fromURL ) import shutil try: shutil.copy2(DownTo, dir) ## print("%s is copied to %s"%(DownTo, dir)) ## print "File copied successfully" except Exception as err : print err fp=fromURL.split('/')[-1] filename=fp #Give the certificate name with extension import os import subprocess cmd=os.path.join(dir,filename) cmd1='certutil -addstore "TrustedPublisher" '+fp process= subprocess.Popen(cmd1,shell=True, stdout=subprocess.PIPE) result=process.communicate() #process1= subprocess.Popen(trust_publisher,shell=True, stdout=subprocess.PIPE) ## result=process.communicate() ## ret=process.returncode ## ## if ret==0: ## print filename+" is installed to trusted root certificate succesfully" ## ## else: ## print filename+" is not installed to trusted root certificate" try: subprocess.check_call(argslist) print 'retcode' + str(0) + 'retcode' except subprocess.CalledProcessError as e: print 'retcode' + str(e.returncode) + 'retcode'