0

So currently, the "traditional" way to connect through SSH is the following one:

ssh USERNAME@HOST -p PORT

I would like to create a script in which I will include all the appropriate information (username, password, host, port) and when I run it, it will connect automatically leading me to the shell of the host machine.

I don't care about the language you will use (e.g. Python, Perl...). I have tried to create it using Expect but unfortunately it didn't work for me.

  • is for your self? create a bashrc alias – lese Jan 21 '16 at 16:17
  • 7
    A better option might be to use the user's ~/.ssh/config file + key-based authentication – steeldriver Jan 21 '16 at 16:21
  • you are right, I didn't think about it – lese Jan 21 '16 at 16:22
2

For automatic login, the way to go is using key-based authentication.

A nice tutorial here

You can then use the default one (the one in the ~/.ssh/id_rsa ) or use another key passing -i MyKeyFile parameter to ssh.

0

Use sshpass

$ sudo apt-get install sshpass

Create your script

#!/bin/bash

user="user"
psswd="password"
port="22"
host="domain.com"

sshpass -p $psswd ssh $user@$host -p $port

Execute

$ bash connect_script.bash
Welcome to Ubuntu ..... LTS (.......)

* Documentation:  https://help.ubuntu.com/
New release '14.04.1 LTS' available.
Run 'do-release-upgrade' to upgrade to it.

You have new mail.
Last login: Thu Jan 21 11:39:35 2016 from x.x.x.x
user@host:~$ 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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