Search This Blog

Friday 29 January 2021

script to test remote tcp connection on multiple hosts

Use the following script to validate tcp connection on remote hosts. 

Step 1) Generate ssh keys on your local machine

run command: ssh-keygen -t rsa 

Step 2) Use the following script to establish ssh connectivity to all remote servers.

 #!/bin/ksh

## Author: Pavan Bandaru

##List the hostnames in host.txt file

echo "Enter your password: "

stty -echo
read -s SSHPASS
export SSHPASS
stty echo

for host in `cat host.txt`
do
sshpass -e ssh-copy-id -f ${host} -o StrictHostKeyChecking=no
done

SSHPASS=""


Step 3) Use the following script to to check tcp connection.


inputfile.txt should be updated with 3 arguments
hostname,remotehostname,ip
hostname,remotehostname,ip


#!/bin/ksh

## Author: Pavan Bandaru

display_usage() {
  echo "This script must be run with ...."
  echo -e "\nUsage:\n$0 [inputfile.txt] \n"
}


if [ $# -eq 0 ]; then
    display_usage
    exit 1
fi

inputfile=$1

echo "------------------------------------------"

for host in `cat<$inputfile`

do

hostname=$(echo $host | awk -F"," '{print $1}')

remotehost=$(echo $host | awk -F"," '{print $2}')

remoteip=$(echo $host | awk -F"," '{print $3}')

echo $hostname

echo $remotehost

echo $remoteip

ssh -T $hostname << EOSSH

timeout 2 bash -c 'echo >/dev/tcp/$remotehost/$remoteip' && echo "connection status: success" || echo "connection status: failed"

EOSSH

echo "-----------------------------------------"

done




No comments: