AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 942279
Accepted
Karl Morrison
Karl Morrison
Asked: 2017-08-03 05:46:51 +0800 CST2017-08-03 05:46:51 +0800 CST 2017-08-03 05:46:51 +0800 CST

为 ssh 连接创建别名

  • 772

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

我有服务器让我们说:

123.123.123.1
123.123.123.2
123.123.123.3

我通常连接以下内容:

ssh -p 12345 [email protected]

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

我尝试了以下代码:

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

但是我收到一个错误:

karl@karls-laptop ~/scripts $ ssht 1
Pseudo-terminal will not be allocated because stdin is not a terminal.

有没有办法让这个工作?

bash
  • 5 5 个回答
  • 17205 Views

5 个回答

  • Voted
  1. Jakuje
    2017-08-03T06:07:31+08:002017-08-03T06:07:31+08:00

    使用预期的方式并将选项和别名写入~/.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
    

    等等...

    然后连接只是使用

    ssh 1
    ssh 2
    ...
    
    • 61
  2. Best Answer
    heemayl
    2017-08-03T05:54:50+08:002017-08-03T05:54:50+08:00

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

    这样的事情应该做:

    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
    }
    

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

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

    ssht 1
    ssht 2
    ssht 3
    

    把它放在你~/.bashrc的,让它在任何交互式会话中都可用。

    • 20
  3. muru
    2017-08-03T21:19:51+08:002017-08-03T21:19:51+08:00

    您可以在~/.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.?
    

    结合:

    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.
    

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

    Host ?
        Hostname 123.123.123.%h
        Port 12345
        User my_user
    

    然后:

    $ 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
    
    • 17
  4. Ravexina
    2017-08-03T05:50:23+08:002017-08-03T05:50:23+08:00

    改用函数:

    function ssht(){
     ssh -p 12345 [email protected].$1
    }
    
    $ ssht 1
    $ ssht 2
    $ ...
    

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

    touch ~/.ssh/config
    

    有一些类似于:

    Host some-name
        HostName 123.123.123.1
        User your_user
        Port 22
    

    您还可以使用 ssh 密钥来提高速度,最后只运行:

    ssh some-name
    

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

    • 7
  5. user311189
    2017-08-03T06:56:43+08:002017-08-03T06:56:43+08:00

    您甚至不需要使用 ssht 之类的名称。以数字开头的名称,即使只有数字也是 ssh 配置文件中的有效主机名。

    以下作品适用于 Xubuntu Xenial

    我的一部分 ~/.ssh/config

    Host 1
            Hostname bastion.example.me
            User said
            Port 22
            IdentityFile ~/.ssh/id_rsa
            ForwardAgent yes
    

    我运行的命令(下面我默认添加-vv了详细日志记录到STDOUT您的屏幕)

    ssh -vv 1
    

    输出

    OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g  1 Mar 2016
    debug1: Reading configuration data /home/said/.ssh/config
    debug1: /home/said/.ssh/config line 24: Applying options for 1
    debug1: /home/said/.ssh/config line 540: Applying options for *
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 19: Applying options for *
    debug2: resolving "bastion.example.me" port 22
    debug2: ssh_connect_direct: needpriv 0
    debug1: Connecting to bastion.example.me [XXX.YYY.120.51] port 22.
    debug1: Connection established.
    debug1: identity file /home/said/.ssh/id_rsa type 1
    debug1: key_load_public: No such file or directory
    debug1: identity file /home/said/.ssh/id_rsa-cert type -1
    debug1: Enabling compatibility mode for protocol 2.0
    debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
    debug1: Remote protocol version 2.0, remote software version OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
    debug1: match: OpenSSH_7.2p2 Ubuntu-4ubuntu2.2 pat OpenSSH* compat 0x04000000
    debug2: fd 3 setting O_NONBLOCK
    debug1: Authenticating to bastion.example.me:22 as 'said'
    debug1: SSH2_MSG_KEXINIT sent
    debug1: SSH2_MSG_KEXINIT received
    debug2: local client KEXINIT proposal
    debug2: KEX algorithms: [email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,ext-info-c
    debug2: host key algorithms: [email protected],[email protected],[email protected],ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,[email protected],[email protected],ssh-ed25519,rsa-sha2-512,rsa-sha2-256,ssh-rsa
    debug2: ciphers ctos: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],aes128-cbc,aes192-cbc,aes256-cbc,3des-cbc
    debug2: ciphers stoc: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected],aes128-cbc,aes192-cbc,aes256-cbc,3des-cbc
    debug2: MACs ctos: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
    debug2: MACs stoc: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
    debug2: compression ctos: none,[email protected],zlib
    debug2: compression stoc: none,[email protected],zlib
    debug2: languages ctos: 
    debug2: languages stoc: 
    debug2: first_kex_follows 0 
    debug2: reserved 0 
    debug2: peer server KEXINIT proposal
    debug2: KEX algorithms: [email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha1
    debug2: host key algorithms: ssh-rsa,rsa-sha2-512,rsa-sha2-256,ecdsa-sha2-nistp256,ssh-ed25519
    debug2: ciphers ctos: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
    debug2: ciphers stoc: [email protected],aes128-ctr,aes192-ctr,aes256-ctr,[email protected],[email protected]
    debug2: MACs ctos: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
    debug2: MACs stoc: [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha2-512,hmac-sha1
    debug2: compression ctos: none,[email protected]
    debug2: compression stoc: none,[email protected]
    debug2: languages ctos: 
    debug2: languages stoc: 
    debug2: first_kex_follows 0 
    debug2: reserved 0 
    debug1: kex: algorithm: [email protected]
    debug1: kex: host key algorithm: ecdsa-sha2-nistp256
    debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
    debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
    debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
    debug1: Server host key: ecdsa-sha2-nistp256 SHA256:44tChrTUMwuRcOi6laiYlf6VM3qAD+PEn9EdNMribFw
    debug1: Host 'bastion.example.me' is known and matches the ECDSA host key.
    debug1: Found key in /home/said/.ssh/known_hosts:69
    debug2: set_newkeys: mode 1
    debug1: rekey after 134217728 blocks
    debug1: SSH2_MSG_NEWKEYS sent
    debug1: expecting SSH2_MSG_NEWKEYS
    debug2: set_newkeys: mode 0
    debug1: rekey after 134217728 blocks
    debug1: SSH2_MSG_NEWKEYS received
    debug2: key: /home/said/.ssh/id_rsa (0x562c764294f0), explicit, agent
    debug1: SSH2_MSG_EXT_INFO received
    debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
    debug2: service_accept: ssh-userauth
    debug1: SSH2_MSG_SERVICE_ACCEPT received
    debug1: Authentications that can continue: publickey
    debug1: Next authentication method: publickey
    debug1: Offering RSA public key: /home/said/.ssh/id_rsa
    debug2: we sent a publickey packet, wait for reply
    debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
    debug2: input_userauth_pk_ok: fp SHA256:KQNLYiJICyNbKmIxVVgc67RF+qRKjNi3w+0iXz/YDyk
    debug1: Authentication succeeded (publickey).
    Authenticated to bastion.example.me ([XXX.YYY.120.51]:22).
    debug1: channel 0: new [client-session]
    debug2: channel 0: send open
    debug1: Requesting [email protected]
    debug1: Entering interactive session.
    debug1: pledge: network
    debug1: client_input_global_request: rtype [email protected] want_reply 0
    debug2: callback start
    debug1: Requesting authentication agent forwarding.
    debug2: channel 0: request [email protected] confirm 0
    debug2: fd 3 setting TCP_NODELAY
    debug2: client_session2_setup: id 0
    debug2: channel 0: request pty-req confirm 1
    debug2: channel 0: request shell confirm 1
    debug2: callback done
    debug2: channel 0: open confirm rwindow 0 rmax 32768
    debug2: channel_input_status_confirm: type 99 id 0
    debug2: PTY allocation request accepted on channel 0
    debug2: channel 0: rcvd adjust 2097152
    debug2: channel_input_status_confirm: type 99 id 0
    debug2: shell request accepted on channel 0
    Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.8.0-42-lowlatency x86_64)
    <TRUNCATED CUSTOM MOTD>
    $
    

    更新 - 下面是一个骇人听闻的替代解决方案,仅用于 bashist 练习,也许是快速和肮脏的东西:

    alias ssht='f(){ ssh -p 22 [email protected].$@;unset -f f;}&&f'
    

    它能做什么

    • 创建临时函数
    • 将所有参数传递给它
    • 进行 SSH 连接
    • 会话结束后,取消设置功能,使其不会徘徊
    • 它可以接受额外的参数,这意味着您可以链接额外的 ssh 选项,如隧道 ( -L, -R, -D)、详细模式 ( -vv)、no-TTY ( -T) 等。

    例如,我想启动一个没有终端的 socks 代理

    $ ssht 2 -vv -D 1080 -T
    OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g  1 Mar 2016
    debug1: Reading configuration data /home/said/.ssh/config
    debug1: /home/said/.ssh/config line 540: Applying options for *
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: /etc/ssh/ssh_config line 19: Applying options for *
    debug2: resolving "192.168.0.2" port 22
    debug2: ssh_connect_direct: needpriv 0
    debug1: Connecting to 192.168.0.2 [192.168.0.2] port 22.
    debug1: Connection established.
    <TRUNCATED>
    Welcome to Linux Mint 18.1 Serena (GNU/Linux 4.4.0-81-generic x86_64)
    
     * Documentation:  https://www.linuxmint.com
    
    98 packages can be updated.
    0 updates are security updates.
    

    如您所见,没有命令提示符,它使用-vv, -T,-D 1080参数执行。

    我也可以在我的机器上验证隧道(基本上是 SOCKS5 代理)

    $ ss -ltnp | grep 1080
    LISTEN     0      128    127.0.0.1:1080                     *:*                   users:(("ssh",pid=17038,fd=6))
    
    • 3

相关问题

  • 同时复制到两个位置

  • 如何在 shell 脚本中创建选择菜单?

  • 从 bash 迁移到 zsh [关闭]

  • bashrc 还是 bash_profile?

  • 备份 bash 脚本未压缩其 tarball

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve