此行提取连接到路由器第一的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命令行上,是否有更好的方法为此编写一些单行?
答案 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
$i
。 curl -s http://192.168.6.100:8081 ...
curl -s http://192.168.6.100:8082 ...
开始,依此类推,直到完成所有9个路由器接下来,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"
调用