Linux 在 SSH 会话中查找客户端的 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/996231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:25:16  来源:igfitidea点击:

Find the IP address of the client in an SSH session

linuxnetworkingsship-address

提问by flybywire

I have a script that is to be run by a person that logs in to the server with SSH.

我有一个脚本,由使用SSH登录服务器的人运行。

Is there a way to find out automatically what IP address the user is connecting from?

有没有办法自动找出用户连接的 IP 地址?

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.

当然,我可以问用户(它是程序员的工具,所以没问题),但如果我刚刚发现会更酷。

采纳答案by nolim1t

Check if there is an environment variable called:

检查是否有一个名为的环境变量:

$SSH_CLIENT 

OR

或者

$SSH_CONNECTION

(or any other environment variables) which gets set when the user logs in. Then process it using the user login script.

(或任何其他环境变量)在用户登录时设置。然后使用用户登录脚本处理它。

Extract the IP:

提取IP:

$ echo $SSH_CLIENT | awk '{ print }'
1.2.3.4
$ echo $SSH_CONNECTION | awk '{print }'
1.2.3.4

回答by Daniel Schneller

Usually there is a log entry in /var/log/messages (or similar, depending on your OS) which you could grep with the username.

通常在 /var/log/messages(或类似的,取决于您的操作系统)中有一个日志条目,您可以使用用户名对其进行 grep。

回答by mihi

Assuming he opens an interactive session (that is, allocates a pseudo terminal) and you have access to stdin, you can call an ioctl on that deviceto get the device number (/dev/pts/4711) and try to find that one in /var/run/utmp(where there will also be the username and the IP address the connection originated from).

假设他打开了一个交互式会话(即分配了一个伪终端)并且您可以访问 stdin,您可以在该设备上调用一个 ioctl来获取设备号 (/dev/pts/4711) 并尝试在/var/run/utmp(其中还将包含用户名和连接来源的 IP 地址)。

回答by vncprado

You could use the command:

您可以使用以下命令:

server:~# pinky

that will give to you somehting like this:

这会给你这样的东西:

Login      Name                 TTY    Idle   When                 Where 

root       root                 pts/0         2009-06-15 13:41     192.168.1.133

回答by aric

netstat will work (at the top something like this) tcp 0 0 10.x.xx.xx:ssh someipaddress.or.domainame:9379 ESTABLISHED

netstat 将工作(在顶部像这样) tcp 0 0 10.x.xx.xx:ssh someipaddress.or.domainame:9379 ESTABLISHED

回答by AlexP

Try the following to get just the IP address:

尝试以下操作以获取 IP 地址:

who am i|awk '{ print }'

回答by pfrandsen

Linux: who am i | awk '{print $5}' | sed 's/[()]//g'

Linux:我是谁 | awk '{print $5}' | sed 's/[()]//g'

AIX: who am i | awk '{print $6}' | sed 's/[()]//g'

AIX:我是谁| awk '{print $6}' | sed 's/[()]//g'

回答by Sébastien Moreau

netstat -tapen | grep ssh | awk '{ print }'

回答by gerard

netstat -tapen | grep ssh | awk '{ print }'

Output:

输出:

two # in my experiment

两个 # 在我的实验中

netstat -tapen | grep ssh | awk '{ print }' 

gives the IP address.

给出 IP 地址。

Output:

输出:

127.0.0.1:22 # in my experiment

But the results are mixed with other users and stuff. It needs more work.

但结果与其他用户和东西混合在一起。它需要更多的工作。

回答by Spindrift

who am i | awk '{print }' | sed 's/[()]//g' | cut -f1 -d "." | sed 's/-/./g'


export DISPLAY=`who am i | awk '{print }' | sed 's/[()]//g' | cut -f1 -d "." | sed 's/-/./g'`:0.0

I use this to determine my DISPLAY variable for the session when logging in via ssh and need to display remote X.

当通过 ssh 登录并需要显示远程 X 时,我使用它来确定会话的 DISPLAY 变量。