場景
需要批次傳送curl請求,並且請求引數是動態變化的~
實現
shell指令碼
建立一個sh檔案,如:batch_curl.sh,指令碼內容:
#!/bin/bash token="my token" url="https://ip:port/client/service-ids/agent/v1/report-conns" if [ -z "$1" ]; then echo "Usage: $0 <number_of_requests>" exit 1 fi echo "Start send requests~" total_requests=$1 for ((i=1; i<=$total_requests; i++)) do curl -d '{ "src_ip": "192.168.102.'$i'", "src_port": 100, "dst_ip": "10.0.3.15", "dst_port": '$i', }' -X POST -k -H "Content-Type: application/json" -H "Authorization: Bearer $token" "$url" # 每100個請求列印一次日誌 if [ $((i % 100)) -eq 0 ] || [ $i -eq $total_requests ]; then echo "Sent $i requests. $(date)" fi done echo "Finished sending $total_requests requests."
實現說明:
- 傳送請求,需要一個有效的token,伺服器的ip:port
執行命令
bash ./batch_curl.sh 1000