#!/usr/bin/perl -w # tmobile.pl use strict; use LWP::UserAgent; use HTTP::Request::Common qw(:DEFAULT $DYNAMIC_FILE_UPLOAD); use HTTP::Cookies; use Logger::Simple; my ($username,$password) = @ARGV; my $i = 0; my $max_tries = 5; my %levels = (ERROR=>1, WARN=>4, INFO=>6, DEBUG=>9); my $loglevel = "DEBUG"; print "level set to: $loglevel -- $levels{$loglevel}\n"; my $log=Logger::Simple->new(LOG=>"/var/log/tmobile.log",CARP=>'1'); $log->set("###### Start tmobile.pl v.01 ######") if $levels{"$loglevel"} > $levels{'WARN'}; my $ua = LWP::UserAgent->new; ## Just a little lie $ua->agent("Mozilla/4.0 (compatible; MSIE 5.5; MSN 2.5; Windows 98) "); $ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt", autosave => 1)); push @{ $ua->requests_redirectable }, 'POST'; $log->set("Attempting to Login") if $levels{"$loglevel"} > $levels{'WARN'}; ## ## Try to get the post url automatically by trying Google. ## And, if getting Google works, I'm lucky so quit while I'm ahead. ## ## In SoCal all the TMobile HotSpots I'd visited used "service1.hotspot.t-mobile.com" but ## in South TX I found "service3...", so let's iterate thru a bit. ## unless (can_get_google(\$ua)) { while (!login_to_tmobile(\$ua,$i) && $i < $max_tries) { $log->set("\tLogin retry # $i ... ") if $levels{"$loglevel"} > $levels{'WARN'}; $i++; sleep 3; # Let's not bang too hard... } } exit 0; # Create a request, send it through UA and get result sub login_to_tmobile{ my ($ua, $j, $post_url) = @_; $j++; $post_url = "https://service$j.hotspot.t-mobile.com//user/home" unless $post_url; print "trying $j..."; my $res = $$ua->request(POST $post_url, Content_Type => "application/x-www-form-urlencoded", Content => [ username => $username, password => $password, ]); #Check the outcome of the response if ($res->is_success) { $log->set("Login successful!\n $res->content"); # always log print "Got it!\n"; return 1; } $log->set("Post failed with $res->status_line"); # always log print " nope: ",$res->status_line,"\n"; return 0; } # Try Google, if I get it, quit # Otherwise, grab the post URl from what should be the # T-Mobile login page and use it to login. Any failure sends # me to the regular incremental login method sub can_get_google{ my ($ua) = @_; print "trying to get lucky..."; my $res = $$ua->request(GET "http://www.google.com"); #Check the outcome of the response if ($res->is_success) { if ($res->content =~ m|http://images.google.com/|) { $log->set("Already logged in and connected"); print "Already logged in!\n"; return 1; } else { # name="postURL" value="https://service1.hotspot.t-mobile.com//user/home?t=dog2g830" my ($post_url) = $res->content =~ /name="postUrl" value="([^"]+)"/; return login_to_tmobile($ua,0,$post_url); } } $log->set("Get Google failed with $res->status_line"); # always log print " Not lucky: ",$res->status_line,"\n"; return 0; }