內容目錄
curl 是 Linux 下一個很強大的 http 命令列工具
curl -I 的參數說明
-I/--head (HTTP/FTP/FILE) Fetch the HTTP-header only! HTTP-servers feature the command HEAD which this uses to get nothing but the header of a document. When used on a FTP or FILE file, curl displays the file size and last modification time only.
範例
curl -I http://example.com curl -I example.com curl -I bit.ly/Q8f9o # 縮短網址, 可以看到 HTTP/1.1 301 Moved.
root@tonyhack:~# curl -I tonyhack.asuscomm.com
HTTP/1.1 200 OK Server: nginx/1.1.19 Date: Mon, 16 Sep 2013 10:44:26 GMT Content-Type: text/html; charset=utf-8,gbk,big5 Content-Length: 763 Last-Modified: Wed, 20 Feb 2013 15:42:44 GMT Connection: keep-alive Accept-Ranges: bytes
取得網頁內容,螢幕輸出
$ curl http://www.linuxidc.com
取得網頁內容,檔案輸出
$ curl -o page.html http://www.linuxidc.com
指定 http 使用的 proxy
$ curl -x 123.45.67.89:1080 -o page.html http://www.linuxidc.com
把 http response 裡面的 cookie 資訊另存新檔
$ curl -x 123.45.67.89:1080 -o page.html -D cookie0001.txt http://www.linuxidc.com
把 cookie 資訊加到 http request 裡
$ curl -x 123.45.67.89:1080 -o page1.html -D cookie0002.txt -b cookie0001.txt http://www.linuxidc.com
設定瀏覽器資訊
#Windows 2000上的 IE6.0
$ curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -x 123.45.67.89:1080 -o page.html -D cookie0001.txt http://www.linuxidc.com
# Linux Netscape 4.73
$ curl -A "Mozilla/4.73 [en] (X11; U; Linux 2.2; 15 i686" -x 123.45.67.89:1080 -o page.html -D cookie0001.txt http://www.linuxidc.com
設定 referrer
另外一個伺服器端常用的限制方法,就是檢查 http 訪問的 referer。比如你先訪問首頁,再訪問裡面所指定的下載頁,這第二次訪問的 referer 位址就是第一次訪問成功後的頁面位址。這樣,伺服器端只要發現對下載頁面某次訪問的 referer 位址不是首頁的位址,就可以斷定那是個盜連了
$ curl -A "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" -x 123.45.67.89:1080 -e "mail.linuxidc.com" -o page.html -D cookie0001.txt http://www.linuxidc.com
使用伺服器上的檔案名,存在本地
$ curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen1.JPG
可使用 Regular Expression 抓取所有 match 的檔案,指定 match 的群組內容為新檔名
$ curl -O http://cgi2.tky.3web.ne.jp/~zzh/screen[1-10].JPG $ curl -o #2-#1.jpg http://cgi2.tky.3web.ne.jp/~{zzh,nick}/[001-201].JPG
原來: ~zzh/001.JPG -> 下載後:001-zzh.JPG
原來: ~nick/001.JPG -> 下載後:001-nick.JPG
續傳 (只能用在原本是 curl 傳輸的檔案)
$ curl -c -O http://cgi2.tky.3wb.ne.jp/~zzh/screen1.JPG
分塊下載
$ curl -r 0-10240 -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &\ $ curl -r 10241-20480 -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &\ $ curl -r 20481-40960 -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3 &\ $ curl -r 40961- -o "zhao.part1" http:/cgi2.tky.3web.ne.jp/~zzh/zhao1.mp3
Linux用 cat zhao.part* > zhao.mp3合併
Windows用copy /b 合併
指定 FTP 帳號密碼
$ curl -u name:passwd ftp://ip:port/path/file $ curl ftp://name:passwd@ip:port/path/file
上傳檔案
$ curl -T localfile -u name:passwd ftp://upload_site:port/path/ $ curl -T localfile http://cgi2.tky.3web.ne.jp/~zzh/abc.cgi
(注意這時候使用的協定是 HTTP 的 PUT method)
Http GET 與 POST 模式
GET 模式什麼 option 都不用,只需要把變數寫在 url 裡面就可以了比如:
$ curl http://www.linuxidc.com/login.cgi?user=nickwolfe&password=12345
而 POST 模式的 option 則是 -d
$ curl -d "user=nickwolfe&password=12345" http://www.linuxidc.com/login.cgi
到底該用 GET 模式還是 POST 模式,要看對面伺服器的程式設定。比如 POST 模式下的文件上傳
<form action="http://cgi2.tky.3web.ne.jp/~zzh/up_file.cgi" enctype="multipar/form-data" method="POST"> <input name="upload" type="file"/> <input name="nick" type="submit" value="go"/></form>
這樣一個 HTTP 表單,我們要用 curl 進行模擬,就該是這樣的語法:
$ curl -F upload=@localfile -F nick=go http://cgi2.tky.3web.ne.jp/~zzh/up_file.cgi
https 使用本地認證
$ curl -E localcert.pem https://remote_server
通過 dict 協定去查字典
$ curl dict://dict.org/d:computer
Hits: 398