Advertisement
Guest User

Untitled

a guest
Sep 28th, 2017
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.48 KB | None | 0 0
  1. bitsadmin /transfer myDownloadJob /download /priority normal http://downloadsrv/10mb.zip c:10mb.zip
  2.  
  3. @echo off
  4. setlocal
  5.  
  6. :: uses bitsadmin utility to download a file
  7. :: bitsadmin is not available in winXP Home edition
  8. :: the only way to download a file with 'pure' batch
  9. :download
  10.  
  11. if "%2" equ "" (
  12. call :help
  13. exit /b 5
  14. )
  15.  
  16. if "%1" equ "" (
  17. call :help
  18. exit /b 6
  19. )
  20. set url=%~1
  21. set file=%~2
  22. rem ----
  23. if "%~3" NEQ "" (
  24. set /A timeout=%~3
  25. ) else (
  26. set timeout=5
  27. )
  28.  
  29. bitsadmin /cancel download >nul
  30. bitsadmin /create /download download >nul
  31. call bitsadmin /addfile download "%url%" "%CD%%file%" >nul
  32. bitsadmin /resume download >nul
  33. bitsadmin /setproxysettings download AUTODETECT >nul
  34.  
  35. set /a attempts=0
  36. :repeat
  37. set /a attempts +=1
  38. if "%attempts%" EQU "10" (
  39. echo TIMED OUT
  40. endlocal
  41. exit /b 1
  42. )
  43. bitsadmin /info download /verbose | find "STATE: ERROR" >nul 2>&1 && endlocal && bitsadmin /cancel download && echo SOME KIND OF ERROR && exit /b 2
  44. bitsadmin /info download /verbose | find "STATE: SUSPENDED" >nul 2>&1 && endlocal && bitsadmin /cancel download &&echo FILE WAS NOT ADDED && exit /b 3
  45. bitsadmin /info download /verbose | find "STATE: TRANSIENT_ERROR" >nul 2>&1 && endlocal && bitsadmin /cancel download &&echo TRANSIENT ERROR && exit /b 4
  46. bitsadmin /info download /verbose | find "STATE: TRANSFERRED" >nul 2>&1 && goto :finishing
  47.  
  48. w32tm /stripchart /computer:localhost /period:1 /dataonly /samples:%timeout% >nul 2>&1
  49. goto :repeat
  50. :finishing
  51. bitsadmin /complete download >nul
  52. echo download finished
  53. endlocal
  54. goto :eof
  55.  
  56. :help
  57. echo %~n0 url file [timeout]
  58. echo.
  59. echo url - the source for download
  60. echo file - file name in local directory where the file will be stored
  61. echo timeout - number in seconds between each check if download is complete (attempts are 10)
  62. echo.
  63. goto :eof
  64.  
  65. call winhhtpjs.bat https://example.com/files/some.zip -saveTo c:somezip.zip
  66.  
  67. @if (@X)==(@Y) @end /* JScript comment
  68. @echo off
  69.  
  70. rem :: the first argument is the script name as it will be used for proper help message
  71. cscript //E:JScript //nologo "%~f0" "%~nx0" %*
  72.  
  73. exit /b %errorlevel%
  74.  
  75. @if (@X)==(@Y) @end JScript comment */
  76.  
  77. // used resources
  78. //http://www.codeproject.com/Tips/506439/Downloading-files-with-VBScript
  79. //http://blogs.msdn.com/b/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
  80. //https://msdn.microsoft.com/en-us/library/ie/ms535874(v=vs.85).aspx
  81. //https://msdn.microsoft.com/en-us/library/aa923283.aspx
  82. //https://msdn.microsoft.com/en-us/library/ms759148(v=vs.85).aspx
  83. //https://msdn.microsoft.com/en-us/library/ms759148(v=vs.85).aspx
  84. //https://msdn.microsoft.com/en-us/library/ms760236(v=vs.85).aspx
  85. //http://stackoverflow.com/questions/20712635/providing-authentication-info-via-msxml2-serverxmlhttp
  86. //https://msdn.microsoft.com/en-us/library/ms763680(v=vs.85).aspx
  87. //https://msdn.microsoft.com/en-us/library/ms757849(v=vs.85).aspx
  88. //http://fm4dd.com/programming/shell/microsoft-vbs-http-download.htm
  89. //http://stackoverflow.com/questions/11573022/vba-serverxmlhttp-https-request-with-self-signed-certificate
  90. //http://www.qtcentre.org/threads/44629-Using-XMLHttpRequest-for-HTTPS-Post-to-server-with-SSL-certificate
  91.  
  92. // global variables and constants
  93. var ARGS = WScript.Arguments;
  94. var scriptName=ARGS.Item(0);
  95.  
  96. var url="";
  97. var saveTo="";
  98.  
  99. var user=0;
  100. var pass=0;
  101.  
  102. var proxy=0;
  103. var bypass="";
  104. var proxy_user=0;
  105. var proxy_pass=0;
  106.  
  107. var certificate=0;
  108.  
  109. var force=true;
  110.  
  111. //ActiveX objects
  112. //Use the right version of MSXML
  113. /*var progIDs = [ 'Msxml2.DOMDocument.6.0', 'Msxml2.DOMDocument.5.0', 'Msxml2.DOMDocument.4.0', 'Msxml2.DOMDocument.3.0', 'Msxml2.DOMDocument' ]
  114. for (var i = 0; i < progIDs.length; i++) {
  115. try {
  116. var XMLHTTPObj = new ActiveXObject(progIDs[i]);
  117. }catch (ex) {
  118. }
  119. }
  120.  
  121. if typeof XMLHTTPObj === 'undefined'{
  122. WScript.Echo ("You are using too ancient windows or you have no installed IE");
  123. WScript.Quit(1);
  124. }*/
  125.  
  126. var XMLHTTPObj = new ActiveXObject("MSXML2.XMLHTTP");
  127. var FileSystemObj = new ActiveXObject("Scripting.FileSystemObject");
  128. var AdoDBObj = new ActiveXObject("ADODB.Stream");
  129.  
  130.  
  131. function printHelp(){
  132. WScript.Echo(scriptName + " - downloads a file through HTTP");
  133. WScript.Echo(scriptName + " url localfile [-force yse|no] [-user username -password password] [-proxy proxyserver:port -bypass bypass_list]");
  134. WScript.Echo(" [-proxyuser proxy_username -proxypassword proxy_password] [-certificate certificateString]");
  135. WScript.Echo("-force - decide to not or to overwrite if the local exists");
  136. WScript.Echo("proxyserver:port - the proxy server");
  137. WScript.Echo("bypass- bypass list can be "" if you don't need it");
  138. WScript.Echo("proxy_user , proxy_password - credentials for proxy server");
  139. WScript.Echo("user , password - credentials for the server");
  140. WScript.Echo("certificate - location of SSL certificate");
  141. WScript.Echo("Example:");
  142. WScript.Echo(scriptName +" http://somelink.com/somefile.zip c:\somefile.zip -certificate "LOCAL_MACHINE\Personal\My Middle-Tier Certificate"");
  143. }
  144.  
  145. function parseArgs(){
  146. //
  147. if (ARGS.Length < 3) {
  148. WScript.Echo("insufficient arguments");
  149. printHelp();
  150. WScript.Quit(43);
  151. }
  152. url=ARGS.Item(1);
  153. saveTo=ARGS.Item(2);
  154.  
  155. if(ARGS.Length % 2 != 1) {
  156. WScript.Echo("illegal arguments");
  157. printHelp();
  158. WScript.Quit(44);
  159. }
  160.  
  161. for (var i=3;i<ARGS.Length-1;i=i+2){
  162. if(ARGS.Item(i).toLowerCase=="-force" && ARGS.Item(i+1)=='no'){
  163. force=false;
  164. }
  165.  
  166. if(ARGS.Item(i).toLowerCase=="-user"){
  167. user=ARGS.Item(i+1);
  168. }
  169.  
  170. if(ARGS.Item(i).toLowerCase=="-password"){
  171. pass=ARGS.Item(i+1);
  172. }
  173.  
  174. if(ARGS.Item(i).toLowerCase=="-proxy"){
  175. proxy=ARGS.Item(i+1);
  176. }
  177.  
  178. if(ARGS.Item(i).toLowerCase=="-bypass"){
  179. bypass=ARGS.Item(i+1);
  180. }
  181.  
  182. if(ARGS.Item(i).toLowerCase=="-proxyuser"){
  183. proxy_user=ARGS.Item(i+1);
  184. }
  185.  
  186. if(ARGS.Item(i).toLowerCase=="-proxypassword"){
  187. proxy_pass=ARGS.Item(i+1);
  188. }
  189.  
  190. if(ARGS.Item(i).toLowerCase=="-certificate"){
  191. certificate=ARGS.Item(i+1);
  192. }
  193. }
  194. }
  195.  
  196. function existsItem(path){
  197. return FileSystemObj.FolderExists(path)||FileSystemObj.FileExists(path);
  198. }
  199.  
  200. stripTrailingSlash = function(path){
  201. while (path.substr(path.length - 1,path.length) == '\') {
  202. path=path.substr(0, path.length - 1);
  203. }
  204. return path;
  205. }
  206.  
  207. function deleteItem(path){
  208. if (FileSystemObj.FileExists(path)){
  209. FileSystemObj.DeleteFile(path);
  210. return true;
  211. } else if (FileSystemObj.FolderExists(path) ) {
  212. FileSystemObj.DeleteFolder(stripTrailingSlash(path));
  213. return true;
  214. } else {
  215. return false;
  216. }
  217. }
  218.  
  219. function writeFile(fileName,data ){
  220. AdoDBObj.Type = 1;
  221. AdoDBObj.Open();
  222. AdoDBObj.Position=0;
  223. AdoDBObj.Write(data);
  224. AdoDBObj.SaveToFile(fileName,2);
  225. AdoDBObj.Close();
  226. }
  227.  
  228. function download( url,file){
  229. if (force && existsItem(file)){
  230. if(!deleteItem(file)){
  231. WScript.Echo("Unable to delete "+ file);
  232. WScript.Quit(8);
  233. }
  234. }else if (existsItem(file)){
  235. WScript.Echo("Item " + file + " already exist");
  236. WScript.Quit(9);
  237. }
  238.  
  239.  
  240.  
  241. if (proxy!=0 && bypass !="") {
  242. //https://msdn.microsoft.com/en-us/library/ms760236(v=vs.85).aspx
  243. XMLHTTPObj.setProxy(SXH_PROXY_SET_DIRECT,proxy,bypass);
  244. } else if (proxy!=0) {
  245. XMLHTTPObj.setProxy(SXH_PROXY_SET_DIRECT,proxy,"");
  246. }
  247.  
  248.  
  249.  
  250. if (proxy_user!=0 && proxy_pass!=0 ) {
  251. //https://msdn.microsoft.com/en-us/library/ms763680(v=vs.85).aspx
  252. XMLHTTPObj.setProxyCredentials(proxy_user,proxy_pass);
  253. }
  254.  
  255. if(certificate!=0) {
  256. //https://msdn.microsoft.com/en-us/library/ms763811(v=vs.85).aspx
  257. WinHTTPObj.setOption(3,certificate);
  258. }
  259.  
  260. if (user!=0 && pass!=0){
  261. //https://msdn.microsoft.com/en-us/library/ms757849(v=vs.85).aspx
  262. XMLHTTPObj.Open('GET',url,false,user,pass);
  263. } else {
  264. XMLHTTPObj.Open('GET',url,false);
  265. }
  266.  
  267.  
  268.  
  269. XMLHTTPObj.Send();
  270. var status=XMLHTTPObj.Status
  271.  
  272. switch(status){
  273. case 200:
  274. WScript.Echo("Status: 200 OK");
  275. break;
  276. case 401:
  277. WScript.Echo("Status: 401 Unauthorized");
  278. WScript.Echo("Check if correct user and password were provided");
  279. WScript.Quit(401);
  280. break;
  281. case 407:
  282. Wscript.Echo("Status:407 Proxy Authentication Required");
  283. Wscript.Echo("Check if correct proxy user and password were provided");
  284. WScript.Quit(407);
  285. break;
  286. default:
  287. Wscript.Echo("Status: "+status);
  288. WScript.Echo("Try to help yourself -> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes");
  289. WScript.Quit(status);
  290. }
  291. writeFile(file,XMLHTTPObj.ResponseBody);
  292. }
  293.  
  294. function main(){
  295. parseArgs();
  296. download(url,saveTo);
  297. }
  298. main();
  299.  
  300. @if (@X)==(@Y) @end /* JScript comment
  301. @echo off
  302. setlocal
  303.  
  304. for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%Microsoft.NETFramework*jsc.exe"') do (
  305. set "jsc=%%v"
  306. )
  307.  
  308. ::if not exist "%~n0.exe" (
  309. "%jsc%" /nologo /out:"%~n0.exe" "%~dpsfnx0"
  310. ::)
  311.  
  312. %~n0.exe %*
  313.  
  314. endlocal & exit /b %errorlevel%
  315.  
  316.  
  317. */
  318.  
  319. //todo SSL Support
  320. //todo Better help message
  321. //todo check if local file exists
  322.  
  323.  
  324. import System;
  325. import System.Net.WebClient;
  326. import System.Net.NetworkCredential;
  327. import System.Net.WebProxy;
  328. import System.Uri;
  329. import System.Security.Cryptography.X509Certificates;
  330.  
  331. var arguments:String[] = Environment.GetCommandLineArgs();
  332.  
  333. var url=0;
  334. var toFile=0;
  335. var force=true;
  336.  
  337. var user=0;
  338. var password=0;
  339.  
  340. var proxy=0;
  341. var bypass=0;
  342. var proxy_user=0;
  343. var proxy_pass=0;
  344.  
  345. var certificate=0;
  346.  
  347. function printHelp(){
  348. Console.WriteLine(arguments[0] + "download from url to a file");
  349. Console.WriteLine(arguments[0] + "<url> <file> [-user user -password password] [-proxy proxy] [-proxy_user proxy.user -proxy_pass proxy.pass]");
  350.  
  351. }
  352.  
  353. function parseArgs(){
  354.  
  355. if (arguments.length < 3) {
  356. Console.WriteLine("Wrong arguments");
  357. printHelp();
  358. Environment.Exit(1);
  359. }
  360.  
  361. if (arguments.length %2 != 1) {
  362. Console.WriteLine("Wrong number arguments");
  363. printHelp();
  364. Environment.Exit(2);
  365. }
  366.  
  367. url=arguments[1];
  368. toFile=arguments[2];
  369.  
  370. for (var i=3;i<arguments.length-1;i=i+2){
  371. var arg=arguments[i].ToLower();
  372. switch (arg){
  373. case "-user" :
  374. user=arguments[i+1];
  375. break;
  376. case "-password" :
  377. password=arguments[i+1];
  378. break;
  379. case "-proxy" :
  380. proxy=arguments[i+1];
  381. break;
  382. case "-proxy_user" :
  383. proxy_user=arguments[i+1];
  384. break;
  385. case "-proxy_pass" :
  386. proxy_pass=arguments[i+1];
  387. break;
  388. case "-bypass" :
  389. bypass=[arguments[i+1]];
  390. break;
  391. /*case "-certificate" :
  392. certificate=arguments[i+1];
  393. break;*/
  394. default:
  395. Console.WriteLine("Invalid argument "+ arguments[i]);
  396. printHelp();
  397. Environment.Exit(3);
  398. }
  399. }
  400.  
  401. }
  402.  
  403. function download(){
  404. var client:System.Net.WebClient = new System.Net.WebClient();
  405.  
  406. if (user!=0 && password!=0){
  407. client.Credentials=new System.Net.NetworkCredential(user, password);
  408. }
  409.  
  410. if (proxy!=0){
  411. var webproxy =new System.Net.WebProxy();
  412. webproxy.Address=new Uri(proxy);
  413. if (proxy_user!=0 && proxy_pass!=0){
  414. webproxy.Credentials=new System.Net.NetworkCredential(proxy_user,proxy_pass);
  415. }
  416. webproxy.UseDefaultCredentials =false;
  417.  
  418. if (bypass!=0){
  419. webproxy.BypassList=bypass;
  420. webproxy.BypassProxyOnLocal = false;
  421. }
  422. client.Proxy=webproxy;
  423. }
  424.  
  425. try {
  426. client.DownloadFile(arguments[1], arguments[2]);
  427. } catch (e) {
  428. Console.BackgroundColor = ConsoleColor.Green;
  429. Console.ForegroundColor = ConsoleColor.Red;
  430. Console.WriteLine("nnProblem with downloading " + arguments[1] + " to " + arguments[2] + "Check if the internet address is valid");
  431. Console.ResetColor();
  432. Environment.Exit(5);
  433. }
  434. }
  435.  
  436. parseArgs();
  437. download();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement