Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to write a simple perl script that goes through a host file and gives the last login and time for every system in the hosts file I feel I am close but this is blowing up screen noise any help would be appreciated - thx
#!/usr/bin/perl -w open (IN, "<hosts.txt" ) || die "Cant open hosts.txt"; while(<IN>) { chomp; system("finger$_ $_"); }

Replies are listed 'Best First'.
Re: Last logon and time for a system console
by almut (Canon) on Mar 03, 2009 at 15:35 UTC
        system("finger$_  $_");

    Let's say one of your hosts in hosts.txt is named "foo", then your command line would expand to

    fingerfoo foo

    Probably not what was intended...

    Update: perhaps you meant something like "ssh host last -n1"1 ? (see "man last")  With finger, you'd need to specify some user ("finger user@host"), so this probably isn't what you want — and depending on your site configuration, remote fingering (i.e. @host) may not work at all.

    ___

    1 after seeing Utilitarian's reply, I realize that "the last login and time for every system" can be read in several ways. The way I understood it was that you wanted to know when anyone last logged in on those hosts, not from those hosts to your local machine...

Re: Last logon and time for a system console
by Bloodnok (Vicar) on Mar 03, 2009 at 15:47 UTC
    Aside from the pertinent observations made elsewhere in this thread by toolic & almut, regarding the command expansion as seen by the system call, I feel I ought to point out that finger(1M) is a command to return information about users, not hosts.

    A user level that continues to overstate my experience :-))
Re: Last logon and time for a system console
by toolic (Bishop) on Mar 03, 2009 at 15:38 UTC
    It is hard to provide specific help because you did not show us the contents of your input file, or a small sample of your output or error messages.

    You should check the status of your system call.

    If your input file contains the string "foo", do you really have a command called "fingerfoo"? Perhaps you really meant (without the extra $_):

    system("finger $_");
      Last user logon and time for system
Re: Last logon and time for a system console
by Utilitarian (Vicar) on Mar 03, 2009 at 17:15 UTC
    This is a quick and dirty solution, only tested with gnu last from RHEL4 so may need a bit of tweaking to suit the output of your version of last.

    last comes in reverse time order already, so the first value assigned to any host will be the latest log in from that host.

    #!/usr/bin/perl use strict; use warnings; my @last= `last`; my (%time, @details, $host); map{ @details=split(/ +/); $host=$details[2]; $time{$host}=join (' ', @details[3,4,5,6]) unless $time{$host} ; }@last[0..$#last - 2]; map{ print "$_, last login at $time{$_}\n"; } keys %time;
Re: Last logon and time for a system console
by slacker (Friar) on Mar 03, 2009 at 17:13 UTC
    If you're searching for the last log in time for every user,
    a couple options would be to finger every user you're looking for,
    or run last and parse out the first appearance of each user.