Linux – How to write a script to connect to an ssh server with the credentials?

bashlinuxperlremote-accessremote-server

I want to automate password entry when using SSH to log on to a remote linux server.

I made an executable bash script with

#! /bin/bash

ssh user@machine.remote.host

But is there a way to automate my password entry?

Best Answer

ssh-keygen # generate a pair of RSA keys
scp ~/.ssh/id_rsa.pub user@machine.remote.host:id_rsa.tmp # copy the public key to remote host
ssh user@machine.remote.host # ssh to remote server, this time it requires password
cat id_rsa.tmp >> .ssh/authorized_keys # add the public key to authorized_keys

Then you could log off and log on without passwords.