Some problems encountered in installing Net: SSH: Perl using cpan

Source: Internet
Author: User
Tags gmp


Use cpan to install Net: SSH: Perl:
Cpan> install Net: SSH: Perl

Some problems encountered during this period are recorded here for future reference.

Because cpan is dependent on other software, the software version must not be too low, so upgrade the two modules first:


The Code is as follows: cpan> upgrade Module: Build
Cpan> upgrade ExtUtils: Install

Math: BigInt:
Math: BigInt: couldn't load specified math lib (s), fallback to Math: BigInt :: calc at/usr/lib/perl5/site_perl/5.8.8/Crypt/DH. pm line 6

Change the sixth line of the DH. PM file according to the error message:
Original: use Math: BigInt lib => "GMP, Pari ";
More: use Math: BigInt try => "GMP, Pari ";

During the installation process, when the Math: GMP module is installed, an error is reported and it is stopped. The general meaning is that the GMP. c file is missing, so now a GMP library file and a GMP Library Development Kit are installed under the system.

The Code is as follows: aptitude install libmath-gmp-perl
Aptitude search libgmp3-dev

For example, if the above Math installation fails, delete the following folder before installing it.
Rm-fr/root/. cpan/build/Math-GMP-2.06 -*

Basic syntax
1. General syntax


The Code is as follows: my $ host = "192.168.14.222 ";
My $ user = "root ";
My $ passwd = "123456 ";
My % params = (
Protocal => '2, 1 ',
Debug => '1 ',
);
Use Net: SSH: Perl;
My $ ssh = Net: SSH: Perl-> new ($ host [, % params]);
$ Ssh-> login ($ user, $ pass );
My ($ out, $ err, $ exit) = $ ssh-> cmd ($ cmd );
Print "$ out \ n ";

2. Return Characters Based on the prompt to perform interactive operations.


The Code is as follows: $ ssh-> register_handler ("stdout", sub {
My ($ channel, $ buffer) = @_;
My $ str = $ buffer-> bytes;
If ($ str = ~ /(. *) \ [Y \/n \/\? \]/I ){
$ Channel-> send_data ("y \ n ")
}}
);

3. Term: The ReadKey module obtains an interactive pseudo-terminal.


The Code is as follows: use Term: ReadKey;
ReadMode ('Raw ');
$ Ssh-> shell;
ReadMode ('restore ');

Slow connection to remote hosts

Install three things online:
YAML
Math: BigInt
Math: BigInt: GMP

YAML and Math: BigInt have been installed. After Math: BigInt: GMP is installed, test the connection to the remote host, the speed is significantly improved (the operation time remains unchanged after the remote host is connected ).

Instance

Instance 1: log on to a remote host to complete simple operations.


The Code is as follows: use strict;
Use warnings;
Use Net: SSH: Perl;
My $ host = "192.168.14.222 ";
My $ user = "root ";
My $ passwd = "123456 ";
My % params = (
Protocal => '2, 1 ',
Debug => '1 ',
);
My $ ssh = Net: SSH: Perl-> new ($ host, % params );
$ Ssh-> login ($ user, $ passwd );
My ($ out, $ err, $ exit) = $ ssh-> cmd ("ifconfig ");
Print "$ out \ n ";
$ Ssh-> cmd ("mkdir/home/user; touch/home/user/{1 .. 10}. log ");

Instance 2: log on to N remote hosts through a remote host list to complete the inquired interactive operation.
Remoto_host_list.txt:
192.168.14.222 root 123456
192.168.14.223 root 123456

Program:


The Code is as follows: use strict;
Use warnings;
Use Net: SSH: Perl;
Open HOST_LIST, "Remote_host_list.txt" or die "can't open this file \ n ";
Sub MySSH {
My ($ host, $ user, $ passwd) = split;
My % params = (
Protocal => '2, 1 ',
# Debug => '1 ',
);
My $ ssh = Net: SSH: Perl-> new ($ host, % params );
$ Ssh-> login ($ user, $ passwd );
$ Ssh-> register_handler ("stdout", sub {
My ($ channel, $ buffer) = @_;
My $ str = $ buffer-> bytes;
If ($ str = ~ /(. *) \ [Y \/n \/\? \]/I ){
$ Channel-> send_data ("y \ n ")
}}
);
$ Ssh-> cmd ("aptitude install ppp ");
Print "\ n ** $ host complete... \ n ";
};
While (<HOST_LIST> ){
MySSH
}

Note:
Install PPP one by one based on the list of remote hosts. During the actual installation process, you will be asked if you want to install some dependency packages, such as install by y, otherwise, press n

The scenario is as follows:
Do you want to continue? [Y/n/?]
You can use "register_handler" to achieve automatic interactive installation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.