Linux shell one-liner用于编写多个CURL下载脚本

时间:2015-11-28 00:41:30

标签: curl command-line

此行提取连接到路由器第一的WiFi客户端数量(请注意8081端口):

luis@balanceador:~$ curl -s http://192.168.6.100:8081 | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c
7

这是针对2号路由器(请注意8082端口):

luis@balanceador:~$ curl -s http://192.168.6.100:8082 | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c
12

和数字n:

curl -s http://192.168.6.100:808n | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c

1和2的总和:

luis@balanceador:~$ echo $(( $(curl -s http://192.168.6.100:8081 | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c)+$(curl -s http://192.168.6.100:8082 | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c)  ))
19

所以,我想了解所有9台路由器的WiFi客户端数量:

echo $(( Router1+Router2+...+RouterN ))

echo $(( $(curl -s http://192.168.6.100:8081 | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c)+$(curl -s http://192.168.6.100:8082 | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c)+$(curl -s http://192.168.6.100:8083 | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c)+$(curl -s http://192.168.6.100:8084 | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c)+$(curl -s http://192.168.6.100:8085 | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c)+$(curl -s http://192.168.6.100:8086 | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c)+$(curl -s http://192.168.6.100:8087 | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c)+$(curl -s http://192.168.6.100:8088 | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c)+$(curl -s http://192.168.6.100:8089 | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c) ))

在Linux命令行上,是否有更好的方法为此编写一些单行

2 个答案:

答案 0 :(得分:2)

试试这个:

for (( i = 1; i <= 9; i=i+1 )); do curl -s http://192.168.6.100:808$i | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c ; done | awk '{s+=$1} END {print s}'

以下是它的工作原理:

对于

我们首先使用您的and for number n:示例:

curl -s http://192.168.6.100:808n | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c

并将其修改为在C风格的for循环中工作。 C风格的for循环语法如下所示,例如,如果您想要回显数字2 9次,您可以这样做:

for (( i = 1; i <= 9; i=i+1 )); do echo 2 ; done

因此,要将您的示例集成到for循环体内(替换上面的echo 2部分):

curl -s http://192.168.6.100:808$i | grep "eth1" -i | grep "setWirelessTable" -i | grep "eth1" -io | grep "eth1" -c
  • bash将使用for循环中的迭代编号替换$i
  • 因此第一次迭代命令将以:curl -s http://192.168.6.100:8081 ...
  • 开头
  • 迭代2,命令将从:curl -s http://192.168.6.100:8082 ...开始,依此类推,直到完成所有9个路由器

awk中

接下来,awk程序可用于计算一列数字的总和,例如我们可以在单独的行上模拟1和3的输出,传递给awk:

echo -e '1\n3' | awk '{s+=$1} END {print s}'

结果:4

  • $1是多行输入awk收到的第一个“字段”
  • s+=将值添加到我们称之为s的变量中。您也可以将其称为其他内容,例如mysum但我更喜欢s来保持简短
  • awk通过每一行执行此操作
  • print s输出保存总和的s的最终值

把它放在一起

... done | awk ...只是管道for-loop中的多行输出,并将其传递给awk程序,以便awk程序可以计算出你想要的最终总和。 / p>

答案 1 :(得分:1)

不是单行,而是某种结构:

count_clients () {
    curl -s http://192.168.6.100:$1 | grep "setWirelessTable" -i | grep "eth1" -c
}
sum=0
for n in {8001..8009}; do
    (( sum += $(count_clients $n) ))
done
echo $sum

您的管道中似乎有很多冗余的grep "eth1"调用