Skip to content

Instantly share code, notes, and snippets.

@fapestniegd
Created July 21, 2009 22:00
Show Gist options
  • Save fapestniegd/151622 to your computer and use it in GitHub Desktop.
Save fapestniegd/151622 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use Data::Dumper;
use FileHandle;
use YAML;
use WWW::Mechanize;
################################################################################
# This is an added kludge to work around the some behavior between linode and
# www::mechanize, where it just stopped getting the session cookie from the
# linode.com authentication page. You have to download the Export Cookies
# Plug-in from https://addons.mozilla.org/en-US/firefox/addon/8154 and log into
# linode once, export your cookies to a cookies.txt file, and then import it
# by scraping the cookies.txt with the method below. I know... weak... lame...
################################################################################
my $exported_cookies="$ENV{'HOME'}/Dropbox/cookies.txt";
my $fh = new FileHandle;
my $cookies;
if ($fh->open("< $exported_cookies")) {
while ($line = $fh->getline){
$line=~s/^M//g;
if($line=~m/.www.linode.com\s+TRUE\s+\/\s+TRUE\s+0\s+CFTOKEN\s+(.*)/){
$cookies->{'cftoken'}=$1;
}
if($line=~m/.www.linode.com\s+TRUE\s+\/\s+TRUE\s+0\s+CFID\s+(.*)/){
$cookies->{'cfid'}=$1;
}
}
$fh->close;
}
exit unless(defined($cookies->{'cfid'})&&defined($cookies->{'cftoken'}));
################################################################################
# Then you can get on with your www::mechanize session
my $mech = WWW::Mechanize->new(agent => 'WWW::Mechanize-1.34', 'cookie_jar' => {});
$mech->add_header( Cookie => "CFID=$cookies->{'cfid'}; CFTOKEN=$cookies->{'cftoken'}");
$mech->get( "https://www.linode.com/members/linode" );
print $mech->response->content();
print $mech->cookie_jar->as_string;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment