which host

Discussion in 'Perl Misc' started by Werner Winter, Oct 31, 2003.

  1. 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
    Werner Winter, Oct 31, 2003
    #1
    1. Advertising

  2. On Fri, 31 Oct 2003, Werner Winter <> wrote:
    > 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.

    --
    David Efflandt - All spam ignored http://www.de-srv.com/
    David Efflandt, Oct 31, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    0
    Views:
    351
  2. ALI-R
    Replies:
    11
    Views:
    588
    Victor
    Nov 23, 2005
  3. Orpheus66
    Replies:
    0
    Views:
    4,403
    Orpheus66
    Jul 30, 2003
  4. multiplex77
    Replies:
    0
    Views:
    610
    multiplex77
    Jan 30, 2007
  5. Jerry Qu
    Replies:
    1
    Views:
    448
    lee atkinson
    Feb 20, 2009
Loading...

Share This Page