1

This question already has an answer here:

Can I input the password as a parameter to SCP rather than be prompted for it?

How can I save password,when copy a file using SCP? This question is similar, but answers deal with doing away with SSH password completely.

marked as duplicate by Jakuje, phk, DarkHeart, Jeff Schaller, Toby Speight May 23 '17 at 16:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Server Fault might be a better forum to ask this question. You can have a look at this question. Anyway, having a password to ssh and, even worse storing it somewhere, is a very bad practice and may bring you much more trouble than the one you have now, i.e. typing each time your password. – nikolas May 23 '17 at 9:46
  • 1
    Why can you not use ssh keys? They are a far more secure way to do this. Problem with passwords as arguments is they are visible to everyone else on your local machine (via the process list) as well as being written plain text your your shells history. – Michael Daffin May 23 '17 at 9:50
4

There are several options:

  1. using sshpass

Execute the command:

sshpass -p 'password' scp filename user@host:
  • Install sshpass:

    • Ubuntu:

    sudo apt install sshpass

sshpass man

sshpass - noninteractive ssh password provider

sshpass is a utility designed for running ssh using the mode referred to as "keyboard-interactive" password authentication, but in non-interactive mode.

ssh uses direct TTY access to make sure that the password is indeed issued by an interactive keyboard user. Sshpass runs ssh in a dedicated tty, fooling it into thinking it is getting the password from an interactive user.

The command to run is specified after sshpass' own options. Typically it will be "ssh" with arguments, but it can just as well be any other command. The password prompt used by ssh is, however, currently hardcoded into sshpass.

  1. Using expect (or other script)

write a script which will inject the password once it will identify the ssh password prompt

  1. Using ssh with key-pair (instead of using password)

IMHO - This is the most secure way, and it doesn't require using passwords.

Using public/private key in order to perform ssh or scp operation without having to enter a password.

  • More info can be found in this answer

  • Detailed information can be found here

2

There is already an answer on how to do it, so I will focus on why not.

  • If you pass a password on the command line then it can be snooped by a local user, using ps or any other process lister.
  • Creating passwords that are hard to guess and easy to remember is hard: so don't use passwords with ssh.

Instead use a public/private key, and use an key agent to temporarily remember your key.

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