-1

I'm writing a code in order to capture information about routers thanks to SSH and SNMP. I work on network with 4 different router configurations. In my script bash, a while loop read line by line the IP address of routers then try to know the configuration and finally capture information about my router.

To do so, I use a while loop but it only run for one IP address... I assume that the problem comes from SNMP or SSH.

I tried the while loop just with

echo $line

The while loop :

while IFS= read -r line
do
    echo
    echo "line : $line"
    echo

    typeRouter=""
    version=""


    # type multitech V4.1
    type=$(snmpget -v3 -u Ai -l authpriv -a SHA -A "pass" -x AES -X "pass" "$line" 1.3.6.1.4.1.995.14.1.1.8.3.0 | cut -d\" -f2 | cut -d. -f1 )
    if  [[ "$type" == "4" ]]; then
        typeRouter=2
        version=4
    fi


    # type sierra
    type=$(snmpget -v3 -u Ai -l authpriv -a SHA -A "pass" -x AES -X "pass" "$line" iso.3.6.1.4.1.20542.9.1.1.9.7.0 | cut -d\" -f2  ) 
    if [[ "$type" == "LX40" ]]; then
        typeRouter=3
    fi

    case $typeRouter in 
    2)
            echo "Mutlitech"

            # version 4.1 et ultérieur
            MultiV4 "$line" "$txtfile" "pass" "pass"
            ;;

    3) # TODO PASSWORD
        echo "Sierra"

        Sierra "$line" "$txtfile" "pass" "pass"
        ;;

    *)
        echo
        echo "Unknown router model, IP : $line"
        echo
        ;;

    esac

done < "file"

The function called

MultiV4 () {
    # "$1" : @IP
    # $2 : txtfile
    # "$3" : mdp snmp
    # $4 : mdp ssh

    # Files
    txtfile=$2
    model="Multitech"
    MACtxt="ephemere/mac.txt"


    MacAddressRouter=$(snmpget -v3 -u Ai -l authpriv -a SHA -A "$3" -x AES -X "$3" "$1" iso.3.6.1.4.1.995.14.1.1.5.1.0 | cut -d\" -f2)
    echo "Router MAC address : $MacAddressRouter"

    sshpass -p "$4" ssh -o StrictHostKeyChecking=no admin@"$1"  "cat /proc/net/arp" > "$MACtxt"

    rssiPacket=$(snmpget -v3 -u Ai -l authpriv -a SHA -A "$3" -x AES -X "$3" "$1" iso.3.6.1.4.1.995.14.1.1.3.5.0 | cut -d: -f2 | cut -d\" -f2 | tr -d dBm)
    echo "RSSI packet : $rssiPacket"

    firmware=$(snmpget -v3 -u Ai -l authpriv -a SHA -A "$3" -x AES -X "$3" "$1" iso.3.6.1.4.1.995.14.1.1.3.4.0 | cut -d: -f2 | cut -d\" -f2)
    echo "Firmware version : $firmware"

    echo "$MacAddressRouter", "$MacAddressPlayer", "$rssiPacket", "$model", "$firmware", "$imei", "$SIMStatus", "$operator", "$connectionType", "$IPaddress", "$SINR", "$RSRP", "$RSRQ" >> "$txtfile"
}

My file

10.252.144.138
10.252.144.140
10.252.144.23
10.252.144.15
10.252.144.134

Can you help me why the loop running only one time ?

Kazrak
  • 13
  • 4
  • 1
    Try to run with -vx to see what was actually executed. – Eran Ben-Natan Aug 26 '19 at 10:31
  • 1
    Did you have any output? Please add to the question. Can you make the problem easier? What happens when you remove the call to `MultiV4()` ? – Walter A Aug 26 '19 at 13:20
  • in your MultiV4 func, I think you can figure out a way to call `snmpget` one time, and use an `awk` script to output multiple words that could be assigned to a shell array. Agree with both comments above. Good luck. – shellter Aug 26 '19 at 15:38
  • 1
    Possible duplicate of https://stackoverflow.com/questions/13800225/shell-script-while-read-line-loop-stops-after-the-first-line – tripleee Aug 28 '19 at 12:06
  • Thanks for your redirection @tripleee ! The problem is solved – Kazrak Aug 28 '19 at 14:35

1 Answers1

0

I solved the problem using

ssh -n 

Go see this answer for more explanation : Shell script while read line loop stops after the first line

Kazrak
  • 13
  • 4