Quantcast
Viewing all articles
Browse latest Browse all 3

Answer by Jesse_b for Ping hosts based on filter and return dead or alive

I believe this will do what you want:

pingme () {
hostfile="/home/jbutryn/test/hostfile.txt"
IFS= mapfile -t  hosts < <(cat $hostfile)
for host in "${hosts[@]}"; do
    match=$(echo "$host" | grep -o "\-$1_" | sed 's/-//' | sed 's/_//')
    if [[ "$match" = "$1" ]]; then
        hostname=$(echo "$host" | awk '{print $2}')
        ping -c1 -W1 $(echo "$host" | awk '{print $1}') > /dev/null
        if [[ $? = 0 ]]; then
            echo "$hostname is alive"
        elif [[ $? = 1 ]]; then
            echo "$hostname is dead"
        fi
    fi
done
}

You can run this function like: pingme UR01 and it should return something like:

[root@JBCLAMP001 test]# pingme CC
BB-CC_DD is dead
AA-CC_EE is alive
[root@JBCLAMP001 test]# pingme DD
CC-DD_EE is dead
CC-DD_JJ is alive

Viewing all articles
Browse latest Browse all 3

Trending Articles