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:


#!/usr/bin/expect -f

# connect via scp
spawn scp "" /u01/dumps/file.dmp
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
-re ".*sword.*" {
exp_send "PASSWORD\r"




I love you ! Thank you very much...

Posted by Gregory on August 28, 2008 at 09:32 PM PDT #

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

Posted by Kian Shokouhi on February 18, 2009 at 11:29 PM PST #

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

Posted by iGuide on February 28, 2009 at 09:03 AM PST #

it's working :D thanks man ;)

Posted by xdanijel on March 12, 2009 at 10:26 PM PDT #

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 on March 23, 2009 at 09:46 PM PDT #

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

Posted by shweta on April 23, 2009 at 05:03 PM PDT #

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 on April 26, 2009 at 03:59 PM PDT #

thank you 's a very helpfull script !!!

Posted by madeinloveyou on May 11, 2009 at 04:37 PM PDT #

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

Posted by sreekanth on May 27, 2009 at 12:47 PM PDT #

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

Posted by frendy on May 31, 2009 at 05:20 PM PDT #

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

Posted by jordi on June 15, 2009 at 06:09 PM PDT #

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 on June 17, 2009 at 06:11 AM PDT #

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 "" /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 on June 18, 2009 at 05:18 AM PDT #

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 on June 19, 2009 at 04:56 PM PDT #

Spiffy, but why store your password for a "secure" file transfer in a script? Wouldn't it be a better to install a public key on the target hosts you are wishing to push/pull files to/from?

Posted by John Ellis on August 19, 2009 at 11:54 PM PDT #

Wouldn't you be better off generating an ssh public/private key pair and using that in conjunction with the -i option? Still the expect -f is one way of doing it I suppose.

Posted by Bill Bixby on September 13, 2009 at 07:53 AM PDT #

Thanks you. Really useful to me.

Posted by cHiRo on October 15, 2009 at 07:43 AM PDT #

I absolutely adore this blog :D definitely going to need to remember to add this to my bookmarks.

Posted by malware removers on December 12, 2009 at 06:24 AM PST #

how to use this code because i am beginner level in php please guide me………any body can help me..

Posted by Damp Europe on January 11, 2010 at 12:08 AM PST #

Hi, I want to copy files from one server to another server using shell script with SCP command.Can anyone give me a solution to pass password as parameter.So the prompt wont ask password

Posted by Nathan on April 21, 2010 at 12:39 PM PDT #

THANKS!!!!!!!!!! Awesome!

Posted by bubba on July 30, 2010 at 02:55 AM PDT #

I seriously grasp your energy you've given to this entry. I'm anticipating for your forthcoming piece.Please that I recently came across this an informative and interesting blog. I just now hope you might make another post affiliated with this. It's well worth reading.

Posted by Sandra Fichter on August 05, 2010 at 10:26 AM PDT #

Hi i am getting the following error. i am using ubuntu 10.4 ./ line 2: spawn: command not found ./ line 4: expect: command not found ./ line 5: -re: command not found ./ line 6: exp_send: command not found ./ line 7: exp_continue: command not found ./ line 8: syntax error near unexpected token `}' ./ line 8: `}' any help

Posted by Prasoon on September 02, 2010 at 10:55 PM PDT #

I aren't able to understand it to work, any kind of thoughts?

Posted by Aron Billips on September 25, 2010 at 11:35 PM PDT #

Prasoon, this is not a bash script. Run it with ./SCRIPTNAME instead of bash SCRIPTNAME that will fix your problem. I am trying to pass variables to this script from a script that will already be running. Any suggestions?

Posted by Jeffrey Simon on November 02, 2010 at 03:58 PM PDT #

Howdy, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam comments? If so how do you prevent it, any plugin or anything you can advise? I get so much lately it's driving me mad so any assistance is very much appreciated.

Posted by Lilly Pulitzer on November 19, 2010 at 02:06 PM PST #

Works beautifully

Posted by Christopher Mikos on December 15, 2010 at 05:50 AM PST #

In large file transfers where timeouts seem to occur add this to the looping... } timeout { exit } -re . { exp_continue } eof { exit } Should keep it running while the scp reports to the screen the progress.

Posted by David Fleming on February 22, 2011 at 02:24 AM PST #

Great script!! yet 1 problem leads to another... the wildcards seems not work :( something like
spawn scp "" /u01/dumps/*dmp

Script stops even if *.dmp files are present.
any clues? thanks

Posted by rommel on June 30, 2011 at 03:59 PM PDT #

Hi i am getting the following error:spawn_id:spawn id exp6 not open while executing "interact"(file"./" line 14)
please help me.

Posted by guest on July 10, 2011 at 08:16 PM PDT #

Need to install EXP

Posted by Santhosh on July 10, 2011 at 08:34 PM PDT #

Thanks! Good stuff. KindLibrarian's comment was very helpful as well.

Posted by guest on July 14, 2011 at 04:45 PM PDT #

Great script!!! Thank you so much for sharing!

Posted by Jana Harper on July 19, 2011 at 05:26 AM PDT #

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post…

Posted by live cricket streaming on July 25, 2011 at 03:02 AM PDT #

finally works for me manually, thanks!!
yet another stops me!
why it isn't running on cron?? does expect run properly on crons?
Need help... again :)

Posted by rommel on July 26, 2011 at 12:58 PM PDT #

Great script, worked for me beautifully.

For people who are getting errors - 'expect not found', you need to run the following to install the missing component

yum install expect

once its installed, try again it should work.
hope this helps. thanks

Posted by Santosh Nair on August 25, 2011 at 11:32 AM PDT #

Great, worked wonderfully for me.

For people who are getting error - '/usr/bin/expect: bad interpreter: No such file or directory', you need to install the missing component using the following

yum install expect

hope this helps.

Posted by Santosh Nair on August 25, 2011 at 12:03 PM PDT #

getting some evaluation error

parse error after spawn token

Posted by guest on September 12, 2011 at 10:01 PM PDT #


Unfortunately expect is not installed on system, and also dont have root access to install it . can u please help how can i automate shell script for rsync contents from one server to another server.

Best Regards,
Bipin Bahuguna

Posted by guest on December 23, 2011 at 01:33 AM PST #

Thanks! very helpful. It works for me.

Posted by viercoles on January 01, 2012 at 08:13 PM PST #

excellent! simple and clear. THANKS.

Posted by guest on January 16, 2012 at 04:01 PM PST #

thanks! it works.

Posted by guest on January 16, 2012 at 04:02 PM PST #

Thank you very very very very very very very much !!! I am a newbee in this scripting world... your script helped me a lot !

Posted by guest on August 14, 2012 at 06:16 AM PDT #

simplified form,

/usr/bin/expect <<EOD
# connect via scp
spawn scp "" /home/nathan/
expect -nocase "password:"
send "yourpassword\r"
expect eof #wait till the end of the file

Posted by guest on August 24, 2012 at 02:43 AM PDT #

Actually the script din't work for me initially :(
But I modified it a little and it worked for me.

Probably you forgot to mention the we need to replace the word PASSWORD in exp_send "PASSWORD\r" with the actual password. Or is it understood?

Hope this will help someone getting errors in the script.

Manohar Bhattarai

Posted by guest on September 27, 2012 at 03:29 AM PDT #

Thank you. it helped me alot.
Awesome script.....

Posted by Reddy on December 09, 2012 at 01:57 AM PST #

hi, as some of the users me too getting the error:

./scp_transfer_files: line 6: spawn: command not found
couldn't read file "{": no such file or directory
./scp_transfer_files: line 9: -re: command not found
./scp_transfer_files: line 10: exp_send: command not found
./scp_transfer_files: line 11: exp_continue: command not found
./scp_transfer_files: line 12: syntax error near unexpected token `}'
./scp_transfer_files: line 12: `}'

I am running it as ./scp_transfer_files

When i check my shell ...

echo $SHELL

Also i have the file /usr/bin/expect

What shall i do to execute this ?!!!

Please assit

Posted by Pratik Mehta on March 14, 2013 at 01:52 PM PDT #

can you please explain each line..

Posted by guest on June 06, 2013 at 07:23 AM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed



« September 2015