Help with shell script to check the tcp network connectivity between server

Hello,

I have a requirement to check the tcp network connectivity between server it's running on and the list of host's and ports combination.

i have written the below code but it doesn't work, but when i execute the nc command outside the script it works fine.

please help me where i am going wrong.

#!/bin/bash
#read the file line by line

cd "$1"
cat testping.txt | while read line
do
        # check if there are no blank lines
        if [ ! -z $line ]; then
                hosts=$line|sed 's/:/ /g'
                result=$"nc -z $hosts"
                echo $result >testouput.txt
        fi
done

testping.txt file contains

seattle01.iris2.local:1522
boston01-vip.iris2.local:1521
boston02-vip.iris2.local:1521

To assign the output of a command to a variable, use command substitution:

hosts=$(echo "$line"|sed 's/:/ /g')

or, since you are using bash, try:

hosts=${line//:/ }

or since you are using read to process the file, you could also:

while IFS=: read host port
do
  # add some checks here
  nc -z "$host" "$port"
done < testping.txt > testouput.txt

Thanks scrutinizer for your help.
i made the changes you mentioned but i am not geeting the output to the file for each line of execution , i only got the last host command saved in the testoutput file.

code:

#!/bin/bash
#read the file line by line

cd "$1"
cat testping.txt | while read line
do
        # check if there are no blank lines
        if [ ! -z $line ]; then
               # hosts=$line|sed 's/:/ /g'
              hosts=$(echo "$line"|sed 's/:/ /g')
                result=$"nc -z $hosts"
                echo $result >testouput.txt
        fi
done

content in testoutput.txt

nc -z boston02-vip.iris2.local 1521

please advice.

Hi, that was not the only place where you needed command substitution. Have a look at the next line..

Please use code tags for code and data samples..

hi,

my nc command is not working inside the shell, if i copy the command from the output file in the command prompt then it works fine as expected.

code:

#!/bin/bash
#read the file line by line

cd "$1"
cat testping.txt | while read line
do
        # check if there are no blank lines
        if [ ! -z $line ]; then
			    hosts=${line//:/ }
                result=$"nc -z $hosts"	
                echo $result >testouput.txt
        fi
done

ouput:
nc -z boston02-vip.iris2.local 1521

please advice scrutinizer.

As Scrutinizer said: use command substitution $(...) for the result assignment!

Hello RudiC,

As per your and Scrutinizer's advice, i midified the code as shown below,but still i am getting the last record execution ouput into the output file not all 3.

code

#!/bin/bash
#read the file line by line

cd "$1"
cat testping.txt | while read line
do
        # check if there are no blank lines
        if [ ! -z $line ]; then
			    hosts=${line//:/ }
				echo $(nc -z $hosts) > testouput.txt
        fi
done

output:

Connection to boston02-vip.iris2.local 1521 port [tcp/ncube-lm] succeeded!

please advice.

Thanks.

Then use the >> (append) redirection instead of > (overwrite).

1 Like

And why are we using echo and command substitution instead of just running the command???

Unless you need to convert sequences of spaces and/or tabs in the nc -z output to single spaces, why not change:

echo $(nc -z $hosts) >> testouput.txt

to:

nc -z $hosts >> testouput.txt