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.
You can fetch your system name using hostname -s then just use scp to copy the files you want eg:
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
Is this code right syntax?
Thanks
This should syntax should be OK for most recent shell versions, if you're having issues try using backticks instead of the $( command ) eg:
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
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:
- 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
You could run a script something like this on systemZ
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.
#!/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
./test.sh servername1
./test.sh servername2
./test.sh servername3
can you help me better this script
There are a few lines in this that concern me:
scp $SSH_SERVER:/etc/services /DRFiles/$SSH_SERVER /etc/profile
The /etc/profile on the end of this looks wrong scp requires a floder as a destination when more that 1 file/folder is supplied and /etc/profile is typically a file.
$SSH_SERVER
What is this line supposed to do, it appears a few times in the script.
Try to avoid duplication if a line is in both the REM_SERVER = SSH_SERVER section and the else part try and do it before the if eg replace:
if (test); then
step A
step B
step C
step D
step E
step F
else
step A
step B
step G
step D
step E
step F
fi
with:
step A
step B
if (test); then
step C
else
step G
fi
step D
step E
step F
scp $SSH_SERVER:/etc/services /DRFiles/$SSH_SERVER /etc/profile
is typo error.
$SSH_SERVER
is also wrong.
RIGHT ONE IS THIS UPDATED ONE:-
#!/bin/bash
MACHINE=mainserver
for server in "system1" "system2" "system3" "system4" "system5"
SSH_SERVER=`ssh $server exec uname -n`
echo "Copying file from $SSH_SERVER......."
if [ $SSH_SERVER = $MACHINE ]; then
scp -q $SSH_SERVER:/etc/passwd /DRFiles/$SSH_SERVER
scp -q $SSH_SERVER:/etc/group /DRFiles/$SSH_SERVER
scp -q $SSH_SERVER:/etc/services /DRFiles/$SSH_SERVER /etc/profile
scp -q $SSH_SERVER:/etc/printers.conf /DRFiles/$SSH_SERVER
scp -q $SSH_SERVER:/etc/profile /DRFiles/$SSH_SERVER
scp -q $SSH_SERVER:/etc/dfs/dfstab /DRFiles/$SSH_SERVER
scp -q $SSH_SERVER:/etc/dfs/sharetab /DRFiles/$SSH_SERVER
scp -q $SSH_SERVER:/etc/vfstab /DRFiles/$SSH_SERVER
scp -q $SSH_SERVER:/etc/shells /DRFiles/$SSH_SERVER
else
scp -q $SSH_SERVER:/etc/passwd /DRFiles/$SSH_SERVER
scp -q $SSH_SERVER:/etc/group /DRFiles/$SSH_SERVER
scp -q $SSH_SERVER:/etc/services /DRFiles/$SSH_SERVER
scp -q $SSH_SERVER:/etc/printers.conf /DRFiles/$SSH_SERVER
scp -q $SSH_SERVER:/etc/profile /DRFiles/$SSH_SERVER
scp -q $SSH_SERVER:/etc/vfstab /DRFiles/$SSH_SERVER
scp -q $SSH_SERVER:/etc/shells /DRFiles/$SSH_SERVER
fi
done
Now please tell me how to copy /etc/shadow file from remote machine which is owned by root , because before copying it ask for sudo password.
now is there any way to avoid this password and copy these files owned by root.
the one way which i thought would be helpful, is to create script at all remote machines to copy these files which are owned by root like, (/etc/shadow) to home directory of that common user from which we are doing passwordless ssh, then change file's perssion to 755 and then let our this script copy those files. but is there any other possible?
Please advise.
Thanks,
Manali
---------- Post updated at 11:21 AM ---------- Previous update was at 01:33 AM ----------
Please Reply...