The following is a little script that uses LWP and friends to auto login to T-Mobile HotSpots, my new office now that I'm a contractor, which are usually at cafes like StarBucks or Borders Books. (You may now groan at the title.)

I run this from Cygwin under Windows XP Pro and it saves me as much aggravation as time; especially when (not if) the connection is lost and I have to log back in. If I was really smart I'd make this script run natively under Win32, detect the optimal network connection (100Mb Ethernet, open-access WiFi, T-Mobile HotSpot, loopback if all else fails) and setup my configuration for me thusly (change default SMTP in Outlook, update HOSTS file for external network access to intranet services, etc.). But, I'm not. I currently trigger this script manually when I am in a HotSpot zone. It works well enough.

Oh, in real life, I hard code my username and password. If someone gets my machine, my tmobile account is the least of my worries.

#!/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'}; ## ## 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. ## 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) = @_; $j++; print "trying $j..."; my $res = $$ua->request(POST "https://service$j.hotspot.t-mobile.co +m//user/home", 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; }
Update: removed unused use Data::Dumper and cleaned up gross spelling errors.

Replies are listed 'Best First'.
Re: Expresso Login
by rjamestaylor (Acolyte) on Dec 04, 2003 at 13:28 UTC
    Actually, I don't need to specify the page so explicitly; I should just make a request, say for Google.com, follow redirects and check the result. If the result is Google -- Good! If it's T-Mobile login page, then post the login based on the URL and the form action. But, I didn't do it that way. Sorry.
      Ok, since I'm at a T-Mobile Hotspot I thought I might as well try this. I used Google, but I could have used anything else. Also, I hard-coded some particulars to identify the site as Google (images.google.com; thinking that that would less likely be referred to on T-Mobile's login page and would always be on Google's page) and also hard-coded the post_url regexp for T-Mobile's login page to find out what url to bang at. Here's the updated code, kinda ugly, but I'm in a hurry and this worked:
      #!/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{'WA +RN'}; ## ## 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.hotspo +t.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"} > $le +vels{'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" unl +ess $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; }