> For the complete documentation index, see [llms.txt](https://kawsing.gitbook.io/opensystem/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kawsing.gitbook.io/opensystem/andoid-shou-ji/untitled-4/shell-script/shi-yong-curl-ce-shi-lian-jie-mou-wang-zhan-de-su-du.md).

# 使用curl測試連接某網站的速度

curl指令本身有測速功能,搭配shell可以作為測速使用

```
curl -o /dev/null -s -w "%{time_namelookup},%{time_connect}, \
%{time_starttransfer},%{time_total}" \
https://tw.yahoo.com && echo

#------------產生數字--------------------
0.004188,0.060702, 1.931382,8.039616
```

0.004188 => DNS解析速度

0.060702 =>Client -> Sever(TCP request)

1.931382 => Server->Client(TCP Response)

8.039616 => 連線總共花費時間

了解規則後，就可以寫一個簡單的連線某網站的測速程式工具

### $1表示第一個參數，使用到條件式 if.... else.... fi

vi curlweb-speed.sh

```bash
#!/bin/bash
if [ $1 ]; then
 url=$1
else
 echo "請輸入url(exp:tw.yahoo.com)"
 read url 
fi
curl -o /dev/null -s -w \
"DNS 解析: %{time_namelookup}\n\
Client Request: %{time_connect}\n\
Server Respon: %{time_starttransfer}\n\
總花費時間: %{time_total}\n" \
"${url}"
```

![](/files/-LwX3TlYocRx0KkxyqIK)
