B.Mr.W.
B.Mr.W.

Reputation: 19628

SSH in the most convenient way without typing in the ssh command everytime

I constantly need to log into a remote server and do all kinds of work. I am using my Mac and every time I have to type in a long command to do that.

ssh -i ~/key.pem [email protected]

I am wondering what would be the easiest way to log into the remote server without typing in the command everyday.

Handy apple apps are also welcome

Updated:

1> Thanks to Matt Bryant's answer. I successfully avoided typing in the whole username and host address by modifying ~/.ssh/config (doesn't exist as default) to

Host <ShortName>
    User <UserName>
    HostName <HostIP>
    IdentityFile <KeyPath>

then I could just type this command to avoid typing in the full name of host and full path of the key:

ssh <ShortName> 

2> Anyone knows how to store the pem key or avoid typing in the password if there is no pem key?

Upvotes: 4

Views: 1416

Answers (2)

V H
V H

Reputation: 8587

Hi B MR W yes I am going to post up an expect script since this comes up a lot:

E2A The instructions are for linux There is expect on mac https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/expect.1.html It might be mean messing with the script to meet the criteria of the mac expect I don't have enough knowledge on that but in principal having had a look the below process should work

Which bit was you interested in the ssh-copy-id or a script?

to get a script going you need to

sudo-apt-get install expect or 
sudo yum install expect

Once installed

This is a typical script:

#!/usr/bin/expect -f


set force_conservative 1  
if {$force_conservative} {
        set send_slow {1 .001}
        proc send {ignore arg} {
        sleep .001
                exp_send -s -- $arg
        }
}

;# Validate user input - make sure all fields required are given 
if {$argc >= 1} {
    ;# Setting  password
    set user "MYUSER";
    set supass "MYPASSWORD"

    set host [lindex $argv 0]
  #     set command1 [lindex $argv 2]

   set prompt "(:|#|%|>|\\\$|% |# |> |\\\$ )"
   set prompt1 "(>)"
   set timeout -1
   ;###############################################################

   ;#connect to specified host given by addstaff or globalstaff.
   spawn ssh $user@$host
   expect { 
        "*word:*" {}
        "*(yes/no)?*" {
                      send - "yes\r" 
                  expect "*word:" { }

                  } 
       }          

   send  - "$supass\r"
  expect eof exit -re $prompt

   send -  "sudo bash\r"
   expect   {
                 "*word:*" {
                         send  "$supass\r"
                         expect  eof exit -re $prompt
                 }
        "*" {  
              expect eof exit -re $prompt
        }

   }

    send  - "whoami\r"
        expect eof exit -re $prompt

    interact 

    ;#send  - "$command\r"
    ;#  expect eof exit -re $prompt

    ;#Logout of current ssh session
    ;#send  - "exit\r"
    ;#expect eof exit -re $prompt

    ;#send  - "exit\r"
    ;#expect eof exit -re $prompt


} else {
  send - "echo Sorry require user host command \n\n\r";
  exit
} 

If you have noticed I have commented out the commands which is not being sent - its using interact mode to allow you to actually log in without a password, user password defined at the top of the script... Once logged in you type as you would per normal ssh

Here is me running it locally:

 whoami
myuser
./ssh-connection.exp localhost
spawn ssh myuser@localhost
myuser@localhost's password: 
Welcome to Ubuntu 12.04.2 LTS (GNU/Linux XXXX)

 * Documentation:  https://help.ubuntu.com/

Last login: Sat Sep  7 20:19:53 2013 from localhost
sudo bash
This is BASH 4.2- DISPLAY on localhost:0.0

Sat Sep  7 20:25:09 BST 2013
whoami
[20:25 myuser@myuser-DP ~] > sudo bash
whoami
This is BASH 4.2- DISPLAY on localhost:0.0

Sat Sep  7 20:25:09 BST 2013
[20:25 myuser@myuser-DP ~] > whoami
root
[20:25 myuser@myuser-DP ~] > whoami
root

Within the script it also does a sudo bash which is why it reconnects me to localhost and I become root

Upvotes: 1

Matt Bryant
Matt Bryant

Reputation: 4961

Use the ~/.ssh/config file. Try adding the following to it:

Host server                                                                                                  
    User ubuntu
    HostName 255.255.255.255

This will allow you to login using ssh -i ~/key.pem server. If you look up ssh config, there are many other settings that will allow you to simplify and enhance your ssh experience.

Upvotes: 6

Related Questions