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.
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?
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.
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.
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.
---------- 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
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.
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?