总览
- ping host 检查网络层是否可达
- telnet host port 检查传输层是否可达,即目标端口是否打开
- curl -v host[:port] 检查应用层服务是否正常
- wget --spider host[:port] 检查应用层服务是否正常
telnet
成功的telnet
telnet samba.example.com 445
Trying 172.31.25.31...
Connected to samba.example.com.
Escape character is '^]'.
有时,Connected to TargetHost后,会在稍后自动断开,提示信息为
Connection closed by foreign host.
这说明Connected后,由于目标主机的安全策略等原因,Connection被目标主机断掉
curl
The command-line parser in curl always parses the entire line and you can put the options anywhere you like; they can also appear after the URL:
curl -vL http://example.com
curl http://example.com -Lv
curl http://example.com -L -v
-
常用的curl option
-v, --verbose 显示更丰富的信息,如请求头、请求体、响应头、响应体
-
-L, --location 用于自动重定向
(HTTP) If the server reports that the requested page has moved to a different location (indicated with a Location: header and a 3XX
response code), this option will make curl redo the request on the new place. If used together with -i, --include or -I, --head, headers from all requested pages will be shown. When authentication is used, curl only sends its credentials to the initial host. If a redirect takes curl to a different host, it won't be able to intercept the user+password(curl发现重定向还是会带账户密码过去). See also --location-trusted on how to change this. -k, --insecure 无需校验服务器证书
(TLS) By default, every SSL connection curl makes is verified to be secure. This option allows curl to proceed and operate even for server connections otherwise considered insecure.-
-d, --data < data > 设置post请求体
(HTTP) Sends the specified data in a POST request to the HTTP server, in the same way that a browser does when a user has filled in an HTML form and presses the submit button. This will cause curl to pass the data to the server using the content-type application/x-www-form-urlencoded.Example:
curl -d '{"id": 21868, "name":"testname"}' http://example.com/
-
-H, --header < header/@file > 设置请求头参数
This option can be used multiple times to add/replace/remove multiple headers.
Example:
curl -H "X-First-Name: Joe" -H "X-Last-Name: James" http://example.com/
此外,curl还可以用来分析http时延
curl命令支持以下阶段的时间统计:
time_namelookup : 从请求开始到DNS解析完成的耗时
time_connect : 从请求开始到TCP三次握手完成耗时
time_appconnect : 从请求开始到TLS握手完成的耗时
time_pretransfer : 从请求开始到向服务器发送第一个GET请求开始之前的耗时
time_redirect : 重定向时间,包括到内容传输前的重定向的DNS解析、TCP连接、内容传输等时间
time_starttransfer : 从请求开始到server端开始传输response的时间
time_total : 从请求开始到完成的总耗时
使用curl命令分析http时延分为2步:
- 1.
touch curl_format.txt
作为模版文件,并写入如下内容
time_namelookup: %{time_namelookup}\n
time_connect: %{time_connect}\n
time_appconnect: %{time_appconnect}\n
time_redirect: %{time_redirect}\n
time_pretransfer: %{time_pretransfer}\n
time_starttransfer: %{time_starttransfer}\n
----------\n
time_total: %{time_total}\n
- 2.`curl -w "@curl_format.txt" -o /dev/null -s http://example.com/
What this does:
-w "@curl-format.txt" tells cURL to use our format file
-o /dev/null redirects the output of the request to /dev/null
-s tells cURL not to show a progress meter
也可以不使用模版文件,直接在curl命令里键入输入模版,如:
curl -w '\ntime_namelookup=%{time_namelookup}\ntime_connect=%{time_connect}\ntime_appconnect=%{time_appconnect}\ntime_redirect=%{time_redirect}\ntime_pretransfer=%{time_pretransfer}\ntime_starttransfer=%{time_starttransfer}\ntime_total=%{time_total}\n\n' -o /dev/null -s -L https://www.nixops.me/
可直接执行看效果
curl使用二进制的payload
curl --request POST --data-binary @README http://localhost:6666
# 使用@符号可以从文件中读取数据作为参数
eg.
curl 'http://9.134.166.231:8080/webapi/v1/ExpandOrder/Create' \
-X 'POST' \
-H 'Content-Type: application/json;charset=utf-8' \
-H 'Accept: application/json, text/plain, */*' \
-H 'Accept-Encoding: gzip, deflate' \
-H 'Accept-Language: zh-cn' \
-H 'Host: 9.134.166.231:8080' \
-H 'Origin: http://9.134.166.231:8080' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15' \
-H 'Referer: http://9.134.166.231:8080/' \
-H 'Content-Length: 463' \
-H 'Connection: keep-alive' \
-H 'Cookie: x_host_key=17f209a8ab4-c5e8d903d5f8a6a810b5c51dcc9670d854d27d2f' \
--data-binary $'{"expand_plan_id":4,"plan_type_name":"\u4e1a\u52a1\u81ea\u8eab","product_id":132,"module_path":"TKE\u4e1a\u52a1\u8fd0\u8425-\u4e1a\u52a1\u8fd0\u8425-[\u6d4b\u8bd5][\u6d4b\u8bd5]","if_partition":1,"number":1,"device_class":"c1","wan_rate":"","lan_rate":"\u5343\u5146","os":"os","inner_ip_num":1,"outer_ip_num":0,"logic_domain":"logic","switch_dt":0,"rack_dt":0,"city":"\u6df1\u5733","campus":"\u6df1\u5733-\u576a\u5c71","expand_reason":"t s t","expand_detail_explain":"test","admin":"xiuxianwen","creator":null,"bak_admin":"xiuxianwen”}'
ping不通但是可以curl通,怎么理解
ping和curl是不同的协议。ping是ICMP协议,不是tcp/udp socket。而curl是走的tcp/ip协议。服务器或防火墙可以通过拒绝ICMP协议来实现拒绝ping,不能ping通不代表你的socket不能访问,它们是不同的协议