0

This question already has an answer here:

I have a script in AIX to read all hosts from os_init.d directory and gather their Ipv4 IPs and store them in a file. This file is then used to ssh to each host to collect various data via other scripts [not relevant here].

  1. When the host is not reachable and has IPv4 IP address --> no error, while loop continues
  2. When the host is not reachable and has IPv6 IP address --> no error, while loop continues
  3. When the host is reachable and has IPv6 IP address --> while loop continues BUT exists without going through the loop.

I can't seem to figure out why in the last condition, after successfully executing the if condition for a reachable IPv6 host, the while loop would exit without continuation.

#!/usr/bin/bash
# Sumit Das 2016 12 26
# Find All PHYsical HOSTs and their IP in a cluster

#set -x
#cd /home/users/in10c2/moni
ls -1 /cAppCom/os_init.d | grep dcpaix > listPHYSERV
FN='listPHYSERV'
rm listPHYIP 2> /dev/null
while read LINE
do
  PHYHOST=$LINE
  PHYHOSTIP=`ping -c 1 $LINE | grep PING | awk '{print $3}' | cut -c2- | rev | cut -c3- | rev`

  IPV6IPchk=`echo $PHYHOSTIP | cut -c -4`
  if [ $IPV6IPchk = "2a00" ] 
    then 
    ssh -q -o ConnectTimeout=10 -o BatchMode=yes -o StrictHostKeyChecking=no $LINE ifconfig -a > IFCNF 
    PHYHOSTIP=`awk '/en2:/{x=NR+1;next}(NR<=x){print}' IFCNF | awk '{print $2}'` 
    rm IFCNF
  fi

  echo "$PHYHOST,$PHYHOSTIP" >> listPHYIP
done < $FN

cat listPHYIP
cp  ALL_PHY_HOSTIP_`uname -n`.txt

marked as duplicate by Stephen Kitt, sam, Jeff Schaller, Rui F Ribeiro, GAD3R Dec 28 '16 at 7:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5

You need to use ssh -n, otherwise a successful ssh consumes standard input and your while loop is left with nothing to read.

Not the answer you're looking for? Browse other questions tagged or ask your own question.