如何判断SSH是否进入机器?

use*_*961 14 ssh

是否可以判断终端是否通过 SSH 连接到机器?如果我是 SSHd,某种命令可以为我执行此操作并执行某些操作?

run*_*uhl 21

您可以检查是否设置了$SSH_CLIENT$SSH_TTY变量:

(through SSH) $ [[ -z $SSH_TTY ]]; echo $?
1
(local) $  [[ -z $SSH_TTY ]]; echo $?
0
Run Code Online (Sandbox Code Playgroud)

$SSH_CLIENT变量包含您正在连接的 IP,以及远程和本地端口:

(through SSH) $ echo $SSH_CLIENT
15.3.25.189 54188 22
Run Code Online (Sandbox Code Playgroud)


vsc*_*hum 6

尝试按Enter然后按~?

如果您使用的是 OpenSSH 客户端,您应该会收到一个支持的转义序列列表,这些转义序列执行其他功能。

$ ~?
Supported escape sequences:
 ~.   - terminate connection (and any multiplexed sessions)
 ~B   - send a BREAK to the remote system
 ~C   - open a command line
 ~R   - request rekey
 ~V/v - decrease/increase verbosity (LogLevel)
 ~^Z  - suspend ssh
 ~#   - list forwarded connections
 ~&   - background ssh (when waiting for connections to terminate)
 ~?   - this message
 ~~   - send the escape character by typing it twice
(Note that escapes are only recognized immediately after newline.)
Run Code Online (Sandbox Code Playgroud)

这将验证本地机器上的连接状态,而不是检查远程机器上设置的环境变量,因此受远程配置的影响。