检查120秒的命令后跳出循环

检查120秒的命令后跳出循环
nmap_vm(){ 
    while (( SECONDS < 120 )); do

        sshportactive=$(nmap $kernel_ipv4 -PN -p ssh | egrep 'open|closed|filtered' | awk '{print $2}')
            if [[ $sshportactive == open ]]; then
            $(setup_vm)
            break
            fi
            $(failed_VM)
    done

}

我正在尝试启动多个虚拟机并检查它们的 ssh 端口是否打开。如何持续不断地检查sshportactive==打开直到120秒。

就像如果在 120 秒之前达到该值,它就会中断并执行$(setup_vm)& 如果该值不是 == 打开直到 120 秒,我们就中断循环并执行$(失败的虚拟机)

编辑1

我按照你说的尝试了,但它不会等待 120 秒(我在下面发布了一个示例代码,我们将从生产中的另一个函数获取 IP)

funA(){

    #Now we nmap to check if the SSH is Working or Not
    t_start=$SECONDS

    while (( (SECONDS - t_start) < 10 )); do
        if nmap $1 -PN -p ssh -oG - | grep -q '22/open/tcp'; then
            echo " This VM is Working Properly $2"
            return 0
        fi
        sleep 1
    done

    # we only get here if the loop exits without the port being open
    echo "This VM is not working $2"
    return 1
}

funB(){

        funA 172.105.252.241 lol
        funA 192.46.213.31 lol
}

我们得到的输出为:

bash -x g.sh 
+ funB
+ funA 172.105.252.241 lol
+ t_start=0
+ ((  (SECONDS - t_start) < 10  ))
+ nmap 172.105.252.241 -PN -p ssh -oG -
+ grep -q 22/open/tcp
+ echo ' This VM is Working Properly lol'
 This VM is Working Properly lol
+ return 0
+ funA 192.46.213.31 lol
+ t_start=0
+ ((  (SECONDS - t_start) < 10  ))
+ nmap 192.46.213.31 -PN -p ssh -oG -
+ grep -q 22/open/tcp
+ sleep 1
+ ((  (SECONDS - t_start) < 10  ))
+ nmap 192.46.213.31 -PN -p ssh -oG -
+ grep -q 22/open/tcp
+ sleep 1
+ ((  (SECONDS - t_start) < 10  ))
+ nmap 192.46.213.31 -PN -p ssh -oG -
+ grep -q 22/open/tcp
+ sleep 1
+ ((  (SECONDS - t_start) < 10  ))
+ nmap 192.46.213.31 -PN -p ssh -oG -
+ grep -q 22/open/tcp
+ sleep 1
+ ((  (SECONDS - t_start) < 10  ))
+ echo 'This VM is not working lol'
This VM is not working lol
+ return 1

编辑2

我直接从生产中使用的脚本发布它

ipv4_11011_22022="192.46.209.19"
ipv4_33033_44044_10101="45.79.121.184"
ipv4_55055_10001_20002="172.105.47.73"
ipv4_20202_30303_40404_50505_30033="172.105.58.123"
ipv4_30003_40004_50005="172.105.34.152"
ipv4_10011_20022_40044="45.79.124.118"
ipv4_50055_11111="172.105.48.47"
ipv4_22222_33333="172.105.51.44"
ipv4_44444_55555="172.105.253.130"
ipv4_KERNEL="172.105.42.211"
VM1_LABEL="11011_22022"
VM2_LABEL="33033_44044_10101"
VM3_LABEL="55055_10001_20002"
VM4_LABEL="20202_30303_40404_50505_30033"
VM5_LABEL="30003_40004_50005"
VM6_LABEL="10011_20022_40044"
VM7_LABEL="50055_11111"
VM8_LABEL="22222_33333"
VM9_LABEL="44444_55555"
VMK_LABEL="KERNEL"


healthcheck(){


    nmap $ipv4_11011_22022 $VM1_LABEL
    nmap $ipv4_33033_44044_10101 $VM2_LABEL
    nmap $ipv4_55055_10001_20002 $VM3_LABEL
    nmap $ipv4_20202_30303_40404_50505_30033 $VM4_LABEL
    nmap $ipv4_30003_40004_50005 $VM5_LABEL
    nmap $ipv4_10011_20022_40044 $VM6_LABEL
    nmap $ipv4_50055_11111 $VM7_LABEL
    nmap $ipv4_22222_33333 $VM8_LABEL
    nmap $ipv4_44444_55555 $VM9_LABEL
    nmap $ipv4_KERNEL $VMK_LABEL

}


nmap(){
    #Now we nmap to check if the SSH is Working or Not
    t_start=$SECONDS

    while (( (SECONDS - t_start) < 120 )); do
        if nmap $1 -PN -p ssh -oG - | grep -q '22/open/tcp'; then
            echo " This VM is Working Properly $2"
                return 0
        fi
        sleep 5
    done

    # we only get here if the loop exits without the port being open
    healthcheck_failed $2
    return 1
}

healthcheck_failed(){

    echo "Healthcheck of VM $1 FAILED"
}

healthcheck

我们得到的输出为: 输出发布在这里

IDK 出了什么问题,脚本没有移动啊,它卡在第一个 IP :(

答案1

不要在每次循环迭代期间检查 的值,而是在循环开始之前SECONDS记录 的值,然后计算每次循环迭代的经过时间并进行检查。SECONDS例如:

#!/bin/bash

nmap_vm() {
    t_start=$SECONDS

    while (( (SECONDS - t_start) < 10 )); do
        if some_command; then
            echo "some_command succeeded"
            return 0
        fi

        sleep 1
    done

    # we only get here if the loop exits because the time expired
    echo "some_command failed"
    return 1
}

nmap_vm

更新您的代码,以便我们仅failed_vm在 120 秒后无法连接时才调用,我们会得到类似以下内容的信息:

#!/bin/bash

setup_vm() {
    echo setup vm
}

failed_vm() {
    echo failed vm
}

nmap_vm() {
    t_start=$SECONDS

    while (( (SECONDS - t_start) < 10 )); do
        # rather than nmap | egrep | awk, we can use nmap's "greppable" output and just look
        # for a specific fixed pattern
        if nmap $kernel_ipv4 -PN -p ssh -oG - | grep -q '22/open/tcp'; then
            setup_vm
            return 0
        fi

        # you don't necessarily need this delay in the loop but I like
        # to ensure a reasonable rate limit.
        sleep 1
    done

    # we only get here if the loop exits without the port being open
    failed_vm
    return 1
}

相关内容