获取迭代的最后一行 - UNIX

发布于 2025-01-12 19:59:40 字数 1777 浏览 0 评论 0原文

我创建了一个简单的脚本,为配置的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

擦肩而过的背影 2025-01-19 19:59:40

修改后的脚本:

#!/bin/bash

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
    sendemail='false'
    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
            sendemail='true'
            echo "ERROR Traffic is pointing to INACTIVE DC"
            break
        fi
    done
    
    if [[ "$sendemail" == 'true' ]]
    then
        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

我添加的是:

  • 在 IP 地址上的每个 for 循环开始时,变量 sendemail 设置为 false。
  • 如果出现错误,则必须发送电子邮件,因此将 sendemail 变量设置为 true 并break 脱离for i ...环形。
  • 如果 sendemail 为 true,则发送电子邮件。现在,每个 IP 仅发生一次,因为电子邮件发送代码位于 nslookup 10X 循环之外。

Modified script:

#!/bin/bash

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
    sendemail='false'
    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
            sendemail='true'
            echo "ERROR Traffic is pointing to INACTIVE DC"
            break
        fi
    done
    
    if [[ "$sendemail" == 'true' ]]
    then
        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

What I added is:

  • variable sendemail is set to false at the beginning of every for loop on IP addresses.
  • If there is an error, the email must be sent, so set the sendemail variable to true and break out of the for i ... loop.
  • If the 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文