linux下curl的用法
- 2019 年 10 月 4 日
- 筆記
參考:http://www.aiezu.com/system/linux/linux_curl_syntax.html
curl [options] [URL…]
curl的常用選項:
--user-agent <string> 設置用戶代理髮送給服務器 -basic 使用HTTP基本認證 --tcp-nodelay 使用TCP_NODELAY選項 --referer <URL> 來源網址 --cacert <file> CA證書 (SSL) --compressed 要求返回是壓縮的格式 --header <line>自定義頭信息傳遞給服務器 -I 只顯示響應報文首部信息 --limit-rate <rate> 設置傳輸速度 -u/--user <user[:password]>設置服務器的用戶和密碼 -0/--http1.0 使用HTTP 1.0 -x 接代理服務器的IP 表示使用這個代理IP去請求其他的網頁 -s 靜默模式,不顯示返回的一大堆頁面內容 -d 帶參數請求。這樣就可以post用戶名和密碼之類的參數了,模擬登錄非常有用。 -c 接文件名,表示將curl時候的服務器返回的cookie存到本地文件中 -b 接cookie文件路徑, 表示請求的時候將cookie文件中的信息帶上 -L 表示如果在response header中如果有location的話就直接轉向到location的地址(redirect地址) (HTTP/HTTPS)追隨http響應頭「Location:」定向到跳轉後的頁面,(在http響應碼為3XX時使用,如301跳轉、302跳轉) 用法:curl [options] [URL...] 例:curl -e www.baidu.com/index.html 192.168.2.11/index.html get方式提交數據: curl -G -d "name=value&name2=value2" http://www.baidu.com post方式提交數據: curl -d "name=value&name2=value2" http://www.baidu.com #post數據 curl -d a=b&c=d&txt@/tmp/txt http://www.baidu.com #post文件 表單方式提交數據: curl -F file=@/tmp/me.txt http://www.aiezu.com #將http header保存到文件 curl -D /tmp/header http://www.aiezu.com # 查看是否跳轉正常 curl -I "http://pc.demo.com/" -L 例如:負載均衡中檢測服務器存活命令 curl -I -s http://172.16.20.13|head -1|awk -F " " '{print $2}' 檢測網址訪問情況: curl -I -s 'http://api.demo.com/User/Register'|sed -n '1p'|grep '200 OK' # post提交參數檢測是否可以網站登錄 if [ ! `curl -s -d 'UserName=xxxxxx&&UserPass=xxxxxx' http://www.demo.com/User/Login |wc -l` -eq 0 ] ; then echo -e "賬號出現無法登陸,請關注" fi