I created a Bash Shell script on Gnu/Linux, that does an ssh from one server to a bunch of other servers, and runs netcat command for a bunch of IPs.
This is the script that connects to other servers via SSH:
*read -p "Enter the instant stack host[s]: " hosts
read -p "Enter the list of destinations [IP]: " dest_list
read -p "Enter the port # associated with the destination: " port
for h in $hosts
do
scp ./connectivityTest.sh @$h:/var/tmp/
ssh user@$h . /var/tmp/connectivityTest.sh "$dest_list" "$port"
done*
This is the script that runs the netcat command:
*echo "Working on `hostname` to check connectivity for following end points"
echo $1
echo $2
dest_list=`echo "$1"`
port=`echo "$2"`
echo $dest_list
for d in $dest_list
do
echo "Endpoint checked: $d"
nc -v -i 1 -w 5 $d $port
done
exit*
When I give just single IP [end point] to run, the script runs fine:
*Enter the instant stack host[s]: host1.com
Enter the list of destinations [IP]: **test.ip1.com**
Enter the port # associated with the destination: 443
connectivityTest.sh 100% 520 1.8MB/s 00:00
##############################################################
Working on host1 to check connectivity for following end points
##############################################################
test.ip1.com
443
test.ip1.com
Endpoint checked: test.ip1.com
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to ip1:443.
Ncat: Idle timeout expired (1000 ms).*
However when I give multiple end points, the script takes the second IP/end point as the port number.
*Enter the instant stack host[s]: host1.com
Enter the list of destinations [IP]: **test.ip1.com test.ip2.com**
Enter the port # associated with the destination: 443
connectivityTest.sh 100% 520 1.8MB/s 00:00
##############################################################
Working on host1 to check connectivity for following end points
##############################################################
test.ip1.com
**test.ip2.com**
test.ip1.com
Endpoint checked: test.ip1.com
Ncat: Version 7.50 ( https://nmap.org/ncat )
**Ncat: Invalid port number "test.ip2.com". QUITTING.***
Also this happens only when the script is run via SSH. If I run the script directly on the server, it works fine with multiple IPs. It is obvious that something is not right at this level:
*ssh user@$h . /var/tmp/connectivityTest.sh "$dest_list" "$port"*
Then I found this work around. I added the input variables under single quote:
*ssh user@$h . /var/tmp/ "'$dest_list'" "'$port'"*
It works with multiple IPs now:
*Enter the instant stack host[s]: host1.com
Enter the list of destinations [IP]: **test.ip1.com test.ip2.com test.ip3.com test.ip4.com**
Enter the port # associated with the destination: 443
connectivityTest.sh 100% 525 1.5MB/s 00:00
##############################################################
Working on host1 to check connectivity for following end points
##############################################################
test.ip1.com test.ip2.com
443
test.ip1.com test.ip2.com
Endpoint checked: test.ip1.com
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to ip1:443.
Ncat: Idle timeout expired (1000 ms).
Endpoint checked: test.ip2.com
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to ip2:443.
Ncat: Idle timeout expired (1000 ms).*
Even though I resolved it, I am not sure how the single quote makes the difference between running the script locally vs running it via ssh. Also how the single quote makes the difference between running the script with multiple inputs vs single input.
Any insights will be helpful. Thanks in advance !!!