Skip to content

Instantly share code, notes, and snippets.

/p5

Created October 22, 2015 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e329f07eca6b0f894162 to your computer and use it in GitHub Desktop.
Save anonymous/e329f07eca6b0f894162 to your computer and use it in GitHub Desktop.
[jdv@wieldy ~]$ cat test_login_gh.pl
use strict;
use warnings;
use LWP::UserAgent;
use HTML::TreeBuilder;
my ( $user, $pass, ) = @ARGV;
my $ua = LWP::UserAgent->new(
requests_redirectable => [ 'GET', 'HEAD', 'POST' ]);
$ua->cookie_jar({ file => "$ENV{HOME}/.cookies.txt" });
my $html = HTML::TreeBuilder->new_from_content(
$ua->get('https://github.com/login')->decoded_content);
my %form = map {
$_->attr('type') eq 'submit' ? () : ($_->attr('name'), $_->attr('value'));
} $html->look_down(qw(_tag form action /session))->look_down(qw(_tag input));
@form{qw(login password)} = ($user, $pass);
my $resp = $ua->post( 'https://github.com/session', \%form );
print +($resp->decoded_content =~ /Signed in as.*jdv/ ? "" : "Not ")
. "Logged In\n";
[jdv@wieldy ~]$
[jdv@wieldy ~]$ cat test_login_gh.p6
use v6;
use HTML::Parser::XML;
use HTTP::UserAgent;
my ($user, $pass) = @*ARGS;
my $ua = HTTP::UserAgent.new;
my $html = HTML::Parser::XML.new.parse(
$ua.get('https://github.com/login').content);
my %form;
%form{.<name>} = .<value>
for $html.getElementsByTagName('form').[1].elements(:TAG<input>, :RECURSE);
%form{<< login password>>} = ($user,$pass);
say %form;
my $resp = $ua.request(
HTTP::Request.new(POST => 'https://github.com/session', |%form));
say $resp.perl;
[jdv@wieldy ~]$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment