为 ssh 连接创建别名

Kar*_*son 15 ssh bash alias

我想加快连接到特定服务器的速度。

我有服务器让我们说:

123.123.123.1
123.123.123.2
123.123.123.3
Run Code Online (Sandbox Code Playgroud)

我通常连接以下内容:

ssh -p 12345 my_user@123.123.123.1
Run Code Online (Sandbox Code Playgroud)

这很痛苦,因为服务器之间的唯一区别是 ip 的最后一个数字。

我尝试了以下代码:

alias ssht='{ ip=$(cat -); ssh -p 12345 my_user@"123.123.123.$ip"; }<<<'
Run Code Online (Sandbox Code Playgroud)

但是我收到一个错误:

karl@karls-laptop ~/scripts $ ssht 1
Pseudo-terminal will not be allocated because stdin is not a terminal.
Run Code Online (Sandbox Code Playgroud)

有没有办法让这个工作?

Jak*_*uje 59

使用预期的方式并将选项和别名写入~/.ssh/config

Host 1
  Port 12345
  User my_user
  HostName 123.123.123.1

Host 2
  Port 12345
  User my_user
  HostName 123.123.123.2
Run Code Online (Sandbox Code Playgroud)

等等...

然后只使用连接

ssh 1
ssh 2
...
Run Code Online (Sandbox Code Playgroud)

  • 您仍然可以为 `Host 123.123.123.*` 提供一个 ssh 配置行来指定端口和用户名! (2认同)

hee*_*ayl 20

这需要一个函数——简单而健壮,而alias在这种情况下,它是脆弱的。

这样的事情应该做:

function ssht () {
    [[ $1 =~ ^(1|2|3)$ ]] || { echo 'Not a valid last octet value !!' && return ;}
    ip=123.123.123.$1
    ssh my_user@"$ip" -p 12345
}
Run Code Online (Sandbox Code Playgroud)

该条件[[ $1 =~ ^(1|2|3)$ ]]确保您已输入 1、2、3 之一作为第一个参数(忽略任何尾随参数)。

现在,您可以将所需的最后一个八位字节作为第一个参数:

ssht 1
ssht 2
ssht 3
Run Code Online (Sandbox Code Playgroud)

把它放在你~/.bashrc的任何交互式会话中。

  • “这需要一个函数”我使用相同的经验法则,如果涉及参数,请使用函数而不是别名。 (2认同)
  • 虽然这有效,但它有点矫枉过正,而且没有必要。只需使用内置的 `~/.ssh/config` 文件 (2认同)

mur*_*uru 16

您可以在~/.ssh/config. 来自man ssh_config

PATTERNS
     A pattern consists of zero or more non-whitespace characters, ‘*’ (a
     wildcard that matches zero or more characters), or ‘?’ (a wildcard that
     matches exactly one character).  For example, to specify a set of
     declarations for any host in the “.co.uk” set of domains, the following
     pattern could be used:

           Host *.co.uk

     The following pattern would match any host in the 192.168.0.[0-9] network
     range:

           Host 192.168.0.?
Run Code Online (Sandbox Code Playgroud)

结合:

HostName
    Specifies the real host name to log into.  This can be used to
    specify nicknames or abbreviations for hosts.  If the hostname
    contains the character sequence ‘%h’, then this will be replaced
    with the host name specified on the command line (this is useful
    for manipulating unqualified names).  The character sequence ‘%%’
    will be replaced by a single ‘%’ character, which may be used
    when specifying IPv6 link-local addresses.
Run Code Online (Sandbox Code Playgroud)

所以,在你的~/.ssh/config,放:

Host ?
    Hostname 123.123.123.%h
    Port 12345
    User my_user
Run Code Online (Sandbox Code Playgroud)

然后:

$ ssh -v 1
OpenSSH_7.4p1, LibreSSL 2.5.0
debug1: Reading configuration data /home/muru/.ssh/config
debug1: /home/muru/.ssh/config line 41: Applying options for ?
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to 123.123.123.1 [123.123.123.1] port 12345.
debug1: connect to address 123.123.123.1 port 12345: Connection refused
ssh: connect to host 123.123.123.1 port 12345: Connection refused
Run Code Online (Sandbox Code Playgroud)


Rav*_*ina 7

改用函数:

function ssht(){
 ssh -p 12345 my_user@123.123.123.$1
}
Run Code Online (Sandbox Code Playgroud)
$ ssht 1
$ ssht 2
$ ...
Run Code Online (Sandbox Code Playgroud)

更好的解决方案是使用 sshconfig文件:

touch ~/.ssh/config
Run Code Online (Sandbox Code Playgroud)

有一些类似于:

Host some-name
    HostName 123.123.123.1
    User your_user
    Port 22
Run Code Online (Sandbox Code Playgroud)

你也可以使用 ssh 密钥来提高速度,最后你只需要运行:

ssh some-name
Run Code Online (Sandbox Code Playgroud)

并且您已连接到该服务器。