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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with shell script to check the tcp network connectivity between server
# 1  
Old 06-01-2014
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.

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'
                result=$"nc -z $hosts"
                echo $result >testouput.txt
        fi
done

testping.txt file contains

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


Last edited by Scrutinizer; 06-01-2014 at 09:33 AM.. Reason: Formatting, code tags
# 2  
Old 06-01-2014
To assign the output of a command to a variable, use command substitution:
Code:
hosts=$(echo "$line"|sed 's/:/ /g')

or, since you are using bash, try:
Code:
hosts=${line//:/ }

or since you are using read to process the file, you could also:
Code:
while IFS=: read host port
do
  # add some checks here
  nc -z "$host" "$port"
done < testping.txt > testouput.txt


Last edited by Scrutinizer; 06-01-2014 at 05:04 PM..
# 3  
Old 06-01-2014
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:

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

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

please advice.

Last edited by Scrutinizer; 06-01-2014 at 09:45 AM.. Reason: code tags
# 4  
Old 06-01-2014
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..
# 5  
Old 06-01-2014
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:

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.
# 6  
Old 06-01-2014
As Scrutinizer said: use command substitution $(...) for the result assignment!
# 7  
Old 06-01-2014
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
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check connectivity with multiple hosts - BASH script available here

Hi everyone! Some time ago, I had to check connectivity with a big list of hosts, using different formats (protocol://server:port/path/, server:port, ....). I developed a script that checks the connectivity using different commands (ping, telnet, nc, curl). It worked for me so I'm sharing it... (9 Replies)
Discussion started by: Fr3dY
9 Replies

2. Solaris

External Network Connectivity w/Oracle VM Server for SPARC & Solaris 11

Hello all, thanks for reading my question: So I've been a Unix/Linux SysAdmin for a couple years, and I'm a bit over my head running solo, trying to set up LDoms using Oracle VM Server 3.1 for SPARC. I've been very careful, and things have gone well up until the point I try to access the new... (9 Replies)
Discussion started by: Lyxix
9 Replies

3. IP Networking

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... (17 Replies)
Discussion started by: sknovice
17 Replies

4. Shell Programming and Scripting

Check the connectivity of the DB through script, exit if no connection

check the connectivity of the DBs through script, script should exit if no connection and display the output as below. connectivity for DB1 is OK connectivity for DB2 is OK connectivity for DB3 is FAILED for DB in 1 2 3 do (sqlplus -s... (5 Replies)
Discussion started by: only4satish
5 Replies

5. Solaris

Sybase Connectivity Check through Shell Script

Hi, I need to check the sysbase database connectivity through the Unix Shell Script. Can you help me on the how to write the script to test the sysbase database connection. Thanks in Advance Nandha (0 Replies)
Discussion started by: nandha2387
0 Replies

6. Shell Programming and Scripting

Check connectivity script

This past weekend I had some issues with my ISP. So for future purpose I'm going to have some logging on my internet so I'm able to attach log files to my complaint email if this issue reoccurs. Decided to do a simple ping script that runs every 5 or 10 min with crontab if ping fail write date... (5 Replies)
Discussion started by: chipmunken
5 Replies

7. Shell Programming and Scripting

Linux: Writing a tricky script to check connectivity

So, first and foremost, I'm having issues with my internet connection. Periodically, the connection drops across the network. The fix is simple enough: restart the modem. However, this gets old when the connection dies out every hour. I can hit my surfboard on 192.168.100.1, and navigate to a... (5 Replies)
Discussion started by: kungfujoe
5 Replies

8. Shell Programming and Scripting

Script to check connectivity

I want to write a script to check if a unix box say abc.tdc.cin.net can be connected or not on certain port say 22. right know i have to telnet them manually from DOS prompt and if it is successful then isay it is connected. Also to check Database connectivity I am using tnsping From DOS prompt.... (3 Replies)
Discussion started by: kukretiabhi13
3 Replies

9. UNIX for Advanced & Expert Users

how to check if a process is running in a server from shell script.

I want to write a unix shell script that will check if a process (say debu) is running in the server or not. If no , then send a mail to the corresponding person to start the process??? (2 Replies)
Discussion started by: debu
2 Replies

10. Programming

How to check TCP server status

Please tell me according to C/C++ socket programming; how client can check whether server is running or not during TCP communication. (1 Reply)
Discussion started by: mansoorulhaq
1 Replies
Login or Register to Ask a Question