Find the IP address of the client in an SSH sessio

2019-01-20 22:55发布

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

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.

19条回答
Anthone
2楼-- · 2019-01-20 23:49

Try the following to get just the IP address via SSH:

Command: ifconfig

Example:

stalinrajindian@ubuntuserver:~$ ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.30.3.27  netmask 255.255.255.0  broadcast 172.30.3.255
        inet6 fe80::a00:27ff:fe8b:9986  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:8b:99:86  txqueuelen 1000  (Ethernet)
        RX packets 4876  bytes 1951791 (1.9 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 775  bytes 73783 (73.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 78  bytes 5618 (5.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 78  bytes 5618 (5.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
查看更多
姐就是有狂的资本
3楼-- · 2019-01-20 23:50
netstat -tapen | grep ssh | awk '{ print $10}'

Output:

two # in my experiment

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

gives the IP address.

Output:

127.0.0.1:22 # in my experiment

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

查看更多
甜甜的少女心
4楼-- · 2019-01-20 23:51

You can get it in a programmatic way via an SSH library (https://code.google.com/p/sshxcute)

public static String getIpAddress() throws TaskExecFailException{
    ConnBean cb = new ConnBean(host, username, password);
    SSHExec ssh = SSHExec.getInstance(cb);
    ssh.connect();
    CustomTask sampleTask = new ExecCommand("echo \"${SSH_CLIENT%% *}\"");
    String Result = ssh.exec(sampleTask).sysout;
    ssh.disconnect();   
    return Result;
}
查看更多
我命由我不由天
5楼-- · 2019-01-20 23:52

One thumb up for @Nikhil Katre's answer :

Simplest command to get the last 10 users logged in to the machine is last|head.

To get all the users simply use last command

The one using who or pinky did what is basically asked. But But But they don't give historical sessions info.

Which might also be interesting if you want to know someone who has just logged in and logged out already when you start this checking.

if it is a multiuser system. I recommand add the user account you are looking for:

last | grep $USER | head

EDIT:

In my case, both $SSH_CLIENT and $SSH_CONNECTION do not exist.

查看更多
狗以群分
6楼-- · 2019-01-20 23:54

Try the following to get just the IP address:

who am i|awk '{ print $5}'
查看更多
淡お忘
7楼-- · 2019-01-20 23:54

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

查看更多
登录 后发表回答