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.

In reply to Expresso Login by rjamestaylor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.