Net::GitHub::V3 and Proxy settings - perl

I am trying to connect to github using Net::GitHub
my code is something like the below :
use Net::GitHub::V3;
my $gh = Net::GitHub::V3->new(
login => 'myuser', pass => 'mypassword'
);
my $search = $gh->search;
my %data = $search->repositories({ q => 'repname' });
i get the following error message :
Can't connect to api.github.com:443 at Net/GitHub/V3/Search.pm line 27.
worth to mention i am using this mdoule behind a proxy
do someone familiar how to solve this issue ?

I just uploaded a new version of Net::GitHub to CPAN. 0.69.
now you can do something like
$gh->ua->proxy('https', 'socks://127.0.0.1:9050');
check examples/proxy.pl $gh->ua is LWP::UserAgent.
Thanks

Related

Error: ' Can't use string ("") as a HASH ref while "strict refs" ' in combination with google recaptcha v2. Works on local machine but not on webhost

I am very new to Perl and was just assigned the quick task to change the reCAPTCHA to reCAPTCHA v2.
On my local machine this works fine, however when I push the new version of the login form to the webhost, the form does not seem to work. I get the following error:
AH01215: Can't use string ("") as a HASH ref while "strict refs" in use at login_new.cgi line 68, line 1.: [........]
The code snippet is based on the documentation I found here: https://metacpan.org/pod/Captcha::reCAPTCHA::V2
And according to the error log my error is somewhere in here (line 68):
if ($submit) {
my $response = $cgi->param('g-recaptcha-response');
my $result = $captcha->verify($captcha_private_key, $response );
if ($result->{success}) # This is line 68
{....}
This is quite confusing to me, especially since it is working on my local machine. Could you please help? Best regards and thank you in advance!
The Verify Function has a bug. I just looked at the implementation. in case that "$res" isn't 'successful' it wont give you back the wanted hash reference.
As a quick workaround I would check if your my $result is equal to "" if thats the case the verify function failed.
In the long run you should probably submit that bug to the creator of the module.
disclaimer: I havent downloaded the module and just looked at the source without actually trying my workaround so take it with a grain of salt and try it in a safe environment first
sub verify {
my ($self, $secret, $response, $remoteip) = #_;
# ... more code here, removed for better readability
my $res = $self->{ua}->post_form(
$self->{verify_api},
$params
);
if ($res->{success}) {
my $content = decode_json $res->{content};
if ($content->{success}){
return { success => 1 };
} else {
return { success => 0, error_codes => $content->{'error-codes'} };
}
}
}

Using Bitcoin's sendmany function with perl

I have been struggling with this one for a while and will be grateful if someone could offer a solution. Basically, I am trying to use bitcoin's 'sendmany' function (https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list) to send mass payment with a perl script. I am running bitcoind on a vps and other functions are working fine but sendmany isn't. This is the code that I have :
use Finance::Bitcoin::API;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
use Data::Dumper;
my %recipients = ("ADDRESS1" => sprintf("%.8f",0.00005460)+0, "ADDRESS2" => sprintf("%.8f",0.00005460)+0);
my $uri = "https://$rpcuser:$rpcpass\#$rpcip:8332/";
my $api = Finance::Bitcoin::API->new( endpoint => $uri );
my $action = $api->call('sendmany','',%recipients);
if ($api->error)
{
print Dumper($api->error->{message})."\n";
}
else
{
print Dumper($action)."\n";
}
I am able to send single payments using the 'sendtoaddress' function and I am able to use the sendmny function directly in the vps that is running bitcoind by executing it from the shell, but it fails when I try it using the perl script above. There is no error message, I just get the instructions for using sendmany from shell and using curl.
I am also open to scripts in any other language that will let me execute sendmany.
Thanks for the help.
I finally figured out the error in the above code. Replace the line my $action = $api->call('sendmany','',%recipients); with my $action = $api->call('sendmany','',\%recipients);
Basically just add a forward slash before %recipients. Hope this helps someone.

How do I change the root password on a remote machine with Net::OpenSSH?

I want to change the root password on a remote Linux machine with a Perl script. My first try was the following code:
use Net::OpenSSH;
my $ssh = Net::OpenSSH->new(
"linuxpc",
user => "root",
password => "root",
master_stderr_discard => 1
);
my #changepass = $ssh->capture(
{
stderr_discard => 1,
stdin_data => "newpw123"
},
"passwd"
);
print "Done\n";
But unfortunately it won't work. Could somebody help me please?
Net::OpenSSH distribution includes a sample script that does exactly what you want!
change_passwd.pl
Instead of discarding your errors, use capture2:
($output, $errput) =
$ssh->capture2(\%opts, #cmd)
captures the output sent to both stdout and stderr by #cmd on the
remote machine.
Quoted from CPAN
Also, might not be relevant, but perhaps use the full path to passwd. I am not sure whether a newline is added by the capture function, but it could be worth trying:
my #pwd = ("newpw123\n", "newpw123\n");
($output, $errput) = $ssh->capture2( { stdin_data = \#pwd }, "/bin/passwd" );
ETA: And of course, check the errors to see what's going on. Discarding errors while debugging is Bad Practice (tm).
ETA2: Try using the --stdin option for passwd, see if that helps. E.g.:
$ssh->capture2( { stdin_data = \#pwd }, "/bin/passwd --stdin" );

How to pass a name-value pair as an argument in Perl

I think this is very basic perl question but I am not getting it through.
I am using LWP::UserAgent package to build a post request in perl script.
The code is as follows:
my $urlStr = "http://localhost/testproj/AServlet";
my $postDataStr = "{name => \'ankur434\'}";
my $response = $ua->post($urlStr, $postDataStr);
The above code doesn't work & gives following error -
<Dec 6, 2010 3:15:54 PM IST> <Error> <HTTP> <BEA-101215> <Malformed Request "/testproj/AServlet". Request parsing failed, Code: -1>
However when I directly pass postDataStr's value to post method, it works perfectly well, like shown below:
my $response = $ua->post($urlStr, {name => 'ankur434'});
I tried few options like escaping { with backward slash (\{) but nothing worked...
Can anyone suggest something? Thanks!
{name => 'ankur434'}
is a hash reference.
"{name => \'ankur434\'}"
is, as your variable name indicates, a string.
Perl does not automatically convert the latter to the former.
You seem to be under the impression that in Perl, only strings can be assigned to variables or passed to subroutines.
This is not the case. Just use
my $urlStr = "http://localhost/testproj/AServlet";
my $postData = {name => 'ankur434'};
my $response = $ua->post($urlStr, $postData);

How can I log in to YouTube using Perl?

I am trying to write a Perl script to connect to me YouTube account but it doesnt seem to work. Basically I just want to connect to my account but apparently it is not working. I don't even have an idea on how I could debug this! Maybe it is something related to https protocol?
Please enlighten me! Thanks in advance.
use HTTP::Request::Common;
use LWP::UserAgent;
use strict;
my $login="test";
my $pass = "test";
my $res = "";
my $ua = "";
# Create user agent, make it look like FireFox and store cookies
$ua = LWP::UserAgent->new;
$ua->agent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.12) Gecko/20051213 Firefox/1.0.7");
$ua->cookie_jar ( {} );
# Request login page
$res = $ua->request(GET "https://www.google.com/accounts/ServiceLogin?service=youtube&hl=en_US&passive=true&ltmpl=sso&uilel=3&continue=http%3A//www.youtube.com/signup%3Fhl%3Den_US%26warned%3D%26nomobiletemp%3D1%26next%3D/index");
die("ERROR1: GET http://www.youtube.com/login\n") unless ($res->is_success);
# Now we login with our user/pass
$res = $ua->request(
POST "https://www.google.com/accounts/ServiceLoginAuth?service=youtube",
Referer => "http://www.youtube.com/login",
Content_Type => "application/x-www-form-urlencoded",
Content => [
currentform => "login",
next => "/index",
username => $login,
password => $pass,
action_login => "Log+In"
]
);
# YouTube redirects (302) to a new page when login is success
# and returns OK (200) if the login failed.
#die("ERROR: Login Failed\n") unless ($res->is_redirect());
print $res->content;
what i am doing is learning the web features of perl, so i dont want to use any library except wwwlib or mechanize to get the job done.
how can i just connect to my account using a perl script? this is my objective for now
hope someone can post a script or correct mine.
thank you guys for you help.
i am testing Webscarab now..
What data are you trying to grab? Why not just using an existing implementation like WebService::YouTube
Some comments on your code: I always avoided the shortcut $ua->request(GET/POST) method since I always ended up needing more flexibility that only the use of HTTP::Request and HTTP::Response allowed. I always felt the code was cleaner that way too.
Why is your code not working? Who knows.
Make sure your cookiejar is adding your cookies to the outgoing HTTP::Request. I'd suggest dumping all your headers when you do it in a browser and compare with the headers and data that libwww is sending. There may be some additional fields that they are checking for that vary for every hit. They may be checking for your UserAgent string. If you are just looking to learn libwww I'd suggest using a different site as a target as I'm sure YouTube has all sort of anti-scripting hardening.
Are you using YouTube's stable documented API?
Use an HTTP proxy such as WebScarab to watch the data flow.
Trey's suggestion to use somebody else's CPAN package for the mechanics is a good idea too.
Right right by and large, what you want to do is define a cookiejar for most of these websites that have a redirection login. This is what the package has done. Also the package tunes a lot of the lookups and scrapes based on the youtube spec.
Ajax content for example will be rough since its not there when your scraping
You just picked a somewhat rough page to start out with.
Enjoy
I'm actually working on this issue myself. Before, I would suggest read over this the API guide from Google as a good starting reference. If I'm reading it correctly, one begins with passing user credentials through a REST interface to get a Authentication Token. To handle that, I'm using the following:
sub getToken {
my %parms = #_;
my $response = LWP::UserAgent->new->post(
'https://www.google.com/youtube/accounts/ClientLogin',
[
Email => $parms{'username'},
Passwd => $parms{'password'},
service => "youtube",
source => "<<Your Value Here>>",
]
);
my $content = $response->content;
my ($auth) = $content =~ /^Auth=(.*)YouTubeUser(.*)$/msg
or die "Unable to authenticate?\n";
my ($user) = $content =~ /YouTubeUser=(.*)$/msg
or die "Could not extract user name from response string. ";
return ($auth, $user);
}
And I call that from the main part of my program as such:
## Get $AuthToken
my ($AuthToken, $GoogleUserName) = getToken((
username => $email, password => $password
));
Once I have these two things -- $AuthToken and $GoogleUserName, I'm still testing the LWP Post. I'm still writing this unit:
sub test {
my %parms = #_;
## Copy file contents. Use, foy's three param open method.
my $fileSize = -s $parms{'File'};
open(VideoFile, '<', "$parms{'File'}") or die "Can't open $parms{'File'}.";
binmode VideoFile;
read(VideoFile, my $fileContents, $fileSize) or die "Can't read $parms{'File'}";
close VideoFile;
my $r = LWP::UserAgent->new->post(
"http://uploads.gdata.youtube.com/feeds/api/users/$parms{'user'}/uploads",
[
Host => "uploads.gdata.youtube.com",
'Authorization' => "AuthSub token=\"$parms{'auth'}\"",
'GData-Version' => "2",
'X-GData-Key' => "key=$YouTubeDeveloperKey",
'Slug' => "$parms{'File'}",
'Content-Type' => "multipart/related; boundary=\"<boundary_string>\"",
'Content-Length' => "<content_length>",
'video_content_type'=> "video/wmv",
'Connection' => "close",
'Content' => $fileContents
]
);
print Dumper(\$r->content)
}
And that is called as
&test((auth=>$Auth, user=>$user, File=>'test.wmv'));