Listing 1 The CommandTypes.pl perl script
#!/usr/bin/perl -w
# $Id: CommandTypes.pl,v 1.1 2006/03/18 08:30:53 mtsouk Exp mtsouk $
#
# Please note that this is alpha code
# Provided without any guarantees
#
# Programmer: Mihalis Tsoukalos
# Date: Saturday 18 March 2006
#
# Command line arguments
# program_name.pl <directory with history files>
use strict;
my $directory = "";
my $filename = "";
# Total Lines read
my $TL = 0;
# Usual shell commands.
my $CAT1 = 0;
my %CATHASH1 = ( "ls", 0,
"mkdir", 0,
"rmdir", 0,
"rm", 0,
"cd", 0,
"l", 0,
"ll", 0,
"mv", 0,
"pwd", 0,
"w", 0,
"history", 0,
"grep", 0,
"emacs", 0,
"sudo", 0,
"man", 0,
"vim", 0,
"gvim", 0,
"less", 0,
"chmod", 0,
"lsmod", 0,
"su", 0,
"lilo", 0,
"lsof", 0,
"cp", 0,
"tail", 0,
"kill", 0,
"ps", 0,
"echo", 0,
"cat", 0,
"wc", 0,
"alias", 0,
"rar", 0,
"unzip", 0,
"zip", 0,
"top", 0,
"df", 0,
"find", 0,
"mount", 0,
"last", 0,
"exit", 0,
"vi", 0);
# Remote access and networking commands.
my $CAT2 = 0;
my %CATHASH2 = ( "ssh", 0,
"telnet", 0,
"who", 0,
"ping", 0,
"traceroute", 0,
"netstat", 0,
"ftp", 0,
"wget", 0,
"ncftp", 0,
"scp", 0);
# Compile commands.
my $CAT3 = 0;
my %CATHASH3 = ( "gcc", 0,
"javac", 0,
"java", 0,
"make", 0,
"g++", 0,
"perl", 0,
"python", 0);
# Custom commands or user scripts.
my $CAT4 = 0;
my %CATHASH4 = ();
die <<Thanatos unless @ARGV;
usage:
$0 directory
Thanatos
if ( @ARGV != 1 )
{
die <<Thanatos
usage info:
Please use exactly 1 argument!
Thanatos
}
# Get the directory name
($directory) = @ARGV;
print "The following text files found in directoy: $directory\n";
opendir(BIN, $directory)
|| die "Error opening directory $directory: $!\n";
while (defined ($filename = readdir BIN) )
{
# The following command does not process . and ..
next if( $filename =~ /^\.\.?$/ );
print $filename."\n";
process_file($directory."/".$filename);
}
print "Total Lines Procesed: $TL\n";
print "Category1: $CAT1\n";
print "Category2: $CAT2\n";
print "Category3: $CAT3\n";
print "Category4: $CAT4\n";
# The call to the print_hash function is with a HASH reference.
print_hash(\%CATHASH1);
print_hash(\%CATHASH2);
print_hash(\%CATHASH3);
print_hash(\%CATHASH4);
exit 0;
sub process_file
{
my $file = shift;
my $line = "";
open (HISTORYFILE, "< $file")
|| die "Cannot open $file: $!\n";
while ( defined($line = <HISTORYFILE>) )
{
chomp $line;
$TL++;
# Get the first string of the command.
my @split = split / /, $line;
my $command = $split[0];
# To catch empty history lines.
next if ( ! defined($command) );
check_category($command);
}
}
sub check_category
{
my $command = shift;
chomp $command;
if (exists ($CATHASH1{$command}) )
{
# Counts the number of times
# a command has been found.
$CATHASH1{$command} = $CATHASH1{$command} + 1;
$CAT1++;
}
elsif (exists($CATHASH2{$command}) )
{
$CATHASH2{$command} = $CATHASH2{$command} + 1;
$CAT2++
}
elsif (exists($CATHASH3{$command}) )
{
$CATHASH3{$command} = $CATHASH3{$command} + 1;
$CAT3++;
}
else
{
$CAT4++;
# Keep a list of unknown commands
if ( ! exists ($CATHASH4{$command}) )
{
$CATHASH4{$command} = 1;
}
else
{
$CATHASH4{$command} = \
$CATHASH4{$command} + 1;
}
}
}
sub print_hash
{
my $HASH = shift;
my $key = "";
my $value = "";
while ( ($key, $value) = each(%$HASH))
{
if ( ! ($key =~ /\s/) )
{
print "$value\t$key"."\n";
}
}
}
|