I am currently using this command. I am on another machine (ip1)

scp file.txt root@ip2:/home/praveen/test.c /home/praveen

The command will prompt for password.

I use it in some shell scripts, and am calling these scripts from some crontab files. So, I can not enter the password every time. Is there any way to disable password asking or some another way to input password from somewhere, so that I don't need to enter it again.

And I cannot use ssh-keygen, because I have to do the actions for these scripts only. When I use ssh-keygen, it will never prompt me for password again. I don't want this. Rest of the things should work as usual.


It was not clear why you could not use ssh-keygen and the subsequent key exchange. I believe this is the recommended practice.

An alternative would be to use the expect command (online man pages). This can eliminate user intervention in the interactive processes. As the name suggests, it expects a prompt, and responds accordingly as specified to it.

Look at this solution of automating SSH using expect. To quote a few lines:

# Look for passwod prompt
expect "*?assword:*"
# Send password aka $password
send -- "$password\r"

The second line specifies the prompt that appears. In this case, it asks for password. The fourth line sends the password to the server.

| improve this answer | |
  • 1
    +1. Never heard of expect. Learned sth! – mike Aug 27 '13 at 9:29
  • 1
    This also leaks the password in the clear, just like Christos' answer. As @Barun is most definitely aware and just trying to answer your question, any method other than public/private keys is a bad idea. – slm Aug 27 '13 at 14:00
  • This means writing the password in a file, which is even worse than having a passwordless private key. – Gilles 'SO- stop being evil' Aug 27 '13 at 21:49

You could use ftp, if that's allowed on the remote server. Here's a sample of shell script I used for this purpose

cd /path/to/local/directory/containing/the/file/to/be/transferred
`ftp -n $HOST <<END_SCRIPT
quote USER $USER
cd /diretory/on/remote/server
put file2transfer.dat 
| improve this answer | |

Since nobody posted an actual answer.

Yes, as denoted in the comments. You need to a create public/private keys pair.

Here are 2 manuals:

You then can utilize ssh-agent to load the keys into memory, in that way you don't have to enter the password you specified for the keys - since they get stored in memory for the current session. You don't have to set a password in first place, but I would recommend to do so, since persons holding your private key could use it to compromise you.

Of course you can delete the keys afterwards or delete your public key from ~/.ssh/authorized_keys on the remote host. He will then prompt you for pw again.

| improve this answer | |

A simple solution is:

echo "password" | scp -r myfile user@remotehost:/home/user/destination 
| improve this answer | |
  • Indeed simple. But I don't know where OP works, or if it's private. But having the pw in plaintext in a script or even in his command history seems a little bit unsecure. – mike Aug 27 '13 at 9:20
  • 1
    Yes this works, but it's a bad idea. The password is sent in the clear, better to use public/private keys! – slm Aug 27 '13 at 13:57
  • Does it work? Most things that prompt for a password directly use the users tty, which is part of why expect was created in the first place. That is beyond the fact that it's a bad idea to start with. – kurtm Oct 13 '13 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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