How to execute the same curl request on a sequence of server IPs (URLs)

霸气de小男生 提交于 2019-12-11 15:57:29

问题


Command

curl -v -H "Accept: application/json" -H "Content-type: application/json" \
        -X POST -d '{"test": "some data"}' http://XX.XX.X.001:8080/services/test

I want to execute the above same curl command on different servers (IP addresses). Is there any way or CURL command so I can execute this service on all servers, instead of changing the IP address every time manually?

I have servers IP in some sequence, e.g.

http://XX.XX.X.002:8080/services/test
http://XX.XX.X.003:8080/services/test
...

回答1:


You can either use shell's brace expansion in bash, e.g. {2..15} for 2,3,4,...,15:

curl ... http://x.x.x.{2..15}:8080/services/test

or, curl's alphanumeric series [] operator in the URL part (notice the double-quotes):

curl ... "http://x.x.x.[2-15]:8080/services/test"

Alternatively, you can use arithmetic for loop:

for ((i=2; i<=15; i++)); do
    curl ... "http://x.x.x.$i:8080/services/test"
done



回答2:


You could use a loop:

for HOST in 0.0.0.1:8080 0.0.0.2:8080; do curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"my": "data"}' http://$HOST/services/test ; done

If your hosts are sequential then randomir's answer is a lot cleaner.



来源:https://stackoverflow.com/questions/47075974/how-to-execute-the-same-curl-request-on-a-sequence-of-server-ips-urls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!