获取迭代的最后一行 - UNIX
我创建了一个简单的脚本,为配置的 IP 运行 nslookup。它将运行 10 次(根据要求)来验证它是否指向活动 IP,然后在返回非活动状态时通过电子邮件发送警报。
脚本工作正常,但是,当它发出警报时,由于循环,它会执行 10 倍。如果最后一行不满足条件,如何让每个 IP 只发送一封电子邮件?
代码如下:
CDC="456.22."
PDC="123.11"
ACTIVE_DC=$CDC
#ACTIVE_DC=$PDC
#ADD/REMOVE VIRTUAL IPs HERE
vIP="app1.app.com app2.app.com"
[email protected]
subject="[TEST - - CRITICAL WARNING] NETWORK TRAFFIC POINTING TO INACTIVE DATA CENTER"
DATE=$(date '+%Y-%m-%d %H:%M:%S')
echo ""
echo "########################################################################"
echo "#################################START##################################"
for IP in $vIP; do
echo""
app_name=$(nslookup $IP | awk 'NR==4 {print NR,$0}' | cut -c 9-50) #Prints each VIP
echo "$app_name"
for i in {1..10}; do #Runs nslookup 10x for each VIP
current=$(nslookup $IP | awk 'NR==5 {print NR,$0}' | cut -c 12-30)
current2=$(echo "$current" | cut -c 1-6)
echo "$current"
if [[ "$current2" == "$ACTIVE_DC" ]]; then
echo "Traffic is pointing to active DC"
else
echo "ERROR Traffic is pointing to INACTIVE DC"
echo "$DATE - [TEST] CRITICAL - NETWORK TRAFFIC POINTING TO INACTIVE DATA CENTER
CHECK $app_name
TRAFFIC POINTING TO $current
TRAFFIC SHOULD BE POINTING to $ACTIVE_DC ONLY!
" | mailx -s "$subject" -r $MAIL_FROM [email protected]
fi
done
done
I've created a simple script that runs nslookup for the configured IPs. It will run 10x (per requirement) to validate if it's pointed to the active IP and then email an alert if it returns inactive.
Script is working perfectly, however, when it alerts, it does it 10x due to the loop. How do I get just the last line if it does not satisfy the condition so that it will only send one email per IP?
Code below:
CDC="456.22."
PDC="123.11"
ACTIVE_DC=$CDC
#ACTIVE_DC=$PDC
#ADD/REMOVE VIRTUAL IPs HERE
vIP="app1.app.com app2.app.com"
[email protected]
subject="[TEST - - CRITICAL WARNING] NETWORK TRAFFIC POINTING TO INACTIVE DATA CENTER"
DATE=$(date '+%Y-%m-%d %H:%M:%S')
echo ""
echo "########################################################################"
echo "#################################START##################################"
for IP in $vIP; do
echo""
app_name=$(nslookup $IP | awk 'NR==4 {print NR,$0}' | cut -c 9-50) #Prints each VIP
echo "$app_name"
for i in {1..10}; do #Runs nslookup 10x for each VIP
current=$(nslookup $IP | awk 'NR==5 {print NR,$0}' | cut -c 12-30)
current2=$(echo "$current" | cut -c 1-6)
echo "$current"
if [[ "$current2" == "$ACTIVE_DC" ]]; then
echo "Traffic is pointing to active DC"
else
echo "ERROR Traffic is pointing to INACTIVE DC"
echo "$DATE - [TEST] CRITICAL - NETWORK TRAFFIC POINTING TO INACTIVE DATA CENTER
CHECK $app_name
TRAFFIC POINTING TO $current
TRAFFIC SHOULD BE POINTING to $ACTIVE_DC ONLY!
" | mailx -s "$subject" -r $MAIL_FROM [email protected]
fi
done
done
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修改后的脚本:
我添加的是:
for
循环开始时,变量sendemail
设置为 false。sendemail
变量设置为 true 并break
脱离for i ...
环形。sendemail
为 true,则发送电子邮件。现在,每个 IP 仅发生一次,因为电子邮件发送代码位于 nslookup 10X 循环之外。Modified script:
What I added is:
sendemail
is set to false at the beginning of everyfor
loop on IP addresses.sendemail
variable to true andbreak
out of thefor i ...
loop.sendemail
is true, send the email. It will now happen only once per IP, since the email sending code is outside the nslookup 10X loop.