全部博文(1144)
分类: LINUX
2009-04-18 12:42:52
#!/usr/bin/perl
use LWP;
use Getopt::Std;
use HTTP::Headers;
use HTTP::Request::Common;
use POSIX qw(strftime);
use strict;
use vars qw($login $logout %options %projects);
=pod
Usage: tracker.pl [-u username -p password -P project -t "MM/DD/YYYY HH:MM:SS"] list|login|logout\n);
=cut
$login='';
$logout='';
getopt("u:p:t:P:", \%options);
my $user= $options{u} || 'guym';
my $pass= $options{p} || 's1llium';
my $now = $options{t} || strftime("%m/%d/%Y %H:%M:%S", localtime(time));
my $prj = $options{P} || 0;
my $cmd = $ARGV[0];
my $h=new HTTP::Headers;
$h->authorization_basic($user,$pass);
my($key,$val)=split(/: /, $h->as_string());
my $ua=new LWP::UserAgent;
my $res=$ua->request(GET $login, $key=>$val);
# Get the content of the login page
my $page=$res->content;
my @lines=split(/\n/, $page);
# Build a projects hashtable (id to name).
for my $line (@lines) {
$line=~/;
$projects{$1}=$2;
}
# List the projects
if($cmd eq 'list') {
for my $project (sort { $a <=> $b } keys %projects) {
print "$project $projects{$project}\n";
}
}
# Post a login to a project
elsif($cmd eq 'login') {
print "Logging into project $projects{$prj} at $now\n";
my $res=$ua->request(POST "$login?state=add_do",
$key=>$val,
Content => [
s_project_id=>$prj,
s_start_time=>$now
]);
print $res->is_success;
}
# Post a logout to a project
elsif($cmd eq 'logout') {
print "Logging out of project $projects{$prj} at $now\n";
my $res=$ua->request(GET $logout, $key=>$val);
$res->as_string =~ /state=edit_do\&id=(\d+)/;
my $id=$1;
$res=$ua->request(POST "$logout?state=edit_do&id=$id",
$key=>$val,
Content => [
s_project_id=>$prj,
s_stop_time=>$now
]);
print $res->is_success;
}
else {
print qq(Usage: tracker.pl [-u username -p password -P number -t "MM/DD/YYYY HH:MM:SS"] list|login|logout\n);
}