mercredi 25 décembre 2019

cat a file into an array and read it through if statement in bash

I am trying to make a script in bash that scans my network for alive IPs and then it does port scan and grep services (ssh etc) and products (OpenSSH) and versions then stores them to name.txt,product.txt,version.txt OR to an array. Then, here comes my problem, i want it to take first line from all three files and check them against my arrays, if first line in name.txt = ssh_NameDataBase[] and first line in product.txt = ssh_VulnsDataBase[] and first line in version.txt then echo "vulnerable". the code below works but it prints about 24 times. I REALLY would appreciate your help

ssh_NameDataBase=('ssh' '')
ssh_VulnsDataBase=('OpenSSH' 'OpenSSH')
ssh_VersionDataBase=('2.9p2' '3.9p2')

cat $ips/$ips.xml | grep -oP 'name="\K[^"]+' > $ips/name.txt
cat $ips/$ips.xml | grep -oP 'product="\K[^"]+' > $ips/product.txt
cat $ips/$ips.xml | grep -oP 'version="\K[^"]+' > $ips/version.txt

my problem starts here

for name in $(cat $ips/name.txt)
do
    for product in $(cat $ips/product.txt)
    do
        for version in $(cat $ips/version.txt)
        do
            for ssh in "${ssh_VulnsDataBase[@]}"
            do
                if [ "$product" = "$ssh" ]
                then
                    if [ "$version" = "${ssh_VersionDataBase=[@]}" ]
                    then
                        echo "$ips is vulnerable to $product $version you need to upgrade"
                    fi
                fi
            done
        done
    done
done

Aucun commentaire:

Enregistrer un commentaire