posts - 45, comments - 111, trackbacks - 0, articles - 0

转载 通过cpan安装Net::SSH::Perl

Posted on 2011-08-09 21:24 Morya 阅读(270) 评论(6) 编辑 收藏

转载自:http://my.chinaunix.net/space.php?uid=171341&do=blog&id=204449

通过cpan安装Net::SSH::Perl


  1. cpan>install Net::SSH::Perl



在安装过程中,本人遇到了一些问题,记录如下


因为cpan对其它软件的依赖性,要求软件版本的不能过低,所以先升级一下这两个模块:

  1. cpan>upgrade Module::Build
  2. cpan>upgrade ExtUtils::Install


Math::BigInt报错:

  1. 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

按照报错信息,把DH.PM这个文件的第六行做更改:
原为:use Math::BigInt lib => “GMP,Pari”;
更为:use Math::BigInt try => “GMP,Pari”;


在安装过程中,装到Math::GMP模块时,报错而停止。 大概意思是缺少GMP.c文件,故现在系统下安装一个GMP库文件,和一个GMP库的开发包。

  1. aptitude install libmath-gmp-perl
  2. aptitude search libgmp3-dev


如前面安装Math:GMP失败过,先把如下文件夹删掉后,再安装吧。

  1. rm -fr /root/.cpan/build/Math-GMP-2.06-*




基本语法

1. 一般语法

  1. my $host = "192.168.14.222";
  2.     my $user = "root";
  3.     my $passwd = "123456";
  4.     my %params = (
  5.     protocal => '2,1',
  6.     debug => '1',
  7.     );
  8.     use Net::SSH::Perl;
  9.     my $ssh = Net::SSH::Perl->new($host[,%params]);
  10.     $ssh->login($user, $pass);
  11.     my($out, $err, $exit) = $ssh->cmd($cmd);
  12.     print "$out\n";


2. 根据提示信息,返回字符, 可实现交互式操作。

  1. $ssh->register_handler("stdout",sub{
  2.         my($channel,$buffer) = @_;
  3.         my $str = $buffer->bytes;
  4.         if($str =~ /(.*)\[Y\/n\/\?\]/i){
  5.               $channel->send_data("y\n")
  6.            }}
  7.     );



3. Term::ReadKey模块   得到一个互动的伪终端

  1. use Term::ReadKey;
  2.     ReadMode('raw');
  3.     $ssh->shell;
  4.     ReadMode('restore');





解决连接远程主机慢的问题

网上说要安装三个东西:
YAML
Math::BigInt
Math::BigInt::GMP

之前已经安装完 YAML 和  Math::BigInt 了,在装完 Math::BigInt::GMP 后测试,在与远程主机的连接过程中,速度明显提升(连接到远程主机后操作时间不变)。



实例

实例1: 登录一台远程主机,完成简单操作。

  1. use strict;
  2.     use warnings;
  3.     use Net::SSH::Perl;
  4.     my $host = "192.168.14.222";
  5.     my $user = "root";
  6.     my $passwd = "123456";
  7.     my %params = (
  8.     protocal => '2,1',
  9.     debug => '1',
  10.     );
  11.     my $ssh = Net::SSH::Perl->new($host,%params);
  12.     $ssh->login($user,$passwd);
  13.     my ($out,$err,$exit) = $ssh->cmd("ifconfig");
  14.     print "$out\n";
  15.     $ssh->cmd("mkdir /home/user;touch /home/user/{1..10}.log");




实例2:通过一个远程主机列表,登录N台远程主机,完成可提问式的互动操作。

远程主机列表文件Remoto_host_list.txt文件:
192.168.14.222 root 123456
192.168.14.223 root 123456

程序:

  1. use strict;
  2.     use warnings;
  3.     use Net::SSH::Perl;
  4.     open HOST_LIST, "Remote_host_list.txt" or die "can`t open this file\n";
  5.     sub MySSH{
  6.     my ($host,$user,$passwd) = split;
  7.     my %params = (
  8.     protocal => '2,1',
  9.     # debug => '1',
  10.     );
  11.     my $ssh = Net::SSH::Perl->new($host,%params);
  12.     $ssh->login($user,$passwd);
  13.     $ssh->register_handler("stdout",sub{
  14.         my($channel,$buffer) = @_;
  15.         my $str = $buffer->bytes;
  16.         if($str =~ /(.*)\[Y\/n\/\?\]/i){
  17.               $channel->send_data("y\n")
  18.           }}
  19.     );
  20.     $ssh->cmd("aptitude install ppp");
  21.     print "\n** $host complete...\n\n";
  22.     };
  23.     while(<HOST_LIST>){
  24.     MySSH
  25.     }


说明: 
     根据给出的远程主机列表文件,来逐一完成安装PPP的操作,在实际的安装过程中,会出现询问,是否安装一些依赖包,如安装按y,否则按n 

情景如下:
Do you want to continue? [Y/n/?]

用"register_handler",可实现交互式的自动安装。

Feedback

#1楼[楼主]  回复 引用 查看   

2011-08-10 09:03 by Morya      
安装 Math::pari 的方法
perl Makefile.PL PREFIX=/home/morya/myperl LIB=/home/morya/lib pari_tgz=pari-2.1.7.tgz

#2楼[楼主]  回复 引用 查看   

2011-08-10 09:23 by Morya      
# loading the completion functions


	case "$BASH_VERSION" in
	[2-9].*)
	    if test -e $HOME/.bash_completion ; then
		. $HOME/.bash_completion
	    elif test -e /etc/bash_completion ; then
		. /etc/bash_completion
	    elif test -s /etc/profile.d/complete.bash ; then
		. /etc/profile.d/complete.bash
	    fi
	    for s in /etc/bash_completion.d/*.sh ; do
		test -r $s && . $s
	    done
	    ;;
	*)  ;;
	esac

#3楼[楼主]  回复 引用 查看   

2011-08-10 09:23 by Morya      
# the real completion file
# yast2 completion
# A Christmas gift from Carsten Hoeger

YAST=/sbin/yast
# build a list of know yast modules
MODLIST=($(LC_ALL=C $YAST -l| grep '^[a-z]' | grep -v "Available"))

_yast2 ()
{
        local cur prevprev prev len idx mod MODOPTS line opt rest
        MODOPTS=()

	if [[ ${#COMP_WORDS[@]} -gt 4 ]]; then
		return 0
	fi
        cur=${COMP_WORDS[COMP_CWORD]}
        prev=${COMP_WORDS[COMP_CWORD-1]}
	if [[ ${#COMP_WORDS[@]} -ge 3 ]]; then
	    prevprev=${COMP_WORDS[COMP_CWORD-2]}
	fi
        if [[ $cur == '-' ]]; then
                COMPREPLY=(-h -l -g -s)
                return 0
        fi

	# iterate through all yast modules
	for mod in ${MODLIST[@]}; do
	    # if argument before last argument is a yast module, 
	    # check it's available options
	    if [[ -n $prevprev && $prevprev == $mod ]]; then
		# build option list
		# prev is a module option
                while read line ; do
                    case "$line" in
                        Options:*)
                        while read opt rest ; do
                            case "$opt" in
                                "") break 2 ;;
                                *)  MODOPTS=("${MODOPTS[@]}" "$opt")
                            esac
                        done
                        ;;
                    esac
                done < <(LC_ALL=C $YAST $mod $prev help 2>&1)
		len=${#cur}
		idx=0
		for pval in ${MODOPTS[@]}; do
		    if [[ "$cur" == "${pval:0:$len}" ]]; then
                        COMPREPLY[$idx]=$pval
                        idx=$[$idx+1]
		    fi
		done
		return 0
	    fi
	    # previous option is a known yast module?
	    if [[ $prev == $mod ]]; then
		# build option list
                while read line ; do
                    case "$line" in
                        Basic\ Syntax:*)
                        while read rest rest opt rest ; do
                            case "$opt" in
                                \<*\>) ;;
                                "") break ;;
                                *)  MODOPTS=("${MODOPTS[@]}" "$opt")
                            esac
                        done
                        ;;
                        Commands:*)
                        while read opt rest ; do
                            case "$opt" in
                                "") break 2 ;;
                                *)  MODOPTS=("${MODOPTS[@]}" "$opt")
                            esac
                        done
                        ;;
                    esac
                done < <(LC_ALL=C $YAST $mod help 2>&1)
		len=${#cur}
		idx=0
		for pval in ${MODOPTS[@]}; do
		    if [[ "$cur" == "${pval:0:$len}" ]]; then
                        COMPREPLY[$idx]=$pval
                        idx=$[$idx+1]
		    fi
		done
		return 0
	    fi
	done

        len=${#cur}
        idx=0
        for pval in ${MODLIST[@]}; do
                if [[ "$cur" == "${pval:0:$len}" ]]; then
                        COMPREPLY[$idx]=$pval
                        idx=$[$idx+1]
                fi
        done
        return 0
}
complete -F _yast2 yast2
complete -F _yast2 yast

#4楼[楼主]  回复 引用 查看   

2011-08-10 09:30 by Morya      
# /etc/profile.d/complete.bash

654e72644757747a326b6a79732f51724f6c674a534275684f422b7561755069374652434e716e
4b7778657965376b597279326b41576b744e46714e5a4f4d413939757665305a50444d625a7530
2b4879326a55722b6e756d656e486341414f797a776e53666b306a466a66647a772b54794b5773
6637454651464d6551716a66445345393247634c2f51442b6f4d76515368677974777354786b45
726f417745384276596941524d4747656d7773476773385a384378674b596941525a4641547039
447a444f4977697347694945623978594366695048636a35586948415743334454314c30566372
625077332f382b75377a634b51496e766566675276374549517a6c4977452b6f5850706f4d6a75
5044446c42366b417a316a4c704b42486b36684e416873446b6a71356c47473244794b4544456e
55552f2b446f375072683243775248704575754148796d3455334e3143696a4f49364668477274
7a4a6771776e4a66674e4367513031432f6d4b506a684f386a7967646a53647a7254676d646c6c
446951664347746d5343363745486169734e37745273685637562f44674e596461314667333864
42736531612f56535865726f654d714452634a67734e3470685962706e6e735a53475035513543
63637a4c65426f796f56654943382b2f674a36704c36556545666663434c794273587a31366350
707854382f665834394f705044567a512b587a656f424d77477a32414272787177643239476736
37656c524152384351442b3039676932775738516b386559496368784b584d5a47424d514f622f
516e504346455169354a596c325365697a7534592f52712b785030792b48426765577334586e44
64724d4459617872387975303064534f6a6e544e4d6b48674f6f7963446f435578595472746152
366b6f6341593250637338616d716246727449484d2f54773866662b767762686e4c4c33313241
5155574e475a576b33515536724e57417932422f597064493165462b7752644d32755a74756f71
336477664c773270557053777155317674772f7a2b576557636158616862354c4b5a707a6d4b4d
6c395a342f5242376c6a746e756c62324c4e564d367833324750763475324149625a754f2f3761
636e62772b4d52465868785475744c683238655337654f7146782f4d68504466426f45676e5538
67494e6b6e6c4d49776863624d415a4d69416d7843486c324f316a2b6e6f4c49684148516f3538
396e4a2b52715076633931625947485a65453434497a48734b3565652f6a617131394e66445872
312f45533338664c426d424e6748554463456141737762676e414234414c5658615062796c6454
41574a6853525a2f4854413661726c4530706e37766763757241376657367a4344736163495747
7577333867596f576d65442b6b637a78636b7551683838494b5559784a526a385557336d6d4456
78354d436c432f734a696c75436e5a6a6b436c5879436d455a414f564c36354c4d5633415a5065
4a4f5658754f526c6972766c4f5a3770574f5930704a754568544d4f63434d3942587636564336
302f5a56574f474e704c4a655835786e4d5138787a7145496a4d505a2f4f417936464c4d6c3776
32376a384d696e6973554b32496b65733733494274304f75315171663850347151793944575075
786b36424463726a786a75367473366d5364754b6a4437593159724a30415838756d5569497163
71734b69502b6861396e48504d33314c37587a6649346850494951305932635267367349537375
544d69464d54575049302f2f57316568546e4d444a397a4235627570614b2f42714e6849333969
76754b4354527450303746596c53686c5650584d3268326363722b2f69344c644434792f493047
544a4b6456326c727548354b38503354444c776b64576666482f654c636d743075776d6b47526f
5773734e655377646f6430565165675a49762f506e505254622f5a396c65482f39395533733776
70726133597532366253626456587476475257515951467162384647354379743857386f787571
497262523930546e6f665276627254794f306c6e6c35356b346974766f574a6c6a2f656b46347a
55797255796b6a56724f41692b77365a4465316c537752712b4870614955502b76616e71395058
623069356b757436646257665466545271707135344857396c4b664d3956646e6936747a784e62
736a586c4b59763836544554445348786676663774585a4f457843423467776a2f6158616b7058
38634e725450324749567566534e4d38704272514e427677792f7276415a4b71494749345a796e
6d626d35696178427251464b4c703642395a67335478464f30734a575542672b66446662645a43
4c35704a5764446458686f2b754f444536476874317073624970736c3865374b6b316765564877
2b73506f45754b6638664d426b6c2f756d656d674a2b6944446c7665756661734d335747597356
644346367250376e4a30683467705651386467335532524b77334b74506470656c537244585137
6e425a4a37753558674a46444370497051456e79674d764b6e64624a79696f644b4a315a683266
6e653857396b7464466c6648307a6f37746e3461507a6f356c33765177724f464e554c37634257

#5楼[楼主]  回复 引用 查看   

2011-08-10 09:30 by Morya      
# /etc/profile.d/complete.bash part2
64726262664d6153553938506e553965777539562b534f6d476d76496b44614454373366326175
72586d6f37416761724e7276526d6b5744455475572b374166754d624d6f2f5458613356427149
3775576868356175786876745137625a5a4e694e494651667052784a4b72456630657172505051
4355524468444b736a4d49396554396b54306a624c53454e50305a6d777632795656436c4b4372
6a717070494c6158735258425a4e6132647869586c6a376369617547362f6250662b383635315a
557468577752797673614745744651437654667748515a4e565a76616a617133796c5771736153
315435567063524e61524a6a4256396d6367723244566333514757615273576d4454686972344b
6a4d7a52734d4148584e46584163625543544b4251706c6c73563336794d474c47505932644557
58635169776b324677376159683153697169784c7947676352462f4c53793645575455614e436f
75435a45644c4c5945676a4d6853374876364d4f4b4154706277386b3675466c6e63476d57424b
397371756d2f433975696774544b4e785541426d4d713967473467765743574a7667393539516e
59746556516854765935326a767a42445a7167534f703955544743577367545152387876626f6d
58396230576a74306f6441554e796b7378484537794d4d7277444a652b76553359566e34363732
586e3263524c6d62534b556e59657932654c6f6c7744755545454f6c46316462546b5049357547
375162686c61685231506c305661704e46626f516e794c536834596e46672b327869574661674e
466d79794530534656467654754958454a6a6f6c4c376e52465967634c5a6d683630532b614245
564c70566e5359356132464a5a4c6347646c624834756f5574563662654a6758674a6769394142
76334945396b37513178364446564d4e7776494d726f336f5a324354316d2f6b522f2b326e3035
63323739305071736c5841464743382f665268365054706241714b4e42564e4539483255354679
79524b3647736859464f5069437557545644334b5855567a707a78487a68674a2b465765624a56
46342f4b4b3456627550677241685a41554f3352422f313662576542714a646a4a552b702f6a43
58315a4e5a432f34467466496e53706a4e36467a694a774e4f3339783459505a6d6a67784e3378
6c4c3941742f2b346755773330566c483762707144414744494a77436950416b394b3842754852
6f484f44495349553443597054376851697546576c686f4c79464b36724a44554e6e6d75795430
486f6172734b6b76476d4f452b765078342b764c4c327a70587a6c4854417567344c3579786730
592f5653717137446766644a7863704d37587738505066794f73704a41676e7244344a6f7a6251
4247344b5374425369484d666c7471664133736469376c5176554c396959384d71467330397359
72366f577a35375a5038666e4b2f576775344c4e4b7a394671736b69716443447136777561395a
6f5a64753148366c4132466e5646566e583670594645685761326830464e44457765764b344d6f
7a3873445357382f58613441366861456b77376c494c4a646a32583079305451766b6f54423645
63627a772b6d764c31574a557247747871725549484730304d37766875637378636d34727a5471
573250735845394f5a6b6669704738354c326955484748394f43356d4972574b636b6a65764a48
564f2b61534a56684a744252592b44782b5845786a72652f67734439385555486c47564d587871
4a785556775659504f7930434a69773750754c37626d7370417236717a71736c634e5665616851
3344486b323035573354665356765a55745277534373336c61712b5957645471505a4a52562f56
65334c514b756b774938716f55355a776d5074644446764b476772773875633939574f652b6b57
742b6747742b71564d6c37394b446a456e384f6d3950353469335838416979306936773d3d

#6楼[楼主]  回复 引用 查看   

2011-08-10 22:54 by Morya      
貌似下面这句话可以成功编译libssh-2.1.8

./configure --with-libssl-prefix=$HOME/usr --enable-shared=no --prefix=$HOME | tee compile001.log

之前编译 openssl 1.0.0b 的命令行
./config --prefix=$HOME/usr shared
make && make test && make install