RUN IT AS LOCALSYSTEM USER |
destination_folder = r"C:\Users\Public\Desktop" ## Here mention the path where the file should be copied on endpoint
filename = r"FortiClientVPN.exe" #give filename with extension here ex: "setup.exe" or "file.pdf"
fromURL=r'https://script-downloads.itarian.com/forticlient/64bit/FortiClientVPN.exe' ## Here mention the download url for the file
import os
from subprocess import PIPE, Popen
import ctypes
import ssl
import urllib2
import shutil
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
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['TEMP']
fileName = filename
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!'
print(downloadFile(DownTo, fromURL))
try:
shutil.copy2(DownTo, destination_folder)
print("%s is copied to %s"%(DownTo, destination_folder))
print "File copied successfully"
os.remove(DownTo)
except Exception as err :
print err
Comments