mercredi 29 juin 2016

Nested if statement in for loop bash

I'm writing a bash script that goes through a for loop which is a list of each hostname, then will test each one if it's responding on port 22, if it is then execute an ssh session, however the second if statement is only executed on the first host in the list, not the second. If the host isn't responding on port 22, I want the script to continue to the next host. Any ideas how to ensure the script runs the ssh on each host in the list? Should this be another for loop?

!/bin/bash

hostlist=$(cat '/local/bin/bondcheck/hostlist_test.txt')

for host in $hostlist; do

test=$(nmap $host -P0 -p 22 | egrep 'open|closed|filtered' | awk '{print $2}')

    if [[ $test = 'open' ]]; then

                    cd /local/bin/bondcheck/
                    mv active.current active.fixed
                    ssh -n $host echo -n "$host: ; cat /proc/net/bonding/bond0 | grep Active" >> active.current

                    result=$(comm -13 active.fixed active.current)

            if [ "$result" == "" ]; then
                            exit 0
            else
                    echo "$result" | cat -n
            fi

    else
            echo "$host is not responding"
    fi

done

Aucun commentaire:

Enregistrer un commentaire