Bash 双 while 循环中的反向逻辑
Bash reverse logic in a double while loop
我有这个名为 vs-hosts-objects.txt 的对象列表,源自 Cisco ASA 配置。该列表描述了主机名、它们的 ip 地址和它们的 NAT 地址。
示例:
object network net01 subnet 192.168.100.0 255.255.255.0
object network SRV002 host 172.16.0.2
object network SRV003 host 172.16.0.3
object network VPN-Pool range 172.17.100.200 172.17.100.230
object network SRV003 (internal,int-Internet) static 87.88.89.90
object network SRV012 host 172.16.0.12
object network SRV012 (internal,int-Internet) static 87.88.89.91
object network SRV013 host 172.16.0.13
有些附有 NAT 地址,有些则没有。我需要处理此列表以便能够将列表导入新的防火墙(新供应商)并记录此 IP 用于我们 IPAM 中的主机(因此此时没有子网或范围)。
为此,我需要这样的输出:
SRV003,172.16.0.3,87.88.89.90
SRV012,172.16.0.12,87.88.89.91
SRV013,172.16.0.13
我创建了这个片段,它用于获取具有 NAT 地址的主机:
while read -r line
do
hostobj=`echo $line | grep host | awk '{ print }'`
hostipobj=`echo $line | grep host | awk '{ print }'`
while read -r staticline
do
natobj=`echo $staticline | grep static | awk '{ print }'`
natipobj=`echo $staticline | grep static | awk '{ print }'`
if [ "$natobj" != "" ];
then
if [ "$hostobj" = "$natobj" ];
then
echo "$hostobj,$hostipobj,$natipobj"
fi
fi
done < vs-hosts-objects.txt
done < vs-hosts-objects.txt
但我可能一辈子都不知道如何获取列出的没有附加 NAT 地址的主机。
虽然我确信有更聪明、更好和更快的方法来做到这一点,但我并不反对 运行 另一个没有附加 NAT 的主机的片段。我为获得该结果所做的所有尝试均以失败告终。
希望得到帮助,祝您生活愉快!
问候
如果 host
行总是在 NAT 行之前,您可以像这样用 IP 填充关联数组:
#! /bin/bash
declare -A ips
while read -ra columns ; do
name=${columns[2]}
if [[ ${columns[3]} == host ]] ; then
ips[$name]=${columns[4]}
elif [[ ${columns[4]} == static ]] ; then
ips[$name]+=",${columns[5]}"
fi
done < vs-hosts-objects.txt
for name in "${!ips[@]}" ; do
echo "$name,${ips[$name]}"
done
如果没有,您可以使用两个单独的关联数组,一个对应一种类型。
我有这个名为 vs-hosts-objects.txt 的对象列表,源自 Cisco ASA 配置。该列表描述了主机名、它们的 ip 地址和它们的 NAT 地址。 示例:
object network net01 subnet 192.168.100.0 255.255.255.0
object network SRV002 host 172.16.0.2
object network SRV003 host 172.16.0.3
object network VPN-Pool range 172.17.100.200 172.17.100.230
object network SRV003 (internal,int-Internet) static 87.88.89.90
object network SRV012 host 172.16.0.12
object network SRV012 (internal,int-Internet) static 87.88.89.91
object network SRV013 host 172.16.0.13
有些附有 NAT 地址,有些则没有。我需要处理此列表以便能够将列表导入新的防火墙(新供应商)并记录此 IP 用于我们 IPAM 中的主机(因此此时没有子网或范围)。 为此,我需要这样的输出:
SRV003,172.16.0.3,87.88.89.90
SRV012,172.16.0.12,87.88.89.91
SRV013,172.16.0.13
我创建了这个片段,它用于获取具有 NAT 地址的主机:
while read -r line
do
hostobj=`echo $line | grep host | awk '{ print }'`
hostipobj=`echo $line | grep host | awk '{ print }'`
while read -r staticline
do
natobj=`echo $staticline | grep static | awk '{ print }'`
natipobj=`echo $staticline | grep static | awk '{ print }'`
if [ "$natobj" != "" ];
then
if [ "$hostobj" = "$natobj" ];
then
echo "$hostobj,$hostipobj,$natipobj"
fi
fi
done < vs-hosts-objects.txt
done < vs-hosts-objects.txt
但我可能一辈子都不知道如何获取列出的没有附加 NAT 地址的主机。 虽然我确信有更聪明、更好和更快的方法来做到这一点,但我并不反对 运行 另一个没有附加 NAT 的主机的片段。我为获得该结果所做的所有尝试均以失败告终。 希望得到帮助,祝您生活愉快! 问候
如果 host
行总是在 NAT 行之前,您可以像这样用 IP 填充关联数组:
#! /bin/bash
declare -A ips
while read -ra columns ; do
name=${columns[2]}
if [[ ${columns[3]} == host ]] ; then
ips[$name]=${columns[4]}
elif [[ ${columns[4]} == static ]] ; then
ips[$name]+=",${columns[5]}"
fi
done < vs-hosts-objects.txt
for name in "${!ips[@]}" ; do
echo "$name,${ips[$name]}"
done
如果没有,您可以使用两个单独的关联数组,一个对应一种类型。