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:

Comments (14)
I love you ! Thank you very much...
Posted by Gregory | August 29, 2008 4:32 AM
Posted on August 29, 2008 04:32
Thank you soo much for this amazing script :))))
Posted by Kian Shokouhi | February 19, 2009 7:29 AM
Posted on February 19, 2009 07:29
Thank you! A little more advice life this, and I'll soon be able to automate everything I do!
Posted by iGuide | February 28, 2009 5:03 PM
Posted on February 28, 2009 17:03
it's working :D thanks man ;)
Posted by xdanijel | March 13, 2009 5:26 AM
Posted on March 13, 2009 05:26
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
Posted by Swaroop | March 24, 2009 4:46 AM
Posted on March 24, 2009 04:46
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.
Posted by shweta | April 24, 2009 12:03 AM
Posted on April 24, 2009 00:03
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.
Posted by santhosh tirunahari | April 26, 2009 10:59 PM
Posted on April 26, 2009 22:59
thank you ...it 's a very helpfull script !!!
Posted by madeinloveyou | May 11, 2009 11:37 PM
Posted on May 11, 2009 23:37
dude,
u are awesome...... been trying doing this by RSA ssh keys and all....
thanks this is lot easier.
Posted by sreekanth | May 27, 2009 7:47 PM
Posted on May 27, 2009 19:47
i got this error :
[root@app1 home]# ./scpp2
-bash: ./scpp2: /usr/bin/expect: bad interpreter: No such file or directory
Posted by frendy | June 1, 2009 12:20 AM
Posted on June 1, 2009 00:20
Thanks a lot for this amazing script. Really useful to me.
Posted by jordi | June 16, 2009 1:09 AM
Posted on June 16, 2009 01:09
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...
Posted by TeRReF | June 17, 2009 1:11 PM
Posted on June 17, 2009 13:11
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
Posted by KindLibrarian | June 18, 2009 12:18 PM
Posted on June 18, 2009 12:18
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
Posted by Magnus | June 19, 2009 11:56 PM
Posted on June 19, 2009 23:56