Search This Blog

Friday, 28 September 2018

SHELL SCRIPTS

1) Shell Script to copy ssh keys to remote Servers



#!/bin/ksh

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=""


List the hostnames in host.txt file



2) Shell Script to use rsync to copy the files to remote servers



#!/bin/sh

## Author: Pavan Bandaru

## Date: 9/28/2018

set -x



display_usage() {

  echo "This script must be run with ...."

  echo -e "\nUsage:\n$0 [hostfile.txt] \n"

}



if [ $# -eq 0 ]; then

    display_usage

    exit 1

fi



hostfile=$1

for host in `cat<$hostfile`

do

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

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

  echo "connecting to $hostname"

  rsync -r localdir ${hostname}:/home/pavan/


RESULT=$?

if [ $RESULT -ne 0 ]; then

  echo Login Failed

fi


done


Note: Keep the remote host list on the host file. 


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

Shell Script to use rsync to copy the files to remote servers

#!/bin/sh
## Author: Pavan Bandaru
## Date: 9/28/2018
set -x

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

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

hostfile=$1
for host in `cat<$hostfile`
do
  hostname=$(echo $host|awk -F"," '{print $1}')
  echo "------------------------------------------------"
  echo "connecting to $hostname"
  rsync -r localdir ${hostname}:/home/pavan/

RESULT=$?
if [ $RESULT -ne 0 ]; then
  echo Login Failed
fi

done



Note: Keep the remote host list on the host file. 

Tuesday, 25 September 2018

Shell Script to copy ssh keys to remote Servers

#!/bin/ksh
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=""


List the hostnames in host.txt file