使用 openssh 实现的一些鲜为人知且很少使用的功能,您可以准确地完成您想要的!
- 利用当前状态
- 可以使用你所在的工作目录
- 在会话开始之前不需要任何隧道设置
- 不需要打开单独的终端或连接
- 可以在交互式会话中用作一次性交易,也可以用作自动会话的一部分
在下面的示例中,您应该只键入每个local>
、remote>
和
ssh>
提示符处的内容。
local> ssh username@remote
remote> ~C
ssh> -L6666:localhost:6666
remote> nc -l 6666 < /etc/passwd
remote> ~^Z
[suspend ssh]
[1]+ Stopped ssh username@remote
local> (sleep 1; nc localhost 6666 > /tmp/file) & fg
[2] 17357
ssh username@remote
remote> exit
[2]- Done ( sleep 1; nc localhost 6666 > /tmp/file )
local> cat /tmp/file
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
...
或者,更多时候你想去另一个方向,例如,如果你想做一些事情,比如将你的~/.ssh/id_rsa.pub
文件从本地机器传输到~/.ssh/authorized_keys
远程机器的文件。
local> ssh username@remote
remote> ~C
ssh> -R5555:localhost:5555
remote> ~^Z
[suspend ssh]
[1]+ Stopped ssh username@remote
local> nc -l 5555 < ~/.ssh/id_rsa.pub &
[2] 26607
local> fg
ssh username@remote
remote> nc localhost 5555 >> ~/.ssh/authorized_keys
remote> cat ~/.ssh/authorized_keys
ssh-rsa AAAAB3NzaC1yc2ZQQQQBIwAAAQEAsgaVp8mnWVvpGKhfgwHTuOObyfYSe8iFvksH6BGWfMgy8poM2+5sTL6FHI7k0MXmfd7p4rzOL2R4q9yjG+Hl2PShjkjAVb32Ss5ZZ3BxHpk30+0HackAHVqPEJERvZvqC3W2s4aKU7ae4WaG1OqZHI1dGiJPJ1IgFF5bWbQl8CP9kZNAHg0NJZUCnJ73udZRYEWm5MEdTIz0+Q5tClzxvXtV4lZBo36Jo4vijKVEJ06MZu+e2WnCOqsfdayY7laiT0t/UsulLNJ1wT+Euejl+3Vft7N1/nWptJn3c4y83c4oHIrsLDTIiVvPjAj5JTkyH1EA2pIOxsKOjmg2Maz7Pw== username@local
稍微解释一下就可以了。
第一步是打开一个LocalForward
;如果您还没有建立一个,那么您可以使用~C
转义字符打开一个 ssh 命令行,该命令行将为您提供以下命令:
remote> ~C
ssh> help
Commands:
-L[bind_address:]port:host:hostport Request local forward
-R[bind_address:]port:host:hostport Request remote forward
-D[bind_address:]port Request dynamic forward
-KR[bind_address:]port Cancel remote forward
在这个例子中,我LocalForward
在 localhost 的 6666 端口上为客户端和服务器建立了一个端口;端口号可以是任意开放的端口。
该nc
命令来自netcat
包;它被描述为“TCP/IP 瑞士军刀”;这是一个简单但非常灵活和有用的程序。让它成为你的 unix 工具带的标准部分。
此时nc
正在侦听端口 6666 并等待另一个程序连接到该端口,以便它可以发送
/etc/passwd
.
接下来我们使用另一个转义字符~^Z
,tilde
后面跟着control-Z
. 这会暂时挂起 ssh 进程并将我们放回 shell。
回到本地系统,您可以使用它nc
来连接到转发的端口 6666。请注意,在这种情况下缺少 a -l
,因为该选项告诉nc
在端口上侦听,就好像它是不是我们想要的服务器一样;相反,我们只想nc
用作客户端来连接到nc
远程端已经在监听的。
该nc
命令的其余部分是必需的,因为如果您还记得上面我说过该ssh
进程被暂时挂起,因此&
会将整个(sleep + nc)
表达式置于后台,并sleep
为您提供足够的时间让 ssh 以fg
.
-R
在第二个示例中,想法基本相同,只是我们使用而不是设置了一条通向另一个方向的隧道,-L
以便我们建立一个RemoteForward
. 然后在本地是您要使用-l
参数的地方nc
。
默认情况下,转义字符是 ~ 但您可以使用以下方法更改它:
-e escape_char
Sets the escape character for sessions with a pty (default: ‘~’). The escape character is only recognized at the beginning of a line. The escape character followed by a dot
(‘.’) closes the connection; followed by control-Z suspends the connection; and followed by itself sends the escape character once. Setting the character to “none” disables any
escapes and makes the session fully transparent.
ssh 手册页中提供了对转义字符可用命令的完整说明
ESCAPE CHARACTERS
When a pseudo-terminal has been requested, ssh supports a number of functions through the use of an escape character.
A single tilde character can be sent as ~~ or by following the tilde by a character other than those described below. The escape character must always follow a newline to be interpreted
as special. The escape character can be changed in configuration files using the EscapeChar configuration directive or on the command line by the -e option.
The supported escapes (assuming the default ‘~’) are:
~. Disconnect.
~^Z Background ssh.
~# List forwarded connections.
~& Background ssh at logout when waiting for forwarded connection / X11 sessions to terminate.
~? Display a list of escape characters.
~B Send a BREAK to the remote system (only useful for SSH protocol version 2 and if the peer supports it).
~C Open command line. Currently this allows the addition of port forwardings using the -L, -R and -D options (see above). It also allows the cancellation of existing remote port-
forwardings using -KR[bind_address:]port. !command allows the user to execute a local command if the PermitLocalCommand option is enabled in ssh_config(5). Basic help is avail‐
able, using the -h option.
~R Request rekeying of the connection (only useful for SSH protocol version 2 and if the peer supports it).