Usually scp can't be automated, as it asks for password. So we need some program which can simulate the user interaction. One such program is expect script. Using this any user input can be automated.
Without expect:
scp file_name unix_id@server_name:/tmp/ #tmp directory on server where file will be placed.
This approach will required user to enter the password manually. So if the number of servers are very large then it becomes very painful.
This whole process can be automated using 'expect' script. I have automated the complete process using two scripts:
1) shell script: it will take one time input from user and will call expect script.
2) expect script: it will automate the password part. Please note in below shell script that, its calling expect script with some inputs.
--------------------------scp.sh---------
SCP_DIR=`pwd`
echo $SCP_DIR
echo "\n Enter the name of tar file (without extension .tar.gz) :\c"
read TAR_FILE
if [ -f $TAR_FILE.tar.gz ]
then
flag=0
else
echo "tar file $TAR_FILE.tar.gz for deployment doesn't exist at path $SCP_DIR"
exit 0
fi
echo "\n Enter the source servers file :\c"
read SERVER
if [ -f $SERVER ]
then
flag=0
else
echo "File doesn't exist...."
exit 0
fi
echo "Enter UNIX User id : \c"
read USER
echo "Enter Password : \c"
read PASS
for server in `cat $SERVER`
{
echo "Starting scp to server :...... "$server
expect scp.expect $server $USER $PASS $TAR_FILE.tar.gz
echo "\n scp complete......for server $server \n\n\n\n"
}
----------------------------
-------scp.expect---------
#!/usr/bin/expect -f
set SERVER [lindex $argv 0]
set USER [lindex $argv 1]
set PASS [lindex $argv 2]
set TAR_FILE [lindex $argv 3]
#send -- "./qry_sid.sh $KEYVAL\r"
spawn scp $TAR_FILE $USER@$SERVER.corp.apple.com:/tmp/
match_max 100000
expect "*assword*" {
send -- "$PASS\r"
}
sleep 50
#send -- "exit\r"
expect eof
-----------------
How to run the script:
sh scp.sh
rest everything will be taken by these two scripts.