Install_URL='https://filestore.fortinet.com/forticlient/FortiClientVPNOnlineInstaller.exe'## Here mention the download url
		
import os
from subprocess import PIPE, Popen
import ctypes
import ssl
import urllib2

fromURL=Install_URL


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)


Down_path=os.environ['PROGRAMDATA']
fileName = fromURL.split('/')[-1]
DownTo = os.path.join(Down_path, fileName)

def downloadFile(DownTo, fromURL):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}
    context = ssl._create_unverified_context()
    request = urllib2.Request(fromURL, headers=headers)
    req = urllib2.urlopen(request,context=context)
    try:
        with open(DownTo, 'wb') as f:
            while True:
                chunk = req.read(100*1000*1000)
                if chunk:
                    f.write(chunk)
                else:
                    break
        if os.path.isfile(DownTo):
            return '{} - {}KB'.format(DownTo, os.path.getsize(DownTo)/1024)
		
    except:
        return 'Please Check URL or Download Path!'

if __name__=='__main__':
    print(downloadFile(DownTo, fromURL))
    fileName = fromURL.split('/')[-1]
    print(fileName)


try:
    if os.path.exists(DownTo):
        print("check")
        ec='"%s" /quiet /norestart'%(DownTo)
        print(ec)
        with disable_file_system_redirection():
            OBJ = Popen(ec, shell = True, stdout = PIPE, stderr = PIPE)
        out, err = OBJ.communicate()
        RET = OBJ.returncode
        if RET == 0:
            print("FortiClient VPN is installed")
        else:
            print("FortiClient VPN is not installed")

    else:
        print("Please check the path")
        
except Exception as err :
    print(err)
	
if os.path.exists(DownTo):
    os.remove(DownTo)
