How can I ssh to a remote server with an automated password and no key?

I have a remote server. I planned to use public keys to connect over ssh without typing the password but the remote system rejects public key authentication.

So what is the best way to do it? What about using sshpass?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

EDIT

From the link that I used as a reference, you can find more options.

If you have sshpass installed, you can automate the ssh connection so you don’t need to type your password for each machine:

SSHPASS='password'
  sshpass -e  your commands here

1. Install sshpass

This is a nifty little program that allows you to pass an ssh password as a command line parameter. This is, obviously, not a very secure solution and I highly recommend you read the “Security Considerations” section of man sshpass.

Anyway, it is probably available in your distribution’s repositories, on Debian-based systems it can be installed with

sudo apt-get install sshpass

I can’t check since I don’t have a RedHat based machine but as far as I can tell from searching here, it should be installable on Fedora with

sudo yum install sshpass

Once you have it installed, you can simply run

SSHPASS='password'
  sshpass -e your commands

The -e option tells sshpass to get the password from the SSHPASS variable. This is a bit more secure than giving it as a parameter with the -p option.

IMPORTANT:

This will fail silently if the server you are connecting to is unknown, if its public key is not stored in your machine. If this does not seem to be working, just connect once (ssh or scp) to the remote machine and accept its public key.

References

script to automate scp in network

Method 2

You can use sshpass if you are the only user of you system. But it’s definitely a very bad idea. Everybody could get your passord if he has a shell access to your machine.

Once you can’t exchange public keys. I suggest you to use expect. You’ll need to secure an exp file containing your password. It will be stored on your hard disk, so every user having the sudo or root access can hack it. But at least it’s really more secure than sshpass.

Here’s how to do it

Method 3

As already described in other answers, I also use sshpass but I combine it with the read command to store my password in an temporary environment variable. This way my password is never written anywhere in clear. Here is the one line command I use:

read -s PASS; sshpass -p $PASS ssh <user>@<host adress>

After that you have to enter your password (nothing appears on the screen) and then pressing enter will open the connection.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x