Help with to check the tcp network connectivity between servers and hosts

ello,

i am new to the networking side.
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.

please help me where i am going wrong.

my 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

i think nc doesn't work, as i need to check the tcp connectivity.

testping.txt file contains

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

my output file should be

seattle01.iris2.local:1522|success |3.4s
boston01-vip.iris2.local:1521|success |0.2s
boston02-vip.iris2.local:1521|service not running|3.4s

please help me how to find this.

Not sure what you want to achieve. You had the success message already in your other thread. What else info do you need, and where do you expect to get it from?

Hello Rudic,

Sorry to trouble/confuse you.

  1. If the connection is refused or failed or any other status other than success, the message is not populating the output file.
  2. the format of the ouput file should be as i mentioned below and i am not getting the time elapsed for the connection.

seattle01.iris2.local:1522|success |3.4s
boston01-vip.iris2.local:1521|success |0.2s
boston02-vip.iris2.local:1521|service not running|3.4s

please help.

Try the verbose -v option to nc . No idea where to get the time info from.
If you want an output structure other than what nc provides you need to filter it through e.g. awk , sed , or other.

sry Rudic,

No luck.

May depend on your nc version.

nc -v localhost 2000
Connection to localhost 2000 port [tcp/cisco-sccp] succeeded!
nc -v localhost 2000
nc: connect to localhost port 2000 (tcp) failed: Connection refused

hello Rudic,

I changed my code slightly as shown below but getting some error ,please help

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//:/ }
				#nc_result='(nc -zv $hosts); echo $?'
				nc_result='(nc -zv $hosts)'
				if [ $nc_result != 0 ]; then
				result='Success'
				else
				result='Connection rejected'
				fi
				
				message="$line | $result"
				echo "$message" >> testoutput.txt
        fi 
done

the errors i am getting is

line 12: [: too many arguments
line 12: [: too many arguments
line 12: [: too many arguments

What is this construct: '(nc -zv $hosts)' ? If you want to use command substitution, use $(...) . That will make the exit code available via $? as well. You may need to redirect stderr to stdout to capture the error msg.

Hello,

My below code still not working, if the connection is rejected , the output is not appending to the output 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//:/ }
                nc -z $hosts >> testoutput.txt 2>&1
        fi
done

if there is any hosts with connection rejected, the entry is not available in the oyutput file.

please help.

Try changing:

        if [ ! -z $line ]; then
                hosts=${line//:/ }
                nc -z $hosts >> testoutput.txt 2>&1

to:

        if [ ! -z "$line" ]; then
                hosts=${line//:/ }
                nc -z $hosts 2>&1 >> testoutput.txt

sknovice,
you seem to verify Oracle RAC network interfaces,
so why don't you just use the tool Oracle provides?
Assuming GI is already installed:

cluvfy comp nodecon -n node1,node3 �i eth0

or

cluvfy comp nodecon -n all

Where node1, node2, noden are your cluster nodes (all = all cluster nodes).

Check the official documentation for more details.

I just used them as sample only.
one of the host i am using is www.hotmail.com:53 and www.amazon.com:25 etc.

---------- Post updated at 05:18 AM ---------- Previous update was at 05:17 AM ----------

---------- Post updated at 05:22 AM ---------- Previous update was at 05:18 AM ----------

Hello Don,

That's not working as well, it's creating a file named 1 with no data and creating another file testoutput.txt with only succeeded one's not failed one's.

please advice.

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//:/ }
                nc -zv $hosts 2>1 >> testoutput.txt
        fi
done

Compare what you have posted to what Don Cragun proposed. And I do not understand why you refuse to use the -v option.

Hello Rudic,

Sorry, I copied old code accidently, I am using -zv only.

Then, when using 2>&1 to redirect stderr, you should have all info in your file.

Hello Rudic,

I modified my code as Don suggested but thats not working as well, it's creating a file named 1 with no data and creating another file testoutput.txt with only succeeded one's not failed one's.

please advice.

Did you for sure use 2>&1 as proposed? In your post you showed 2>1 which means "redirect stderr to file 1 and overwrite".
By the way, if you do not redirect at all, what be the output?

Hello Ridic,

now it's working fine.

thanks.