1

I am creating a bash script that trying to connect to a remote server, but it requires to enter a password, I wrote the following script:

ssh HostIP expect "password:" send "password"

but it connects and gives "user@HostIP's password:", so the send command is not writing any password to the screen....what should I do to make it work?

2
  • 1
    I think you should generate a RSA on your machine (id_rsa) and publish your public key to your server (id_rsa.pub). This will avoid you to provide password in clear to the server. Commented Jan 28, 2018 at 12:07
  • @Newmips, I think I too explained the same thing in answer :) Let's see what OP does on this, cheers. Commented Jan 28, 2018 at 12:16

2 Answers 2

2

Writing passwords in file(s) or scripts is NEVER a good practice. Why don't you give a try to password less authentication from one server to another.

Simple steps:

I- generate the RSA public and private keys from command ssh -keygen -t rsa to your server1.

II- Now create .ssh directory in your another server(server2)'s home dorectory with correct permissions.

III- Create file named authorized_keys on server2.

IV- Open file named authorized_keys on server2 and copy file named id_rsa.pub from server1 to server2.

V- Set permissions to 640 to ~/.ssh/authorized_keys now.

VI- try to login to server2 now by doing:

ssh user@server2

Here is a nice link which could tell you about same too.

https://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/

Once passwordless authentication is set from server1 to server2 with ssh then you could simply execute all ssh commands in your script which you want to run on another server.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do it with sshpass like :

sshpass -p **your_password**  ssh user@HostIP

If sshpass is not already installed, you can install it and make the first connection in bash console for "the yes confirmation"

1 Comment

Recommend pass [passwordstore.org/], using with encrypted password.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.