#!/usr/bin/perl -w use strict; use Getopt::Long; use Term::ReadKey; use LWP::UserAgent; # note: requires Crypt::SSLeay to do an HTTPS request GetOptions( 'force|f' => \my $FORCE, ); my $TEST_URL = 'http://www.usc.edu'; my $LOGIN_URL = 'https://netreg.usc.edu/cgi-local/netauth'; # don't follow redirects, so we can see if we # need to log in my $ua = LWP::UserAgent->new(max_redirect => 0); # first, let's check to see whether we need to log in # we do this by sending a simple request and see if we # get redirected to the login page unless ($FORCE) { my $test_response = $ua->get($TEST_URL); unless ($test_response->is_redirect) { print "You appear to already be logged in.\n"; print "(Use the --force option to log in anyway)\n"; exit; } } # ok, we need to get a username and password print "Username: "; my $username = ; chomp $username; print "Password: "; ReadMode 'noecho'; my $password = ; ReadMode 'restore'; print "\n"; chomp $password; # construct the appropriate POST request my $req = HTTP::Request->new(POST => $LOGIN_URL); $req->content_type('application/x-www-form-urlencoded'); $req->content("username=$username&password=$password"); my $res = $ua->request($req); # check for the "You have been authenticated" message in the response # use the /s modifier on the regex so we can match \n with '.' if (my ($success_message) = $res->content =~ /(You have been authenticated.*?is registered to .*?)\n\n/s) { # remove the HTML bits (makes it look nicer in plain text) $success_message =~ s[][]gi; print "$success_message\n"; } else { printf "Login failed: HTTP error: %s (%s)\n", $res->message, $res->code; }