I have a script which is supposed to log in to my vB account and print my security token, however doesn't seem to work globally. The logging in works perfectly just will not retrieve and print the security token for every forum I log in to. Code Below:
#!/usr/bin/perl
use LWP::UserAgent;
my $ua = LWP::UserAgent->new(agent => q{Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)});
$ua->cookie_jar({});
print("Specify site URL(e.g http://site.com/): ");
chomp(my $url = <>);
print("Username: ");
chomp(my $username = <>);
print("Password: ");
chomp(my $password = <>);
my $req = HTTP::Request->new(POST => $url . '/login.php?do=login');
$req->content_type('application/x-www-form-urlencoded');
$req->content("vb_login_username=$username&vb_login_password=$password&do=login&securitytoken=guest&vb_login_md5password=&vb_login_md5password_utf=&s=");
$ua->request($req);
my $content = $ua->get("$url/index.php")->content;
my ($securityToken) = $content =~ /value="\w{10}-\w{40}" /g;
print "Security token: $securityToken\n";
Just view your page source to find an example of the vB security token while logged in.
In $content you have all the page but we need only an input element with the attribute 'name="securitytoken"'. There are can be a few but they are the same (on unix.com at least). So it's enough to get just the first one:
my $input = $1 if $content =~ /(<input.*?securitytoken.*?>)/s;
(/s - just in case an input elements takes several lines.)
Then from this input we can get the value in the same vein:
my $securityToken = $1 if $input =~ /value="(\w{10}-\w{40})"/;
And it's necessary to check whether we really get the token - something like this:
Thank you for the response. Seems to work for a few more forums, however still not for every vB forum. I'll send you another example, read the comments in your banking.
---------- Post updated at 11:47 PM ---------- Previous update was at 11:10 PM ----------
After checking your post update the last example seems to have fixed things. But it seems I've ran in to another problem. I need the usage of gzip, deflate but with the script looking like it does below, it puts the security token extraction back where it was before.
#!/usr/bin/perl
use LWP::UserAgent;
my $ua = LWP::UserAgent->new(agent => q{Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)});
$ua->cookie_jar({});
print("Specify site URL(e.g http://site.com/): ");
chomp(my $url = <>);
print("Username: ");
chomp(my $username = <>);
print("Password: ");
chomp(my $password = <>);
$ua->default_header('Accept' => "text/xml,application/xml,applicati+on/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
'Accept-Language' => "en-us,en;q=0.5",
'Accept-Charset' => "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
'Accept-Encoding' => 'gzip,deflate',
'Keep-Alive' => "300",
'Connection' => "keep-alive",
'Referer' => "$url/index.php");
push @{ $ua->requests_redirectable }, 'POST';
my $req = HTTP::Request->new(POST => $url . '/login.php?do=login');
$req->content_type('application/x-www-form-urlencoded');
$req->content("vb_login_username=$username&vb_login_password=$password&do=login&securitytoken=guest&vb_login_md5password=&vb_login_md5password_utf=&s=");
$ua->request($req);
my $content = $ua->get("$url/index.php")->content;
my $input = $1 if $content =~ /(<input.*?securitytoken.*?>)/m;
my $securityToken = $1 if $input =~ /value="(.*?)"/;
print "Security token: $securityToken\n";
Yes. With 'Accept-Encoding' => 'gzip,deflate' you get a page in gzip form. I check this with unix.com (I hardcoded the data in the script and change the last line to