« HIDE user input using Batch Script – PHP - CLI | Main | Automate FTP download using Shell Script »

Automate SCP command using Shell Script

Below shell script will automatically do the scp ( Secured Copy ) process with out asking the password from the user. Basically automating the secured copy command.

* We can put this script in cron job for daily download or uploads.

* Script automatically manages password entering.

* Login to the new server will popup the Secured Key window asking YES or NO. Script automatically do yes and will continue the copy process.


File Name: scp_dump.sh

*******************************************************************************************************************

#!/usr/bin/expect -f

# connect via scp
spawn scp "user@example.com:/home/santhosh/file.dmp" /u01/dumps/file.dmp
#######################
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "PASSWORD\r"
}
}
interact

*******************************************************************************************************************

Output:

TrackBack

TrackBack URL for this entry:
http://blogs.oracle.com/mte1521/mt-tb.cgi/6061

Comments (14)

I love you ! Thank you very much...

Kian Shokouhi:

Thank you soo much for this amazing script :))))

Thank you! A little more advice life this, and I'll soon be able to automate everything I do!

it's working :D thanks man ;)

Swaroop:

Its not working for me. It is giving these errors:
spawn: not found
expect: not found
-re: not found
exp_send: not found
exp_continue: not found

shweta:

This is not working. Kindly help

PSB error log:
shwetaf.sh[4]: spawn: not found.
shwetaf.sh[6]: expect: not found.
shwetaf.sh[7]: -re: not found.
shwetaf.sh[8]: exp_send: not found.
shwetaf.sh[9]: exp_continue: not found.
shwetaf.sh[10]: 0403-057 Syntax error at line 11 : `}' is not expected.

santhosh tirunahari:

Cause of above errors

1. /usr/bin doesn't contain the expect utility installed in the machine.

2. You must run the script with ./ command as show in the thread screen shot, not with sh command.

madeinloveyou:

thank you ...it 's a very helpfull script !!!

sreekanth:

dude,
u are awesome...... been trying doing this by RSA ssh keys and all....
thanks this is lot easier.

frendy:

i got this error :

[root@app1 home]# ./scpp2
-bash: ./scpp2: /usr/bin/expect: bad interpreter: No such file or directory

jordi:

Thanks a lot for this amazing script. Really useful to me.

TeRReF:

Do realize that this isn't a very secure method on a multi user machine.
Any user with su/ sudo capabilities will be able to read the password from the script...

KindLibrarian:

To put it into a cron job successfully, I had to get rid of the 'interact' statement. Continuing the example where you are transferring a file named file.dmp, the following cycles until the transfer is done (up to 600 seconds) and then exits.



#!/usr/bin/expect -f

# connect via scp
set timeout 600
spawn scp "user@example.com:/home/santhosh/file.dmp" /u01/dumps/file.dmp
#######################
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "PASSWORD\r"
exp_continue
}
-re "file.dmp.*" {
sleep 5
send "\r"
exp_continue
}
}
exit

Magnus:

Thank you very much for this splendid script.

For you who are having problems getting this to work. Make sure you have 'Expect' installed.

In Linux distributions with 'yum', run (as root):
# yum install expect

Then make sure the path to expect is correct in your script by typing:

$ which expect
/usr/bin/expect

The path returned is to be put in the beginning of the script at
#!/usr/bin/expect -f

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About This Entry

This page contains a single entry from the blog posted on August 29, 2008 2:44 AM.

The previous post in this blog was HIDE user input using Batch Script – PHP - CLI.

The next post in this blog is Automate FTP download using Shell Script.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type and Oracle