有人知道如何在bash中计算多少个ip吗?
例如: 命令:
root@ubuntu:~$ dig www.google.com A +short | grep -oE "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"
Run Code Online (Sandbox Code Playgroud)
例如我得到
114.114.114.114
114.114.115.115
8.8.8.8
etc...
Run Code Online (Sandbox Code Playgroud)
我想运行一个特定的命令来获取:
N IPs found in DNS
Run Code Online (Sandbox Code Playgroud)
(N 是在返回中找到的 IP 数)
我还想将这些 IP 划分为不同的变量:
$a="114.114.114.114"
$b="115.115.115.115"
$c="8.8.8.8"
$N="x.x.x.x"
Run Code Online (Sandbox Code Playgroud)
有没有人知道怎么做?
您可以使用数组来获取结果并使用数组元素的数量来显示该N IPs found in DNS行。也可以遍历数组或使用数组中的特定元素:
#!/bin/bash
myarray=( $(dig www.google.com A +short | grep -oE "\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b" ) )
echo "${#myarray[@]} IPs found in DNS"
for IP in ${myarray[@]}
do
echo IP: $IP
done
echo "The third entry found in DNS is: ${myarray[2]}"
Run Code Online (Sandbox Code Playgroud)