Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
add comment

2 Answers

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
4  
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
add comment

Yes, you can.

#!/usr/bin/bash
/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"
EOD
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
add comment

Your Answer

 
discard

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.