20

From the terminal I type: ssh user@ip and then it prompts for a password.
Is there a way to specify the password in the ssh command itself?

  • answer is at https://stackoverflow.com/questions/4780893/use-expect-in-a-bash-script-to-provide-a-password-to-an-ssh-command – gcb Feb 08 '23 at 21:07

4 Answers4

18

Use sshpass, one of two forms:

sshpass -ffilename ssh user@ip   # prefer this
sshpass -pPa5sw0rd ssh user@ip   #  avoid this

where your password is in the first line of the file filename or it is literally Pa5sw0rd. Notes:

  • In the manual there is no space after -p or -f, but at least sshpass 1.06 in my Debian 10 allows it; your sshpass may or may not.
  • If your password contains characters your shell will interpret (like $, ' or ;) then you should quote it properly in the command line (but not in the file).
  • Avoid -p. Use chmod 600 filename to make the file private (root will still be able to access it though). Read about security considerations in the manual.
1

The correct way to do this, is to switch from password authentication to a public/private key pair. This typically needs no reconfiguration at all and is quite easy.

Step 1: If you do not have a key, create one: ssh-keygen will do that for you

Step 2: Authorize this key on the remote host: Run ssh-copy-id user@ip once, using your password

Step 3: From now on ssh user@ip will no longer ask for your password

Eugen Rieck
  • 20,271
  • Thats correct. But the servers that i am working with are currently having password based authentication. And its really irritating to type password each time they prompt – Rohit sharma Nov 26 '20 at 10:03
  • 2
    from a security perspective this is the correct way to do it. but the OP asked "How to specify password in ssh command". – Zina Nov 26 '20 at 14:11
  • Nearly ALL servers allow key authentication in addition to password authentication - so you use the password authentication (via ssh-copy-id) to enable the key. – Eugen Rieck Nov 28 '20 at 16:07
  • Windows, doesn't have ssh-copy-id, type $env:USERPROFILE\.ssh\id_rsa.pub | ssh {IP-ADDRESS-OR-FQDN} "cat >> .ssh/authorized_keys" (from https://chrisjhart.com/Windows-10-ssh-copy-id/)` – js2010 Oct 20 '23 at 16:12
0

Here's a solution that uses clarkwang/passh. It works on macOS (tested on 13.4.1) as well as Linux, FreeBSD, OpenWRT and some others.

Download & compile

Precompiled binaries don't exist at this time, but just a few commands get it installed:

git clone https://github.com/clarkwang/passh && cd passh
cc -o passh passh.c
cp passh /usr/local/bin

Use

passh -c1 -C -t10 -T -p hunter2 ssh -t user@host 'echo $HOSTNAME'

Options explained

  • -c1 only make 1 attempt at password login
  • -C exit if password login fails
  • -t10 means timeout after 10 seconds
  • -T exit if timeout occurs
  • -p specifies the password to input

Other options

expect

Here's an answer that uses expect. I prefer passh because it doesn't require a HEREDOC or separate script, and works on embedded platforms like OpenWRT that don't always ship with expect.

sshpass

sshpass was removed from Homebrew, and is a bit convoluted to install on macOS now. The passh author has also documented some details explaining why it's broken at passh/sshpass-broken.md.

luckman212
  • 239
  • 1
  • 7
0

While this might be a ´workarround you can get the job of logging in fast done pretty easily by pasting the password in your clipboard and paste it when prompted. In a batchfile on windows this would look something like this:

clip <your-password>
ssh <user>@<server>

When you run the script just hit Ctrl + V and you're in.