4

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 ubuntu@255.255.255.255

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?

4

2 回答 2

5

使用 ~/.ssh/config 文件。尝试添加以下内容:

Host server                                                                                                  
    User ubuntu
    HostName 255.255.255.255

这将允许您使用ssh -i ~/key.pem server. 如果您查看 ssh 配置,还有许多其他设置可以让您简化和增强您的 ssh 体验。

于 2013-09-07T17:59:05.833 回答
1

嗨 B MR W 是的,我将发布一个期望脚本,因为这出现了很多:

E2A 指令适用于 linux 在 mac 上有期望https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/expect.1.html 这可能意味着弄乱脚本来满足mac的标准期望我对此没有足够的了解,但原则上看过下面的过程应该可以工作

您对 ssh-copy-id 或脚本的哪一点感兴趣?

要使脚本运行,您需要

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

一旦安装

这是一个典型的脚本:

#!/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
} 

如果您注意到我已经注释掉了未发送的命令 - 它使用交互模式允许您在没有密码的情况下实际登录,在脚本顶部定义的用户密码......一旦登录,您就可以输入会按照正常的 ssh

这是我在本地运行它:

 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

在脚本中它还执行 sudo bash 这就是为什么它将我重新连接到 localhost 并且我成为 root

于 2013-09-07T19:26:58.330 回答