ping 几次并获得每个 ip 的 return 状态
ping several times and get return status for each ip
我正在尝试同时 ping 192.168.2-254 上的所有 IP,并获取每个 IP 的 return 状态。这是我到目前为止所拥有的,但它不起作用,而只是 returns xargs 的状态。任何帮助表示赞赏!我不想使用 nmap...
subnet="192.168.1"
num="2"
while [ "$num" -lt "254" ]; do
num=$((num+1))
printf "${subnet}.${num}\n"
done | xargs -n 1 -I ^ -P 50 ping -c2 -t3
@M Horton 这对你有用吗?
在 xargs 字段中使用 xargs -n1 -P50 ping -c2
xargs
的公认和记录的行为是 return 它自己的退出代码,它指示子进程的 所有 发生了什么你开始了。也就是说;
xargs
exits with the following status:
0 - if it succeeds
123 - if any invocation of the command exited with status 1-125
124 - if the command exited with status 255
125 - if the command is killed by a signal
126 - if the command cannot be run
127 - if the command is not found
1 - if some other error occurred.
(来源:GNU xargs
man page. The POSIX spec 稍微不那么具体,只是将所有 1-125 归为“无法组装满足指定要求的命令行,实用程序的一个或多个调用 return编辑了一个非零退出状态,或者发生了一些其他错误。")
没有简单的方法可以在单个退出代码中传达多个进程的状态(回想一下值是单个字节,所以即使您将 return 值编码为位字段,表明只有成功或失败,你仍然只能将其中的八个塞进一个值)但是如果我正确理解你的请求,那么循环中的 运行 ping
无论如何你都是 运行ning,并且wait
分别为每一个。以下使用 Bash 数组来跟踪各个进程:
declare -a procs
for((num=2; num<254; num++)); do
ping -c2 -t3 "192.168.1.${num}" &
procs+=($!)
done
for p in "${procs[@]}"; do
wait $p; echo $?
done
来自 200 多个 ping
并行处理 运行 的输出将相当嘈杂;也许在第一个 done
之后添加 >/dev/null
。 (将所有内容重定向一次比分别重定向每个 ping
更有效。)
这还不能跟踪哪个进程 ID 属于哪个 IP 地址;如果需要,可以使用关联数组(Bash 4+)或将 IP 地址放入第二个数组并保持对齐,以便 ${ip[x]}
是属于进程 [=22= 的 IP 地址](例如 MacOS 仍然附带 Bash 3.2.57(1)-不支持关联数组的版本)。
这是使用关联数组的重构。
declare -A procs
for((num=2; num<254; num++)); do
ping -c2 -t3 "192.168.1.${num}" &
procs[$num]=$!
done >/dev/null
for p in "${!procs[@]}"; do
wait ${procs[$p]}
printf "%s:%i\n" "192.169.1.$p" $?
done
我正在尝试同时 ping 192.168.2-254 上的所有 IP,并获取每个 IP 的 return 状态。这是我到目前为止所拥有的,但它不起作用,而只是 returns xargs 的状态。任何帮助表示赞赏!我不想使用 nmap...
subnet="192.168.1"
num="2"
while [ "$num" -lt "254" ]; do
num=$((num+1))
printf "${subnet}.${num}\n"
done | xargs -n 1 -I ^ -P 50 ping -c2 -t3
@M Horton 这对你有用吗?
在 xargs 字段中使用 xargs -n1 -P50 ping -c2
xargs
的公认和记录的行为是 return 它自己的退出代码,它指示子进程的 所有 发生了什么你开始了。也就是说;
xargs
exits with the following status:0 - if it succeeds
123 - if any invocation of the command exited with status 1-125
124 - if the command exited with status 255
125 - if the command is killed by a signal
126 - if the command cannot be run
127 - if the command is not found
1 - if some other error occurred.
(来源:GNU xargs
man page. The POSIX spec 稍微不那么具体,只是将所有 1-125 归为“无法组装满足指定要求的命令行,实用程序的一个或多个调用 return编辑了一个非零退出状态,或者发生了一些其他错误。")
没有简单的方法可以在单个退出代码中传达多个进程的状态(回想一下值是单个字节,所以即使您将 return 值编码为位字段,表明只有成功或失败,你仍然只能将其中的八个塞进一个值)但是如果我正确理解你的请求,那么循环中的 运行 ping
无论如何你都是 运行ning,并且wait
分别为每一个。以下使用 Bash 数组来跟踪各个进程:
declare -a procs
for((num=2; num<254; num++)); do
ping -c2 -t3 "192.168.1.${num}" &
procs+=($!)
done
for p in "${procs[@]}"; do
wait $p; echo $?
done
来自 200 多个 ping
并行处理 运行 的输出将相当嘈杂;也许在第一个 done
之后添加 >/dev/null
。 (将所有内容重定向一次比分别重定向每个 ping
更有效。)
这还不能跟踪哪个进程 ID 属于哪个 IP 地址;如果需要,可以使用关联数组(Bash 4+)或将 IP 地址放入第二个数组并保持对齐,以便 ${ip[x]}
是属于进程 [=22= 的 IP 地址](例如 MacOS 仍然附带 Bash 3.2.57(1)-不支持关联数组的版本)。
这是使用关联数组的重构。
declare -A procs
for((num=2; num<254; num++)); do
ping -c2 -t3 "192.168.1.${num}" &
procs[$num]=$!
done >/dev/null
for p in "${!procs[@]}"; do
wait ${procs[$p]}
printf "%s:%i\n" "192.169.1.$p" $?
done