Copy multiple files from A to B through passwordless ssh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy multiple files from A to B through passwordless ssh
# 1  
Old 01-22-2013
RedHat Copy multiple files from A to B through passwordless ssh

hi all,

I need to write one script to copy multiple imp files
like
/etc/passwd
/etc/group
/etc/shadow
/etc/printers.conf
from system A, System B and system C to system Z
and I need to execute this script on System Z.
like if system is equal A copy 1 2 3 files to system Z into /A-files
if system is equal B copy 4 5 6 files to system z into /B-files
I have set password less ssh from and to A,B,C to Z and vice versa.

Could you please guide me as how to go about this..?

Thanks in Advance.
Manali.
# 2  
Old 01-22-2013
You can fetch your system name using hostname -s then just use scp to copy the files you want eg:


Code:
case $(hostname -s) in
    systemA)
        scp -p /etc/passwd systemz:/usr/local/backup/A-Files/
        scp -p /etc/group systemz:/usr/local/backup/A-Files/
    ;;
    systemB)
        scp -p /etc/passwd systemz:/usr/local/backup/B-Files/
        scp -p /etc/group systemz:/usr/local/backup/B-Files/
    ;;
esac


Last edited by Chubler_XL; 01-22-2013 at 05:13 PM.. Reason: Formatting - Added -p scp option to preserve modes
# 3  
Old 01-23-2013
Is this code right syntax?
Thanks
# 4  
Old 01-23-2013
This should syntax should be OK for most recent shell versions, if you're having issues try using backticks instead of the $( command ) eg:

Code:
case `hostname -s` in
    systemA)
        scp -p /etc/passwd systemz:/usr/local/backup/A-Files/
        scp -p /etc/group systemz:/usr/local/backup/A-Files/
    ;;
    systemB)
        scp -p /etc/passwd systemz:/usr/local/backup/B-Files/
        scp -p /etc/group systemz:/usr/local/backup/B-Files/
    ;;
esac

# 5  
Old 01-23-2013
Hi Chubler_XL,

Thanks for reply.

I am going to use "uname -n" instead of "hostname -s".
I am really weak in scripting but I want to learn now more more things.

Would you be so kind to guide me on this:
1. how to define variable for systems?
2.where to put this command
ssh systemA 'uname -n' ;which returns the hostname of remote system.

I actually asking you the whole script, but once I execute thhis script , I will build many things through it.

Please suggest Chubler_XL.

Thanks in Advance.

---------- Post updated at 01:15 AM ---------- Previous update was at 12:55 AM ----------

Hello Chubler_XL,

Would you please post me the entire code?
It will be GREAT help for me.

Then I will build on it.

Thanks in advance.
Manali
# 6  
Old 01-23-2013
You could run a script something like this on systemZ

Code:
for SYSTEM in systemA systemB systemC
do
    HOST=`ssh $SYSTEM uname -n`
    [ -d /usr/local/backup/${HOST}-Files/ ] || mkdir -p /usr/local/backup/${HOST}-Files/
    scp -p \
        $SYSTEM:/etc/passwd \
        $SYSTEM:/etc/group \
        $SYSTEM:/etc/shadow \
        $SYSTEM:/etc/printers.conf \
        /usr/local/backup/${HOST}-Files/
done

The backslash on the end of a line allows 1 big line to be split over several, this is a human readability thing.

Here I copy several files with the one scp command, this reduces the scp connection and disconnection overheads. You can think of it like ringing someone and saying one word and hanging up, it would take a long time to tell them a whole sentence.
# 7  
Old 01-24-2013
RedHat

Code:
#!/bin/bash
REM_SERVER=mainserver100

if [ $# -lt 1 ]; then
        echo "Usage:  test.sh REMOTE_SERVER"
exit
fi
SSH_SERVER=`ssh $1 exec uname -n`
if [ $REM_SERVER = $SSH_SERVER ]; then
cd /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/passwd /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/group /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/services /DRFiles/$SSH_SERVER  /etc/profile
scp $SSH_SERVER:/etc/printers.conf /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/profile /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/dfs/dfstab  /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/dfs/sharetab  /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/vfstab  /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/shells  /DRFiles/$SSH_SERVER

$SSH_SERVER
/usr/local/bin/sudo cp /etc/shadow /users/sysmgr
/usr/local/bin/sudo /usr/bin/chmod 755  /users/sysmgr/shadow
scp $SSH_SERVER:/users/sysmgr/shadow /DRFiles/$SSH_SERVER

else
cd /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/passwd /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/group /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/services /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/printers.conf /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/profile /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/vfstab  /DRFiles/$SSH_SERVER
scp $SSH_SERVER:/etc/shells  /DRFiles/$SSH_SERVER

$SSH_SERVER
/usr/local/bin/sudo cp /etc/default/passwd /users/sysmgr/default-passwd
/usr/local/bin/sudo /usr/bin/chmod 755  /users/sysmgr/default-passwd
scp $SSH_SERVER:/users/sysmgr/default-passwd /DRFiles/$SSH_SERVER

$SSH_SERVER
/usr/local/bin/sudo cp /etc/shadow /users/sysmgr
/usr/local/bin/sudo /usr/bin/chmod 755  /users/sysmgr/shadow
scp $SSH_SERVER:/users/sysmgr/shadow /DRFiles/$SSH_SERVER

$SSH_SERVER
/usr/local/bin/sudo cp /usr/local/bin/stats /users/sysmgr
/usr/local/bin/sudo /usr/bin/chmod 755  /users/sysmgr/stats
scp $SSH_SERVER:/users/sysmgr/stats /DRFiles/$SSH_SERVER

fi

we execute this script in this way
Code:
./test.sh servername1
./test.sh servername2
./test.sh servername3

can you help me better this script

Last edited by Franklin52; 01-25-2013 at 05:40 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Passwordless ssh for different user

Hello Folks, I lost touch in ssh key gen topics. I am in need of ssh to a server without password, kindly help me in configuring. I have two servers, server1 with user name apha & server1 with user name beta. I need to ssh to the server2 from server1 with respective users, Manually i... (3 Replies)
Discussion started by: Thala
3 Replies

2. Shell Programming and Scripting

Ssh passwordless authentication

Hey team I have to enable password less authentication betweeen A to B server and A to C server and A to D server. For this I generated a ssh key on server A using ssh-keygen command and copied the key using ssh-copy-id command to B, C and D server. Everything is working fine as of now but... (5 Replies)
Discussion started by: Sandeep_sandy
5 Replies

3. Solaris

Passwordless ssh for root

Hi Experts, I am trying to setup passwordless ssh for root between two of my solaris servers(say A & B). I have exchanged the public keys between both servers. Password less ssh working fine while I try to connect from Server A to Server B. However it is still asking password... (6 Replies)
Discussion started by: sai_2507
6 Replies

4. Ubuntu

Passwordless ssh authentication fails

Unable to set ssh passwordless authentication I am unable to ssh with passwordless authentication from Windows client onto UBuntu server. The ssh version on UBuntu is OpenSSH_5.8p1 Debian-7ubuntu1, OpenSSL 1.0.0e , while SSH on Windows Client is OpenSSH_5.1p1, OpenSSL 0.9.8k. I turned on ssh... (5 Replies)
Discussion started by: tkota
5 Replies

5. UNIX for Advanced & Expert Users

Passwordless ssh

Hi I have created a user on a linux server and created a passwordless ssh key. I've echoed the key into the authorized_keys file for the user. I've added a series of forced commands to the key. From my laptop - logged in as myself - I can ssh into the server as that user and the commands... (3 Replies)
Discussion started by: steadyonabix
3 Replies

6. Shell Programming and Scripting

passwordless ssh

My main concern is, i have to login into 300 linux server and all are having same userid and password. I dont want to create any key for each server to login . Is there a way to run the shell script ? (3 Replies)
Discussion started by: Mani2512
3 Replies

7. UNIX for Dummies Questions & Answers

passwordless ssh

hi, i have tried with passwordless shh in google.. i followed the below steps ... user:~> ssh-keygen -t rsa Enter file in which to save the key (/home/cantin/.ssh/id_rsa):key.txt Enter passphrase (empty for no passphrase): Enter same passphrase again: till this step i... (0 Replies)
Discussion started by: arunmanas
0 Replies

8. Shell Programming and Scripting

ssh passwordless

Hi, I want to login to a remote server and sftp files without password prompting. So, I created private-public key pair as follows: user1@server1.com .ssh]$ ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/home/user1/.ssh/id_rsa): Enter... (7 Replies)
Discussion started by: dips_ag
7 Replies

9. HP-UX

Help on passwordless ssh...

Hi, Can someone help me on ssh-keygen usage...? I used ssh-keygen after which "id.pub" file was generated in system1's > .ssh directory... I copied the same into the remote system system2 > .ssh directory as "authorized_keys" file. Now i tried ssh connection from system 1 to system... (7 Replies)
Discussion started by: EmbedUX
7 Replies

10. AIX

Passwordless authentication via SSH

I am trying to implement passwordless authentication via ssh2. I have used the well documented technique of generating a key pair with a blank passphrase on my client machine, and installing the public key on the destination server (AIX 5.3) in the user's .ssh2 directory. I have used this technique... (1 Reply)
Discussion started by: RegX
1 Replies
Login or Register to Ask a Question