15

I can see the names of other users on the remote machine with the who command... I'd also like know the IP address of those users...

I was trying with the commands /sbin/ifconfing and netstat but I could not get positive results...

I need this solution compatible both with Linux and Unix...

Is there a command with that utility? Do I need to write a script or use a kind of pipes?

bbaja42
  • 2,800
omar
  • 335
  • 5
    Just to be clear, who doesn't tell you about users on the same network, just users logged in to the same machine as you. – jw013 Nov 10 '11 at 04:39
  • When I log in a distant machine, who gives me my origin machine name on both linux and solaris (last field, between parenthesis, when logged in locally from X I get the X display). Getting the IP from that should be easy (nslookup, host) – AProgrammer Nov 10 '11 at 09:12

4 Answers4

20

Try the w command, part of the procps package.

$ w
 21:12:09 up 6 days,  7:42,  1 user,  load average: 0.27, 1.08, 1.64
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
h3xx     pts/11   192.168.1.3      21:12    2.00s  0.04s  0.04s -bash
amphetamachine
  • 5,517
  • 2
  • 35
  • 43
5

The who manpage on my Debian Linux system shows there's an --ips option to display IPs instead of hostnames.

LawrenceC
  • 10,992
1

who is the command I use, but it is not 100% reliable. The resulting names are from the PTR record for the IP address. There may or may not be a matching A record for the name.

Data from ps and netstat can be integrated if you have root privileges. Otherwise you can only make educated guesses which connection belongs to which process.

There are other tools which can be used, but I haven't found any programs that have consistent parameters and output across UNIX/Linux flavors.

BillThor
  • 8,965
1

On some old UNIX (i.e. SCO OpenServer Release 5.0.7), the w command is a good start, but the IP address is not output by default. Command arguments are sometimes required. From the man page:

-x
   Print out the hostname. This option cannot be used with
   the -u option.

-X
   Print out full hostnames. This option implies the -x
   option. Hostnames printed by the -x option are truncated
   to fit into a field; hostnames printed by the -X option
   are not truncated.

For example:

$ w
  2:39pm  up 2 days, 11:07,  1 user,  load average: 0.00, 0.00, 0.00
User     Tty       Login@   Idle   JCPU   PCPU  What
minime   ttyp0     2:31pm      -      -      -  w
$ w -x
  2:39pm  up 2 days, 11:07,  1 user,  load average: 0.00, 0.00, 0.00
User     Tty       Login@   Idle   JCPU   PCPU Hostname           What
minime   ttyp0     2:31pm      -      -      - 192.168.36.188     w -x
kbulgrien
  • 830