shell脚本:批量发送curl请求

随笔3个月前发布 三千橘猫
28 0 0

场景

需要批量发送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

 

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...