我无法从我的索引数组中 Ping IP,但可以使用关联数组
I am not able to Ping IPs from my indexed array but works with an associative array
我正在尝试将从 csv 读取的 IP 迭代到数组,作为一种监视解决方案。我在索引数组中有 ips,想将 ips 传递给 ping 命令,但它不起作用。
#!/bin/bash
datei=hosts.csv
length=$(cat $datei | wc -l)
for (( i=1; i<=$length; i++ ))
do
ips[$i]=$(cut -d ';' -f2 $datei | awk 'NR=='$i'')
hosts[$i]=$(cut -d ';' -f1 $datei | awk 'NR=='$i'')
done
servers=( "1.1.1.1" "8.8.4.4" "8.8.8.8" "4.4.4.4")
for i in ${ips[@]} #Here i change the array i want to iterate
do
echo $i
ping -c 1 $i > /dev/null
if [ $? -ne 0 ]; then
echo "Server down"
else
echo "Server alive"
fi
done
有趣的是,如果我迭代服务器数组而不是 ips 数组,它就可以工作。如果打印的话,ips 数组从数据来看似乎很好。
如果我使用服务器数组,则输出:
1.1.1.1
Server alive
8.8.4.4
Server alive
8.8.8.8
Server alive
4.4.4.4
Server down
如果我使用 ips 数组
1.1.1.1
: Name or service not known
Server down
8.8.4.4
: Name or service not known
Server down
8.8.8.8
: Name or service not known
Server down
4.4.4.4
: Name or service not known
Server down
猫的输出hosts.csv
test;1.1.1.1
test;8.8.4.4
test;8.8.8.8
test;4.4.4.4
第一列用于主机名,第二列用于 v4 中的 IP。
我在 Ubuntu 20.4.1 LTS
修复了您的代码的多个问题:
- 将
hosts.csv
读入数组。
- 正在测试 ping 结果。
hosts.csv
:
one.one.one.one;1.1.1.1
dns.google;8.8.4.4
dns.google;8.8.8.8
invalid.invalid;4.4.4.4
在代码中评论工作:
#!/usr/bin/env bash
datei=hosts.csv
# Initialize empty arrays
hosts=()
ips=()
# Iterate reading host and ip in line records using ; as field delimiter
while IFS=\; read -r host ip _; do
hosts+=("$host") # Add to the hosts array
ips+=("$ip") # Add to the ips array
done <"$datei" # From this file
# Iterate the index of the ips array
for i in "${!ips[@]}"; do
# Display currently processed host and ip
printf 'Pinging host %s with IP %s\n' "${hosts[i]}" "${ips[i]}"
# Test the result of pinging the IP address
if ping -nc 1 "${ips[i]}" >/dev/null 2>&1; then
echo "Server alive"
else
echo "Server down"
fi
done
您可以使用 readarray 从 stdin 读取数组,然后将其与 awk 结合使用以解析 Host.csv 文件:
readarray -t ips <<< "$(awk -F\; '{ print }' $date1)"
readarray -t hosts <<< "$(awk -F\; '{ print }' $date1)"
我正在尝试将从 csv 读取的 IP 迭代到数组,作为一种监视解决方案。我在索引数组中有 ips,想将 ips 传递给 ping 命令,但它不起作用。
#!/bin/bash
datei=hosts.csv
length=$(cat $datei | wc -l)
for (( i=1; i<=$length; i++ ))
do
ips[$i]=$(cut -d ';' -f2 $datei | awk 'NR=='$i'')
hosts[$i]=$(cut -d ';' -f1 $datei | awk 'NR=='$i'')
done
servers=( "1.1.1.1" "8.8.4.4" "8.8.8.8" "4.4.4.4")
for i in ${ips[@]} #Here i change the array i want to iterate
do
echo $i
ping -c 1 $i > /dev/null
if [ $? -ne 0 ]; then
echo "Server down"
else
echo "Server alive"
fi
done
有趣的是,如果我迭代服务器数组而不是 ips 数组,它就可以工作。如果打印的话,ips 数组从数据来看似乎很好。
如果我使用服务器数组,则输出:
1.1.1.1
Server alive
8.8.4.4
Server alive
8.8.8.8
Server alive
4.4.4.4
Server down
如果我使用 ips 数组
1.1.1.1
: Name or service not known
Server down
8.8.4.4
: Name or service not known
Server down
8.8.8.8
: Name or service not known
Server down
4.4.4.4
: Name or service not known
Server down
猫的输出hosts.csv
test;1.1.1.1
test;8.8.4.4
test;8.8.8.8
test;4.4.4.4
第一列用于主机名,第二列用于 v4 中的 IP。
我在 Ubuntu 20.4.1 LTS
修复了您的代码的多个问题:
- 将
hosts.csv
读入数组。 - 正在测试 ping 结果。
hosts.csv
:
one.one.one.one;1.1.1.1
dns.google;8.8.4.4
dns.google;8.8.8.8
invalid.invalid;4.4.4.4
在代码中评论工作:
#!/usr/bin/env bash
datei=hosts.csv
# Initialize empty arrays
hosts=()
ips=()
# Iterate reading host and ip in line records using ; as field delimiter
while IFS=\; read -r host ip _; do
hosts+=("$host") # Add to the hosts array
ips+=("$ip") # Add to the ips array
done <"$datei" # From this file
# Iterate the index of the ips array
for i in "${!ips[@]}"; do
# Display currently processed host and ip
printf 'Pinging host %s with IP %s\n' "${hosts[i]}" "${ips[i]}"
# Test the result of pinging the IP address
if ping -nc 1 "${ips[i]}" >/dev/null 2>&1; then
echo "Server alive"
else
echo "Server down"
fi
done
您可以使用 readarray 从 stdin 读取数组,然后将其与 awk 结合使用以解析 Host.csv 文件:
readarray -t ips <<< "$(awk -F\; '{ print }' $date1)"
readarray -t hosts <<< "$(awk -F\; '{ print }' $date1)"