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

I'm calling scp from a script and want it to prompt the user running the script for their password. How do I do that?

share|improve this question

5 Answers 5

Write the prompt first, read the variable, then use the variable to connect with scp

echo -n "username:"
read USERNM

scp $USERNM@whatever

share|improve this answer

In addition to Matt Simmons,

read -p "Username: " USERNM

prompts before reading

read -s -p"Password: " PASS

would read a password, ALTHOUGH, you're not able to pass that to SCP, so it's probably not useful!

Also, put

echo

after your read so that it puts a new line

eg:

read -p "Username: " USERNM; echo
share|improve this answer
    
+1 for the -p flag to read, which I forgot about. Nice solution –  Matt Simmons May 29 '09 at 1:21

an alternative approach, in case you need more sophisticated handling of the interactive app you are spawning, is to use Expect ( http //expect.nist.gov/ )

share|improve this answer

Scp will only ask for a password if it needs one (ie no public key auth available) and thinks it's running in an interactive session. If there's a working public key available this question is moot, so you just need to make sure scp knows it's running in an interactive session. If the user directly runs a script from their shell which directly runs scp, they should be fine.

share|improve this answer
read -p "Username:" USER
scp -l $USER -oPubkeyAuthentication=no

...I think.

If not it will be one of the ssh/scp -o options to force password entry and not use public keys.

share|improve this answer

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.