Network script for ping status

Actually I am using Red hat Linux 4.4 version.
I want to check the 16 client pc status of connection from server.Whether the connection is available or not.I tried to develop the shell script for this code.But It don't work properly.I am a self study learner.Please give me a shell script for this program which is suitable for Redhat Linux 4.4 version.Please help me to resolve this problem.

Here this are required fields,

One is Server Ip : 10.66.62.5

Next is, 16 Clients IP :

10.66.1.133  
10.66.6.133  
10.66.7.133   
10.66.0.133 
10.66.1.5     
10.66.2.133  
10.66.4.133  
10.66.3.133   
10.66.5.133   
10.66.2.5    
10.66.3.5    
10.66.7.5    
10.66.0.5    
10.66.6.5    
10.66.4.5    
10.66.5.5   

I am expecting output like as,

Please select your choice [1-2] : 

1 . connections available and listing those ips
2 . connections not available and listing those ips

Please select your choice [1-2]: 1

14 connections are available

Ips are ,
10.66.6.133  
10.66.7.133   
10.66.0.133 
10.66.1.5     
10.66.2.133  
10.66.4.133  
10.66.3.133   
10.66.5.133   
10.66.2.5    
10.66.3.5    
10.66.7.5    
10.66.0.5    
10.66.6.5    
10.66.4.5   

Please Select your choice [1-2] : 2

2 connections are not available

Ips are,
10.66.1.133  
10.66.5.5 

If you dont show us your code, how do you expect us to help you?

We cant just guess what you put in it... Show us the code and the output and we can help get it working...

Actually I tried this below program which is in the correct sequence order of 16 client ip address from 192.168.1.3 to 192.168.1.19.But I questioned program I don't have an idea because it is not in the correct sequence order of ip address.I just want to ping from server to 16 client ip address.whether the connection is available or not. I'm expecting output like as,
*How many connections are available and list that ips.
*How many connections are not available and list that ips.

My tried program code:

#!/bin/bash

upcnt=0
downcnt=0
for ip in 192.168.1.{3..19}
do
   if ping -c 1 -W 1 $ip > /dev/null
   then
        let upcnt=upcnt+1
        uplist="$uplist\n    $ip"
   else
        let downcnt=downcnt+1
        downlist="$downlist\n    $ip"
   fi
done

printf "$upcnt ips are available:$uplist\n"
printf "$downcnt ips are not available:$downlist\n"

Please If you have an idea , Please give me a shell script for my questioned program.

So - what's not working correctly? Are those addresses pingable one by one from the command line? And, in your 10.66... example, what is the subnet mask ? Again, are those pingable?

Perhaps something like this, it's not interactive, however it achieves what you want with test FQDN's that you can change to your IP's.

jaysunn-> cat ping_test.sh 
#!/bin/bash

ServerList='www.google.com www.bing.com www.yahoo.com www.fakesite1111.com'

for i in ${ServerList} ; do



    echo "-------------------------------------------------------------"
    echo ${i}
    echo "-------------------------------------------------------------"
    ping -c 1 ${i} |grep '1 packets transmitted'
    if [ $? == 0 ]
        then
            echo "Ping is ok"
        else
            echo "!!ping Bad!!"
    echo "-------------------------------------------------------------"
            exit 1
    fi
done  

OUTPUT:

jaysunn-> bash ping_test.sh 
-------------------------------------------------------------
www.google.com
-------------------------------------------------------------
1 packets transmitted, 1 packets received, 0.0% packet loss
Ping is ok
-------------------------------------------------------------
www.bing.com
-------------------------------------------------------------
1 packets transmitted, 1 packets received, 0.0% packet loss
Ping is ok
-------------------------------------------------------------
www.yahoo.com
-------------------------------------------------------------
1 packets transmitted, 1 packets received, 0.0% packet loss
Ping is ok
-------------------------------------------------------------
www.fakesite1111.com
-------------------------------------------------------------
ping: cannot resolve www.fakesite1111.com: Unknown host
!!ping Bad!!
-------------------------------------------------------------
[~]

How about this:

IPS="10.66.1.133  10.66.6.133  10.66.7.133  10.66.0.133
     10.66.1.5    10.66.2.133  10.66.4.133  10.66.3.133
     10.66.5.133  10.66.2.5    10.66.3.5    10.66.7.5
     10.66.0.5    10.66.6.5    10.66.4.5    10.66.5.5"

printf "1. connections available and listing those ips\n"
printf "2. connections not avialble and listing those ips\n"
while true
do
    printf "\n\nPlease select you choice [1-2]: "
    read
    printf "\n"
    CNT=0
    list=""
    case $REPLY in
       1|2) for ip in $IPS
          do
              if ping -c 1 -W 1 $ip 2>&1 2>&1 | egrep -q "unreachable|100%"
              then
                 [ $REPLY -eq 2 ] && let CNT=CNT+1 && list="$list\n$ip"
              else
                 [ $REPLY -eq 1 ] && let CNT=CNT+1 && list="$list\n$ip"
              fi
          done
          [ $REPLY -eq 1 ] && printf "$CNT connections are available\n\nIPs are,$list"
          [ $REPLY -eq 2 ] && printf "$CNT connections are not available\n\nIPs are,$list" ;;
       *) printf "Invalid choice\n\n"
          break
       ;;
    esac
done

Note: the parameters and output format of ping can vary from OS to OS for Redhat Linux you have -c 1 -W 1 parameters for 1 ping with 1 sec timeout. On another system it could be -n 1 -w 1 .

For failed output we are looking for "100%" or "unreachable" (as in "100% packet loss" or "destination is unreachable").

Excellent This is I want,Superbly working Well Done Good Job!Crores Of Thanks to Chubler_XL.I learned the above code bit by bit.Please explain the above code.

I have one question , I want to merge the branch name with that ip address in ouptut. Each ip address have a name.Below I have mentioned the branch name for IP Address.Please check it.

IPS="10.66.1.133 10.66.6.133 10.66.7.133 10.66.0.133
10.66.1.5 10.66.2.133 10.66.4.133 10.66.3.133
10.66.5.133 10.66.2.5 10.66.3.5 10.66.7.5
10.66.0.5 10.66.6.5 10.66.4.5 10.66.5.5"

BranchNames="TIRUNELVELIUNITI TIRUNELVELIUNITII TIRUNELVELICAB TENKASI

                   KOVILPATTI TUTICORIN THIRUCHENDURE SRIVAIKUNDAM

                   VALLIOOR NAGERCOILUNITI  NAGERCOILUNITII THUCALAY

                   KUZHITHURAI SANKARANKOIL CHERANMAHADEVI AMBAI"

I am expecting Output like as,

Please Select your choice[1-3]: 1
1.How many branchnetwork connections are available .
2.How many branch network connections are not available.
3.Exit

14 branch network connections are working fine.

TIRUNELVELIUNITI 10.66.1.133

TIRUNELVELIUNITII 10.66.6.133

TIRUNELVELIUNITCAB 10.66.7.133

TENKASI 10.66.0.133

KOVILPATTI 10.66.1.5

TUTICORIN 10.66.2.133

THIRUCHENDURE 10.66.4.133

SRIVAIKUNDAM 10.66.3.133

VALLIOOR 10.66.5.133

NAGERCOILUNITI 10.66.2.5

NAGERCOILUNITII 10.66.3.5

THUCALAY 10.66.7.5

KUZHITHURAI 10.66.0.5

SANKARANKOIL 10.66.6.5

Please Select your choice[1-3]: 2
1.How many branchnetwork connections are available .
2.How many branch network connections are not available.
3.Exit

2 branch network connections are not available.

CHERANMAHADEVI 10.66.4.5

AMBAI 10.66.5.5

Perhaps you can map IPs to host names, like this

hst=`getent hosts "$i" | awk '{print $2}'`
echo "IP=$ip host="$hst

?

I like MadeInGermany's idea of doing a host name lookup as this will involve less maintenance of your script, but it would involve ensuring all these machines are entered in the hosts file.

But if your trying to associate two lists as a learning exercise you could tackle it like this:

IP=( 
  10.66.1.133  10.66.6.133  10.66.7.133  10.66.0.133
  10.66.1.5    10.66.2.133  10.66.4.133  10.66.3.133
  10.66.5.133  10.66.2.5    10.66.3.5    10.66.7.5
  10.66.0.5    10.66.6.5    10.66.4.5    10.66.5.5 )
BN=( 
    TIRUNELVELIUNITI TIRUNELVELIUNITII TIRUNELVELICAB  TENKASI
    KOVILPATTI       TUTICORIN         THIRUCHENDURE   SRIVAIKUNDAM
    VALLIOOR         NAGERCOILUNITI    NAGERCOILUNITII THUCALAY
    KUZHITHURAI      SANKARANKOIL      CHERANMAHADEVI  AMBAI )

printf "1. connections available and listing those ips\n"
printf "2. connections not avialble and listing those ips\n"
while true
do
    printf "\n\nPlease select you choice [1-2]: "
    read
    printf "\n"
    CNT=0
    list=""
    case $REPLY in
       1|2) for((i=0;i<${#IP[@]};i++))
          do
              echo ping -c 1 -W 1 ${IP}
              if ping -n 1 ${IP} 2>&1 2>&1 | egrep -q "unreachable|100%"
              then
                 [ $REPLY -eq 2 ] && let CNT=CNT+1 && list="$list\n${BN} ${IP}"
              else
                 [ $REPLY -eq 1 ] && let CNT=CNT+1 && list="$list\n${BN} ${IP}"
              fi
          done
          [ $REPLY -eq 1 ] && printf "$CNT connections are available\n\nIPs are,$list"
          [ $REPLY -eq 2 ] && printf "$CNT connections are not available\n\nIPs are,$list" ;;
       *) printf "Invalid choice\n\n"
          break
       ;;
    esac
done

I think you are genious chubler . 100 crores of thanks to you.
I have another question,please I want to add Branchcode(BC) also with ipaddress(IP) and BranchNames(BN) in output.

[code]IP=(
10.66.1.133 10.66.6.133 10.66.7.133 10.66.0.133
10.66.1.5 10.66.2.133 10.66.4.133 10.66.3.133
10.66.5.133 10.66.2.5 10.66.3.5 10.66.7.5
10.66.0.5 10.66.6.5 10.66.4.5 10.66.5.5 )

BC=(753 74P 74U 666
667 74W 74Z 32A
74G 754 7251 74S
300 74H 32B 74E)

BN=(
TIRUNELVELIUNITI TIRUNELVELIUNITII TIRUNELVELICAB TENKASI
KOVILPATTI TUTICORIN THIRUCHENDURE SRIVAIKUNDAM
VALLIOOR NAGERCOILUNITI NAGERCOILUNITII THUCALAY
KUZHITHURAI SANKARANKOIL CHERANMAHADEVI AMBAI )

Please Select your choice[1-3]: 1
1.How many branchnetwork connections are available .
2.How many branch network connections are not available.
3.Exit

14 branch network connections are working fine.

753 TIRUNELVELIUNITI 10.66.1.133

74P TIRUNELVELIUNITII 10.66.6.133

74U TIRUNELVELIUNITCAB 10.66.7.133

666 TENKASI 10.66.0.133

667 KOVILPATTI 10.66.1.5

74WTUTICORIN 10.66.2.133

74Z THIRUCHENDURE 10.66.4.133

32A SRIVAIKUNDAM 10.66.3.133

74G VALLIOOR 10.66.5.133

754 NAGERCOILUNITI 10.66.2.5

7251 NAGERCOILUNITII 10.66.3.5

74S THUCALAY 10.66.7.5

300 KUZHITHURAI 10.66.0.5

74H SANKARANKOIL 10.66.6.5

Please Select your choice[1-3]: 2
1.How many branchnetwork connections are available .
2.How many branch network connections are not available.
3.Exit

2 branch network connections are not available.

32B CHERANMAHADEVI 10.66.4.5

74E AMBAI 10.66.5.5