使用 linux bash 同时向多个主机发送 http-get 请求
Send http-get request to many hosts at same time with linux bash
是否可以使用 linux bash 工具同时向多台主机发送 http-get 请求?
目前我在做
wget -O- http://192.168.1.20/get_data-php > out.log
但我需要请求所有 192.168.1.0/17
个 IP。
#!/bin/sh
rm address.txt allout.txt # remove old file with addresses and contents
nmap -n -sn 192.168.1.0/17 -oG - | awk '/Up$/{print }' > address.txt # get all active hosts and store into a file address.txt
while IFS="" read -r add || [ -n "$add" ]
do
wget -q -O- http://"$add"/get_data-php > out"$add".log & # for every address create file with wget content
done < address.txt
wait
cat out*.log > allout.txt # put all .log file contents to allout.txt
rm -r out*.log # remove all created .log files
最简单的方法是使用 bash 大括号扩展:
wget -O- http://192.168.{0..127}.{1..254}/get_data-php >>out.log
...如果性能不是问题(因为它将 运行 按顺序请求)。
当然有办法运行并行请求,但我想这超出了这个问题的范围。
基于 Drejc 的回答,但避免弄乱临时文件并更好地处理低于主机数量的进程限制(例如,如果您有 1000 台主机)。
#!/bin/sh
nmap -T5 -n -sn 192.168.1.0/17 -oG - |
awk '/Up$/{print }' |
parallel -j0 wget -q -O- http://{}/get_data-php > allout.txt
是否可以使用 linux bash 工具同时向多台主机发送 http-get 请求?
目前我在做
wget -O- http://192.168.1.20/get_data-php > out.log
但我需要请求所有 192.168.1.0/17
个 IP。
#!/bin/sh
rm address.txt allout.txt # remove old file with addresses and contents
nmap -n -sn 192.168.1.0/17 -oG - | awk '/Up$/{print }' > address.txt # get all active hosts and store into a file address.txt
while IFS="" read -r add || [ -n "$add" ]
do
wget -q -O- http://"$add"/get_data-php > out"$add".log & # for every address create file with wget content
done < address.txt
wait
cat out*.log > allout.txt # put all .log file contents to allout.txt
rm -r out*.log # remove all created .log files
最简单的方法是使用 bash 大括号扩展:
wget -O- http://192.168.{0..127}.{1..254}/get_data-php >>out.log
...如果性能不是问题(因为它将 运行 按顺序请求)。
当然有办法运行并行请求,但我想这超出了这个问题的范围。
基于 Drejc 的回答,但避免弄乱临时文件并更好地处理低于主机数量的进程限制(例如,如果您有 1000 台主机)。
#!/bin/sh
nmap -T5 -n -sn 192.168.1.0/17 -oG - |
awk '/Up$/{print }' |
parallel -j0 wget -q -O- http://{}/get_data-php > allout.txt