Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5361406
  • 博文数量: 1144
  • 博客积分: 11974
  • 博客等级: 上将
  • 技术积分: 12312
  • 用 户 组: 普通用户
  • 注册时间: 2005-04-13 20:06
文章存档

2017年(2)

2016年(14)

2015年(10)

2014年(28)

2013年(23)

2012年(29)

2011年(53)

2010年(86)

2009年(83)

2008年(43)

2007年(153)

2006年(575)

2005年(45)

分类: 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);
}
阅读(1558) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~