which host

W

Werner Winter

Hello,
I've setup a linux terminal server. Now I want to know, which user is logged
in on which host . So I wrote a perl-script named "which_host"

# which user
$uid=$<;
$username=(getpwuid)$uid))[0];
$username = $1;

#which host
@last= `/usr/bin/last -n 1 -a $username`;
# line 18
$host= (split (/\s+/, $last[0]))[9];

The last two lines cause an error: Use of uninitialized value in string eq
at /usr/local/bin/which_host line 18

The problem is, that "last" shows only 8 columns, no host in column 9 is
shown. If I change the line
@last= `/usr/bin/last -n 1 -a $username`;
to
@last= `/usr/bin/last -n 3 -a $username`;
then I get 3 lines and in the third line the host is shown in column 9.

What can I do?

cu
Werner
 
D

David Efflandt

Hello,
I've setup a linux terminal server. Now I want to know, which user is logged
in on which host . So I wrote a perl-script named "which_host"

# which user
$uid=$<;
$username=(getpwuid)$uid))[0];
$username = $1;

Doesn't the above throw an error (getpwuid)$uid))[0]. Then you
immediately reassign $username (where does $1 get its value from?).
Learn how to copy/paste, so your code examples are not munged by retyping.
#which host
@last= `/usr/bin/last -n 1 -a $username`;
# line 18
$host= (split (/\s+/, $last[0]))[9];

The last two lines cause an error: Use of uninitialized value in string eq
at /usr/local/bin/which_host line 18

The problem is, that "last" shows only 8 columns, no host in column 9 is
shown. If I change the line
@last= `/usr/bin/last -n 1 -a $username`;
to
@last= `/usr/bin/last -n 3 -a $username`;
then I get 3 lines and in the third line the host is shown in column 9.

On my system, that column only indicates a remote host, and is typically
empty from localhost (local console login, xterm, etc.). So you have to
consider that possibility in your code. Of course the 9th column would be
the [8] item in the [0..8] list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top