在 SSH 会话中查找客户端的 IP 地址 [英] Find the IP address of the client in an SSH session
本文介绍了在 SSH 会话中查找客户端的 IP 地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个脚本,该脚本由使用 SSH 登录服务器的人运行
I have a script that is to be run by a person that logs in to the server with SSH.
有没有办法自动找出用户连接的 IP 地址?
Is there a way to find out automatically what IP address the user is connecting from?
当然,我可以问用户(它是程序员的工具,所以没问题),但如果我刚发现就更酷了.
Of course, I could ask the user (it is a tool for programmers, so no problem with that), but it would be cooler if I just found out.
推荐答案
检查是否有环境变量调用:
Check if there is an environment variable called:
$SSH_CLIENT
或
$SSH_CONNECTION
(或任何其他环境变量)在用户登录时设置.然后使用用户登录脚本处理它.
(or any other environment variables) which gets set when the user logs in. Then process it using the user login script.
提取IP:
$ echo $SSH_CLIENT | awk '{ print $1}'
1.2.3.4
$ echo $SSH_CONNECTION | awk '{print $1}'
1.2.3.4
这篇关于在 SSH 会话中查找客户端的 IP 地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文