Script that transfers over the trusted keys to remote hosts and scp or rsync in multiple hosts in parallel from a remote host

 I've never logged into before via ssh, but it also cats the trusted key into the ~/.ssh/authorized_ksys file. Essentially it would be like combining the two scripts below. 

1.  Create a script 1 with following:



#!/bin/bash

set -x
HOST_FILE=$1
for SERVER in `cat $HOST_FILE`
do
        if ping -c 1 ${SERVER} |grep "100% packet loss"
        then
                :
        else

        sshpass -p 'password' scp /root/.ssh/id_rsa.pub root@${SERVER}:/tmp
        sshpass -p 'password' scp script1  root@${SERVER}:/tmp
        sshpass -p 'password' ssh root@${SERVER} "chmod 755 /tmp/script1;/tmp/script1"
        fi

done

2. Create 2nd script of name script1 with following:

#!/bin/bash
set -x
if [ ! -d /root/.ssh ]
then
        mkdir /root/.ssh
        chmod 700 /root/.ssh
fi

###  copy the public key

        cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys
        chmod 644 /root/.ssh/authorized_keys

3. Give the execute permissions of both script and create one more file with hosts IPs.-
4. like: 
vim hosts:
10.10.2.240
10.10.2.241
10.10.2.242
10.10.2.243
10.10.2.244
10.10.2.245
10.10.2.246
10.10.2.247
10.10.2.248
10.10.2.249
10.10.2.250
10.10.2.251
10.10.2.252
10.10.2.253

& save it.

5. then run the script as shown below:

# ./script hosts

this will copied the authorized keys in hosts file systems.

6. Now we can scp any files in multiple hosts in parallel from a remote host:
Again for this I have create a script with following:

# vim scp.sh

#!/bin/bash

echo" This command will take the following inputs and run the parallel scp command"

read -p "Please enter the host file where you have all the host: " HostsFile
#read -p "Please enter the key details: " AwsKey
read -p "Please enter the source_file_path: " source_file
read -p "please enter the remote_path_where_to_scp: " remote_path

ssh-keyscan -f $HostsFile >> ~/.ssh/known_hosts

#/usr/bin/parallel-scp -r -h $HostsFile -x "-i $AwsKey -P PORT" -l root ${source_file} ${remote_path}

/usr/bin/parallel-scp -r -h $HostsFile ${source_file} ${remote_path}

& save it.
  I have write here some lines with #prompt, where you can parallel scp with key, other wise you can skip, if you have configure ssh-key less login.

7. then run scp.sh script to copy any file to multiple hosts

# .scp.sh & define your hosts file, source file & remote path.





Comments

Popular posts from this blog

Defining Audit Rules

AIX Install packages, upgrade, patching commands

Oracle Database Quick Installation steps 11g Release 2 for Linux x86-64