Before reading further, do you know how to find out what Perl modules are installed on your system? Hmm...
How about "perldoc perllocal" or
sed -n -e "/head2/p" /usr/local/lib/perl/5.10.0/perllocal.pod | cut -dL -f2 | sort
Anyway, there are few other ways to do it. Make sure you know one of them.
Today, I'm given a task to write a Perl script to scan 200 servers' Interwoven, EPA, JVM and web server configurations. Normally, I'd like to use shell script to do anything related to SSH. However, with so many text files to scan why don't we use Perl to do it?
Perl SSH? Google over the Internet (thanks for the US for not censoring Google's searching result), it appears Net::SSH::Perl is a good but tough candidate. Most reviews complain about the painful installation process which makes it a challenge to me. I like pain, right? (not really)
Now, let's see what is Net::SSH::Perl.
"Net::SSH::Perl is an all-Perl module implementing an SSH client. It is compatible with both the SSH1 and SSH2 protocols.
Net::SSH::Perl enables you to simply and securely execute commands on remote machines, and receive the STDOUT, STDERR, and exit status of that remote command."
Good enough. Let's do it. (For more information about Net::SSH::Perl, see CPAN).
Installation of Net::SSH::Perl is really painful if your system doesn't have the required Math packages.
For cygwin, as of today (March 25,2010), because Perl 5.10.1 is compiled with gcc 4.3.4 (20090804) while gcc for cygwin is only at 3.4.4, the -fstack-protector gcc flags kills you. I tried to force install the module, it always stop at complaining the -fstack-protector flag when running perl script calling SSH module.
You have to recompile gcc in cygwin in order to use this module. To compile gcc, you need m4, Math::Pari, GMP, MFPR... OK, just kill me. I'm giving up.
Luckily, we have Ubuntu. With the pleasant apt-get utility, installation is much better than cygwin.
Installing Net::SSH::Perl module on Linux server (Ubuntu 9.x)
I'm using a dirty quick solution, first, let's cpan the module with force; then, we clean up the mess. Don't try it on production systems.
1. Force install Net::SSH::Perl
cpan> force install Net::SSH::Perl
2. Clean up work. Let's see the error messages at the end of installation and re-install these packages manually.
Go to .cpan/build/ and compile the following packages manually.
ILYAZ/modules/Math-Pari-2.01080604.tar.gz : writemakefile NO '/usr/bin/perl Makefile.PL INSTALLDIRS=site' returned status 2304
VIPUL/Crypt-Random-1.25.tar.gz : make_test FAILED but failure ignored because 'force' in effect
VIPUL/Crypt-Primes-0.50.tar.gz : make_test FAILED but failure ignored because 'force' in effect
VIPUL/Crypt-RSA-1.99.tar.gz : make_test FAILED but failure ignored because 'force' in effect
TURNSTEP/Math-GMP-2.06.tar.gz : make NO
TURNSTEP/Net-SSH-Perl-1.34.tar.gz : make_test FAILED but failure ignored because 'force' in effect
For GMP, because it was never installed, download it from http://gmplib.org/.
However, before we install GMP, we need to install m4...
To Install m4, we need to install (nothing)... there's always a clue that leads to another clue before you can find the treasure.
sudo apt-get install m4
download gmp from http://gmplib.org/ and compile/install it
Now, we need to check what Perl modules are installed on our server: (yes, another good interview question).
1. If "instmodsh" is available, use that.
2. Or use "perldoc perllocal". Or
3. Check the perllocal.pod directly:
root@ubuntu:~/perl# sed -n -e "/head2/p" /usr/local/lib/perl/5.10.0/perllocal.pod | cut -dL -f2 | sort
< Class::
< convert::ascii::armour|convert::ascii::armour>
< convert::asn1|convert::asn1>
< convert::pem|convert::pem>
< crypt::blowfish|crypt::blowfish>
< crypt::cbc|crypt::cbc>
< crypt::des|crypt::des>
< crypt::des_ede3|crypt::des_ede3>
< crypt::dh|crypt::dh>
< crypt::dsa|crypt::dsa>
< crypt::idea|crypt::idea>
< crypt::primes|crypt::primes>
< crypt::primes|crypt::primes>
< crypt::primes|crypt::primes>
< crypt::random|crypt::random>
< crypt::random|crypt::random>
< crypt::random|crypt::random>
< crypt::rsa|crypt::rsa>
< data::buffer|data::buffer>
< dbi|dbi>
< digest::bubblebabble|digest::bubblebabble>
< digest::hmac|digest::hmac>
< digest::md2|digest::md2>
< digest::sha1|digest::sha1>
< file::homedir|file::homedir>
< file::which|file::which>
< ipc::run3|ipc::run3>
< math::pari|math::pari>
< net::ssh::perl|net::ssh::perl>
< sort::versions|sort::versions>
< string::crc32|string::crc32>
< test::script|test::script>
< test::simple|test::simple>
< tie::encryptedhash|tie::encryptedhash>
OK. Now, it's time to check our handy Net::SSH:Perl module.
#!/usr/bin/perl
$host = localhost;
$cmd = "uname -n";
print "Trying to ssh into server $host. Please wait...\n";
use Net::SSH::Perl;
my $ssh = Net::SSH::Perl->new($host);
$ssh->login("shan","newyork1");
$ssh->shell;
root@ubuntu:~/perl# perl ssh1.pl
Trying to ssh into server localhost. Please wait...
Math::BigInt: couldn't load specified math lib(s), fallback to Math::BigInt::FastCalc at /usr/local/share/perl/5.10.0/Crypt/DH.pm line 6
Last login: Thu Mar 25 14:57:18 2010 from localhost
shan@ubuntu:~$ uname -a
uname -a
Linux ubuntu 2.6.31-14-generic #48-Ubuntu SMP Fri Oct 16 14:04:26 UTC 2009 i686 GNU/Linux
shan@ubuntu:~$ id
id
uid=1000(shan) gid=1000(shan) groups=4(adm),20(dialout),24(cdrom),46(plugdev),104(lpadmin),115(admin),120(sambashare),1000(shan)
shan@ubuntu:~$
shan@ubuntu:~$ exit
exit
logout
It opens an ssh connection!
The installation is painful but not completely bad.