简体   繁体   English

bash 脚本 - 循环中的 2 个变量

[英]bash script - 2 variables in loop

I have a file ip_details.txt with following;我有一个文件 ip_details.txt,其中包含以下内容;

Peer "ATBBB010.domain.com:1111;transport=tcp" ConnectAddress="10.184.88.29,10.184.88.30" LocalPort="0"
Peer "ATBBB020.domain.com:1111;transport=tcp" ConnectAddress="10.184.88.61,10.184.88.62" LocalPort="0"
Peer "CHBBB010.domain.com:1111;transport=tcp" ConnectAddress="10.161.144.5,10.161.144.6" LocalPort="0"
Peer "CHBBB020.domain.com:1111;transport=tcp" ConnectAddress="10.161.144.21,10.161.144.22" LocalPort="0"
Peer "DABBB010.domain.com:1111;transport=tcp" ConnectAddress="10.160.130.5,10.160.130.6" LocalPort="0"
Peer "DABBB020.domain.com:1111;transport=tcp" ConnectAddress="10.160.130.21,10.160.130.22" LocalPort="0"
Peer "ATCCC010.domain.com:1111;transport=tcp" ConnectAddress="10.199.88.29,10.199.88.30" LocalPort="0"
Peer "ATCCC020.domain.com:1111;transport=tcp" ConnectAddress="10.199.88.61,10.199.88.62" LocalPort="0"
Peer "CHCCC010.domain.com:1111;transport=tcp" ConnectAddress="10.161.155.5,10.161.155.6" LocalPort="0"
Peer "CHCCC020.domain.com:1111;transport=tcp" ConnectAddress="10.161.155.21,10.161.155.22" LocalPort="0"
Peer "DACCC010.domain.com:1111;transport=tcp" ConnectAddress="10.199.130.5,10.199.130.6" LocalPort="0"
Peer "DACCC020.domain.com:1111;transport=tcp" ConnectAddress="10.199.130.21,10.199.130.22" LocalPort="0"

I currently use these 2 scripts/commands to ping the IP Addresses of the specific type nodes.我目前使用这 2 个脚本/命令来 ping 特定类型节点的 IP 地址。

To Ping the BBB nodes first IP (IP before the comma delimiter); ping BBB节点的第一个IP(逗号分隔符前的IP);

for i in `cat ip_details.txt | grep BBB | awk '{print $3}' | cut -d= -f2 | cut -d, -f1 | tr -d '[="=]'`; do ping -I eth0 -c 3 $i >> /dev/null ; [ $? != 0 ] && echo $i || echo "Ping to $i Good"; done

To Ping the BBB nodes 2nd IP (IP after the comma delimiter); Ping BBB 节点的第二个 IP(逗号分隔符后的 IP);

for i in `cat ip_details.txt | grep BBB | awk '{print $3}' | cut -d= -f2 | cut -d, -f2 | tr -d '[="=]'`; do ping -I eth0 -c 3 $i >> /dev/null ; [ $? != 0 ] && echo $i || echo "Ping to $i Good"; done

I get the as the following;我得到如下;

Ping to 10.184.88.29 Good
Ping to 10.184.88.61 Good
.
.

Similarly I grep CCC for CCC Nodes.同样,我为 CCC 节点 grep CCC。

I need a script to ping these IP Addresses per nodes types or both types together and get output like this;我需要一个脚本来按节点类型或两种类型一起 ping 这些 IP 地址,并获得这样的输出;

Ping from <hostname> to ATBBB010 IP1 <ip address> is Good
....
....
....

Ping from <hostname> to ATBBB010 IP2 <ip address> is Good 
....
....
....

If the ping fails;如果ping不通;

Ping from <hostname> to ATBBB010 IP1 <ip address> failed
.....
.....

Ping from <hostname> to ATBBB010 IP2 <ip address> failed
.....
.....

Try the following:请尝试以下操作:

#!/usr/bin/env bash

msgPrefix="Ping from $HOSTNAME to"
prevNode=
while read node ip; do
  [[ "$node" == "$prevNode" ]] || i=1
  ping -I eth0 -c 3 "$ip" >/dev/null && status='is good' || status='failed'
  echo "$msgPrefix $node IP$i $ip $status"
  ((++i)); prevNode=$node
done < <(awk -F'"|\\.domain\\.com|,' '{ print $2, $5 }' ip_details.txt)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM