Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to transfer some files using SCP in a shell script, but I am being prompted for my password on the remote machine.

Can I use expect with scp to enter the password so I don't have to type it in all the time?

share|improve this question
up vote 5 down vote accepted

You don't. Use a key instead.

share|improve this answer
i know but in this task i want use expect command – Mohammad AL-Rawabdeh Dec 5 '10 at 7:02
Using expect to do this is terribly insecure -- If you want an answer about using expect you are going to have to justify why you think expect is the right tool or the job. – James Dec 5 '10 at 10:45
A reason could be that he can't put his pubkey on the server or it doesn't accept publickey. Actually, scp must read the secret key too, and usually the only security on it is the 700 on the .ssh directory. – ott-- Jul 27 '12 at 21:22

Yes, you can.

/usr/bin/expect <<EOD
spawn /usr/bin/scp -l 200 file.ext  user@server://path/on/server/
expect "password:"
send "YourPassword\r"
expect "\r"
send "\r\n"
share|improve this answer
As written, this means that the user has to create/edit a shell script every time, rather than merely writing their password, which doesn't feel like a good substitute. :) Perhaps you could improve this script by using $@ to pass the command line parameters through to scp? – nickgrim Apr 26 '13 at 16:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.