Jan
03
2008
To use so you need LWP::Protocol::http::SocksChain.
There is a number of such protocol packages, but usually, installing any one of them requires a number of other prerequisites:
Here is a process I have tested, if you do have super user privilege, you can ignore where "/path/to/install" is mentioned
0. $ sudo -s
0.1 setenv PERL5LIB /path/to/install/lib/site_perl
1. INSTALL OPENSSL
1.1 http://www.openssl.org/source/,
download http://www.openssl.org/source/openssl-0.9.8g.tar.gz
1.2 Unzip openssl-0.9.8g.tar.gz
1.3 Read INSTALL
1.4 $ ./config
1.5 $ make
1.6 $ make test
1.6 $ make install
2. INSTALL Net::SSLeay
2.1 Download it from http://search.cpan.org/~flora/Net-SSLeay-1.32/,
download http://search.cpan.org/CPAN/authors/id/F/FL/FLORA/Net-SSLeay-1.32.tar.gz
2.2 Unzip Net-SSLeay-1.32.tar.gz
2.3 $ perl Makefile.PL PREFIX=/path/to/install
2.4 $ make install # the Makefile requires -lz, on certain system, you can remove it
2.5 $ cd examples
2.6 $ get_page.pl www.cryptsoft.com 443 /
3. INSTALL IO::Socket::SSL
3.1 Download it from http://search.cpan.org/dist/IO-Socket-SSL/,
download http://search.cpan.org/CPAN/authors/id/S/SU/SULLR/IO-Socket-SSL-1.12.tar.gz
3.2 Unzip IO-Socket-SSL-1.12.tar.gz
3.3 $ perl Makefile.PL PREFIX=/path/to/install
3.4 $ make
3.5 $ make test
3.6 $ make install
4. INSTALL Net::SC
4.1 Download it from http://search.cpan.org/~gosha/Net-SC-1.20/,
download http://search.cpan.org/CPAN/authors/id/G/GO/GOSHA/Net-SC-1.20.tar.gz
4.2 Unzip Net-SC-1.20.tar.gz
4.3 $ perl Makefile.PL PREFIX=/path/to/install
4.4 $ make
4.5 $ make test
4.6 $ make install
5. INSTALL
5.1 Download it from http://search.cpan.org/~gosha/LWP-Protocol-http-SocksChain-1.4/,
download http://search.cpan.org/CPAN/authors/id/G/GO/GOSHA/LWP-Protocol-http-SocksChain-1.4.tar.gz
5.2 Unzip LWP-Protocol-http-SocksChain-1.4.tar.gz
5.3 $ perl Makefile.PL PREFIX=/path/to/install
5.4 $ make
5.5 $ make test
5.6 $ make install
Now you can use LWPGet
#!/usr/bin/perl -w
require 5.8.0;
use strict;
use lib "/path/to/install/lib/site_perl/5.8.7";
use LWP::Simple; use LWP::UserAgent;
use LWP::Protocol::http::SocksChain;
LWP::Protocol::implementor( http => 'LWP::Protocol::http::SocksChain' );
@LWP::Protocol::http::SocksChain::EXTRA_SOCK_OPTS = (
Chain_Len => 1,
Debug => 0,
Random_Chain => 1,
Chain_File_Data => [
'ip_of_socks_proxy:port:::5',
],
Auto_Save => 0,
Restore_Type => 0 );
my $ua = new LWP::UserAgent $ua->agent('Mozilla/4.0 (compatible; MSIE 6.0; Windws NT 5.1)');
my $Url = $ARGV[0];
my $response = $ua->get($Url);
if($response->is_success) {
my $page = $response->content;
print $page, "\\n";
} else {
print STDERR "Fail to get $Url\\n";
}
Dec
21
2007
The original post can be found at http://www.perlmonks.org/?node_id=464358
I have made slight changes
(1) Put the following in a file "cfg.cfg"
%CFG = (
'servers' => {
'SRV1' => {
'IP' => '99.32.4.0',
'user' => 'aname',
'pswd' => 'p4ssw0rd',
'status' => 'unavailable'
},
'SRV2' => {
'IP' => '129.99.10.5',
'user' => 'guest',
'pswd' => 'guest',
'status' => 'unavailable',
},
},
);
(2) The following will load the config file and print out SVR1's IP
#!/usr/bin/perl
use strict;
our (%CFG);
# Read a configuration file
sub ReadCfg
{
my $file = $_[0];
our $err;
{ # Put config data into a separate namespace
package CFG_PKG;
# Process the contents of the config file
my $rc = do($file);
# Check for errors
if ($@) {
$::err = "ERROR: Failure compiling '$file' - $@";
} elsif (! defined($rc)) {
$::err = "ERROR: Failure reading '$file' - $!";
} elsif (! $rc) {
$::err = "ERROR: Failure processing '$file'";
}
}
return ($err);
}
# Get our configuration information
if (my $err = ReadCfg('cfg.cfg')) {
print(STDERR $err, "\\n");
exit(1);
}
print "SRV1's IP: ", $CFG_PKG::CFG{'servers'}{'SRV1'}{'IP'}, "\\n";
Dec
12
2007
To count occurrence of a character, say "\", in a string:
#!/usr/bin/perl -w
use strict;
my $url='http://www.aaa.com/bbb/ccc/ddd/eee/fff/aaa';
my $k = ($url =~ tr/\\//\\//);
print "cnt = $k\\n";
Oct
31
2007
Sometimes we need to start a background process to do a long-running job, while the parent process may die before the child dies. To do so in Perl, you can use the following code:
#!/usr/bin/perl -w
use strict;
use POSIX qw(:sys_wait_h);
my $kidpid;
if (!defined($kidpid = fork())) {
# fork returned undef, so failed
die ("Failed to fork");
exit;
} elsif ($kidpid == 0) {
# fork returned 0, so this branch is child
exec("./longjob.pl");
exit;
} else {
# parent
$SIG{CHLD} = sub { 1 while ( waitpid(-1, WNOHANG)) > 0 };
print "Parent exited\\n";
exit;
}
If the parent is a web server process and writes back a html page, it might not return immediately. The solution is to close the STDIN, STDOUT, and STDERR in the child process.
#!/usr/bin/perl -w
use strict;
use POSIX qw(:sys_wait_h);
my $kidpid;
if (!defined($kidpid = fork())) {
# fork returned undef, so failed
die ("Failed to fork");
exit;
} elsif ($kidpid == 0) {
# fork returned 0, so this branch is child
close (STDIN);
close (STDOUT);
close (STDERR);
exec("./longjob.pl");
exit;
} else {
# parent
$SIG{CHLD} = sub { 1 while ( waitpid(-1, WNOHANG)) > 0 };
print "Parent exited\\n";
exit;
}
Oct
26
2007
Use CGI package:
#!/usr/bin/perl -w
use strict;
use CGI qw/:standard/;
my $cgivars = new CGI;
my @names = $cgivars->param;
foreach (@names) {
print "$_ => ", $cgivars->param($_), "\\n";
}
Oct
10
2007
my_pkg.pm
package my_pkg;
use warnings;
use strict;
our (@ISA, @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(subName $varName);
our $varName = "XXXXX";
sub subName($) {
......
}
my_caller.pl
#!/usr/bin/perl -w
use strict;
use my_pkg;
print "$varName";
subName($varName);