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
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
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
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!