26278 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Last time CO2 was
this high, the
world was
underwater? NO,
actually
Boffins find "scary
radio
attack"*
against pacemakers
Pakistan signs up
for China"s GPS
rival
Intel releases
"Beacon Mountain"
Android-on-Atom dev
tool
US boffin builds
32-way Raspberry Pi
cluster
Massive EXPLOSION
visible to naked
eye SEEN ON MOON
Yahoo! Japan says
22 MEELLION User
IDs may have been
nabbed
Nintendo throws
flaming legal
barrel at YouTubing
fans
Optus outlines its
4G future
Hold our tiny
silicon spheres,
say gravity wave
detection
scientists
Slashdot
Music and Movies
Could Trigger
Mobile Malware
Ask Slashdot:
Wiring Home
Furniture?
Medical Firm Sues
IRS For 4th
Amendment Violation
In Records Seizure
Military Dolphins
Discover 1800s
Torpedo
Apple Mobile
Devices Cleared For
Use On US Military
Networks
Mice, Newts
Retrieved After a
Month Orbiting
Earth At 345 Miles
Up
IBM Takes System/z
To the Cloud With
COBOL Update
Google"s Nexus Q
Successor Hits the
FCC
Yahoo Board
Approves a $1.1B
Pricetag For Tumblr
Trade Group: US
Software Developer
Wages Fell 2% Last
Year
Article viewer

Perl Sysadmin Shell History Script



Written by:dimport
Published by:dimport
Published on:2003-06-21 07:19:46
Topic:Perl
Search OSI about Perl.More articles by dimport.
 viewed 10471 times send this article printer friendly

Digg this!
    Rate this article :
This is a short perl script I wrote to help me keep an eye on what user's on my server are doing in their shells.

The script takes various arguments:
-d Display shell history listing for all users on date .
Example: $progname -d "Oct 13"
Displays shell history listings for all users
who logged in on Oct 13 this year.
-u Display complete shell history listing for the user .

Example: $progname -u munk
Display a complete shell history listing for
the user 'munk'.

The script uses the Getopt::Std perl module.
Code follows:
 
#!/usr/bin/perl
# script to show a user's csh history with dates
# 14/10/2002 ammended to add support for
# displaying all user's history files
# on a given date (switch -d)
# useful for running daily from a cron job
# for the paranoid/careful admin


use strict;
use Getopt::Std;
#require 'getopts.pl';

my %opt=();
my($user, $date, $histdate, $histfile, $err);
my $div="#"x68;

my $progname = $0;
$progname =~ s,.*/,,; # use basename only
$progname =~ s/.w*$//; # strip extension, if any

# find out what options we're passed:
&getopts('u:d:h', %opt);

# display usage if bad opts:
if($opt{h}){ &usage(); }

# parse options:
if($opt{u}){
    &showUserHistory($opt{u});
} elsif ($opt{d}) {
    &showDateHistory($opt{d});
} else {
    &usage("No options specified
");
}

sub showUserHistory {
    ($user, $date) = @_;
    chomp($date);

    # make sure the date format is right
    #(for matching against system 'date' cmd):
    $date=~s/(w+) (d)$/$1 $2/;
    
    my $output="";
    my $line;
    my $header="${div}
History for user $user:
$div
";
    
    $histfile="/home/$user/.history";
    open(FD, "$histfile") or ($opt{u} && print "${div}
Couldn't open history file $histfile
$div
");

    while(){
        s/^#//;
        
        $histdate=`date -r $_`;
        chomp($histdate);
        # if this history command occurs on the date passed to us,
        # or if we're just listing all commands for a user,
        # append the command history to the $output vbl:
        my $cmd=;
        if( ($histdate=~/$date/) || $opt{u} ){
            $output.=sprintf("%s %40s", $histdate, $cmd);
        }
    }
    
    # if there's anything in output, show it, otherwise print a msg:
    if($output){
        print $header.$output;
    } else {
        # only if single user, otherwise omit:
        $opt{u} && print "
${div}
No history for user $user
$div
";
    }
}

sub showDateHistory{
    $date=shift;

    opendir(DH, "/home/");

    while(($user=readdir DH)){
        if($user ne "." && $user ne ".."){
            &showUserHistory($user, $date);
        }
    }
}

sub usage{
    $err && (print $err,"
");
    die] [-d ] [-h]
       -h Display this help.

       -d Display shell history listing for all users on
                              date .
                              Example: $progname -d "Oct 13"
                              Displays shell history listings for all users
                              who logged in on Oct 13 this year.

       -u Display complete shell history listing for
                              the user .
                              Example: $progname -u munk
                              Display a complete shell history listing for
                              the user 'munk'.
~USAGE~
}


This article was originally written by munk

Did you like this article? There are hundreds more.

Comments:
<none>
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
bb
List the number of files in a directory and each subdirectory on Fri 29th Sep 11am
I needed to get the number of files in a directory and recurse through all subdirectories doing the same. I nedded it so i could compare two sets of directory trees for something I was working on. My Irish chum Enstyne came up with this Perl solution w

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Perl: The basics by sefo

Basic knowledge on often used Perl features


     
Your Ad Here
 
Copyright Open Source Institute, 2006