我想加快连接到特定服务器的速度。
我有服务器让我们说:
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)
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
的任何交互式会话中。
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)
改用函数:
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)
并且您已连接到该服务器。
归档时间: |
|
查看次数: |
14158 次 |
最近记录: |