Nalina has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks

I am using Net::SSH::Perl module to connect to a firewall from a windows 2000 machine.Following is my script

#!/usr/local/bin/perl -w # Steps: # 1. login # 2. go to enable mode ('en') # 3. execute 'no pager' to disable paging # 4. execute 'sh access-list access-list-name' # 5. Get hit counts # 6. logout $|++; use strict; use Net::SSH::Perl; use Net::SSH::Perl::Constants qw( :msg ); use constant SKIP_PROMPT => 1; # pix prints login prompt twice, skip + first my $host = shift || die "Usage: $0 pix_name\n"; my $time2login = 10; my $time2run = 20; my $file = "C:\\hit_cnt.txt"; open (DAT, ">>$file") || die "can not open"; # modify these in case of prompt (hostname) changes # assuming alphanumeric characters only: # [a-zA-Z0-9] is actually \w, but some hosts have '_' or '-' in their +names my $enb_prompt = qr/(?:[a-zA-Z0-9]+#)\s*/; # alphanumeric followed b +y '#' my $reg_prompt = qr/(?:[a-zA-Z0-9]+>)\s*/; # alphanumeric followed b +y '>' my $pass_prompt = qr/Password:\s*/; my ($prompt_cnt,$save,$done) = (0,0,0); my ($ssh, @config); # login on the device eval { local $SIG{'ALRM'} = sub { die 'TimedouT' }; alarm $time2login; $ssh = Net::SSH::Perl->new($host, protocol=>1, cipher=>'DES', port= +>22); $ssh->login('USER-NAME','PASSWD'); alarm 0; }; ($@)? ( die '[',scalar localtime,'] ', ($@ =~ /TimedouT/)? "Takes too long to login on $ho +st.\n" : "Unexpected eval err: $@.\n" ) : undef; # set up handler and intercept everything that goes to STDOUT $ssh->register_handler(SSH_SMSG_STDOUT_DATA, sub { my($ssh, $packet) = @_; my $str = $packet->get_str; print DAT "$str"; if ( $save ) { # reading config if ( $str =~ /$enb_prompt$/ ) { # last line of the config + p +rompt my $packet = $ssh->packet_start(SSH_CMSG_STDIN_DATA); $packet->put_str('exit ' . "\n"); $packet->send; $done++; } $str =~ s/\cM//g; chomp $str; # skip echo of the command and logout sequence push @config, $str unless ( $done || $str =~ /^(\w|\s)$/ +|| $str =~ /^:/ || $str eq '' ); } else { # login part if ($str =~ /$reg_prompt$/) { # go to enable mode $prompt_cnt++; # pix prints login prompt twice +, remember return unless $prompt_cnt > SKIP_PROMPT; my $packet = $ssh->packet_start(SSH_CMSG_STDIN_DATA); $packet->put_str('enable' . "\n"); $packet->send; $prompt_cnt = 0; # will resuse it in enable mode + } elsif ( $str =~ /$pass_prompt$/ ) { # going into enable mode.... my $packet = $ssh->packet_start(SSH_CMSG_STDIN_DATA); $packet->put_str("PASSWD\n"); $packet->send; } elsif ( $str =~ /$enb_prompt$/ && !$prompt_cnt ) { # exec first command in enable mode my $packet = $ssh->packet_start(SSH_CMSG_STDIN_DATA); $packet->put_str('no pager' . "\n"); $packet->send; $prompt_cnt++; } elsif ( $str =~ /$enb_prompt$/ && $prompt_cnt ) { # exec second command in enable mode, ready to rock my $packet = $ssh->packet_start(SSH_CMSG_STDIN_DATA); $packet->put_str('sh access-list access-list-name' . "\n"); $packet->send; $save++; } else { # Uncomment this for debug purposes # print "Useless data: $str\n"; } } }); eval { local $SIG{'ALRM'} = sub { die 'TimedouT' }; alarm $time2run; $ssh->cmd(''); # thaaaat's right, nothing at all alarm 0; }; ($@)? ( die '[',scalar localtime,'] ', ($@ =~ /TimedouT/)? "Timed out while pulling from $ +host.\n" : "Unexpected eval err: $@.\n" ) : undef;
As I could not install Math::GMP on windows, I installed GMP::BigInt and replaced Math::GMP with Math::BigInt in all the modules. I am using CygWin bash shell. When I execute the script

I got this message with an error

$ perl ssh.pl xxx.xxx.xxx.xxx Nalina: Reading configuration data /home/Administrator/.ssh/config Nalina: Reading configuration data /etc/ssh_config Nalina: Connecting to xxx.xxx.xxx.xxx, port 22. Nalina: Remote protocol version 1.5, remote software version Cisco-1.2 +5 Nalina: Net::SSH::Perl Version 1.25, protocol version 1.5. Nalina: No compat match: Cisco-1.25. Nalina: Connection established. Nalina: Waiting for server public key. Nalina: Received server public key (768 bits) and host key (2048 bits) +. Nalina: Host 'xxx.xxx.xxx.xxx' is known and matches the host key. [Tue Jun 22 11:53:30 2004] Unexpected eval err: Undefined subroutine & +Math::BigI nt::sizeinbase_gmp called at /usr/lib/perl5/site_perl/5.8.2/Net/SSH/Pe +rl/Util/RS A.pm line 30.
What is the compatible function for sizeinbase_gmp in BigInt?

Regards

Nalina

Edited by Chady -- added code tags.

Replies are listed 'Best First'.
Re: Replcing Math::GMP with Math::BigInt
by runrig (Abbot) on Jun 22, 2004 at 07:46 UTC
    What made you think you could replace Math::GMP with Math::BigInt? And what is GMP::BigInt? Do you mean Math::BigInt::GMP?

    Also, I assume you are still using cygwin. If you use ActiveState perl instead, you might be able to install binaries for the modules (via ppm, which won't work for cygwin), instead of compiling source yourself.

      I made the suggestion based on the Math::GMP docs and a very deep thread associated with an inability to compile Net::SSH::Perl secondary to being unable to compile Math::GMP.

      DESCRIPTION Math::GMP was designed to be a drop-in replacement both for Math::BigI +nt and for regular integer arithmetic.

      cheers

      tachyon

      I could not install Math::GMP via ppm.

      These are the steps I followed

      c:\perl\bin> ppm

      ppm> install

      <http://crazyinsomniac.perlmonk.org/perl/ppm/5.8/Math-GMP.ppd>

      I got an error at the end.
      </tr> </table> <!-- image audit code --> <script LANGUAGE="JAVASCRIPT"> <!-- now = new Date(); tail = now.getTime(); document.write("<IMG SRC='http://images-aud.sourceforge.net/pc.gif?l," +); document.write(tail); document.write("' WIDTH=1 HEIGHT=1 BORDER=0>"); //--> <noscript> <img src="http://images-aud.sourceforge.net/pc.gif?l,390086" WIDTH=1 HEIGHT=1 BORDER=0> </noscript> <!-- end audit code --> <!-- end OSDN Footer --> </BODY></HTML></HTML> at C:/Perl/site/lib/PPM/PPD.pm line 177, <$__ANO +NIO__> line 4.
      Where am I wrong?

      Thanks & Regards

      Nalina
      edit - jeffa - code tags
Re: Replcing Math::GMP with Math::BigInt
by PodMaster (Abbot) on Jun 22, 2004 at 08:16 UTC
    ... irrelevant ... What is the compatible function for sizeinbase_gmp in BigInt?
    Do you know what sizeinbase_gmp does? Once you can answer that you can answer what the compatible function is.

    http://search.cpan.org/src/CHIPT/Math-GMP-2.0/GMP.xs

    int sizeinbase_gmp(n, b) mpz_t * n int b CODE: RETVAL = mpz_sizeinbase(*n, b);
    http://docs.freebsd.org/info/gmp/gmp.info.Miscellaneous_Integer_Functions.html
    
    - Function: size_t mpz_sizeinbase (mpz_t OP, int BASE) Return the size of OP measured in number of digits in base BASE. The base may vary from 2 to 36. The returned value will be exact or 1 too big. If BASE is a power of 2, the returned value will always be exact.
    ...
    Here's my guess
    sub Math::BigInt::sizeinbase_gmp { my $bigint = shift; # ? my $base = shift; require Math::BaseCnv; return Math::BaseCnv::cnv( length($bigint)-1, 10, $base (; }
    But like runrig says, this is a bad road you're going down (especially stumbling about like this). I don't know why you switched to cygwin all of a sudden, especially since you can get Math::GMP for activeperl flavored perl from here.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      There are some other functions such as

      Math::BigInt::mul_2exp_gmp, Math::BigInt::powm_gmp etc

      What do I do for these functions?

      First I tried to install Math::GMP with PPM.

      But I could not do it on windows system.

      So I switched to cygwin

      Thanks and Regards

      Nalina

        Depending on whether you have 5.8 or 5.6 run one of these commands. When you get the prompt to install the DLL install it somewhere in your path.

        #5.8 C:\>ppm install http://crazyinsomniac.perlmonk.org/perl/ppm/5.8/Math-G +MP.ppd Installing package 'http://crazyinsomniac.perlmonk.org/perl/ppm/5.8/Ma +th-GMP.ppd '... Error installing package 'http://crazyinsomniac.perlmonk.org/perl/ppm/ +5.8/Math-G MP.ppd': Read a PPD for 'http://crazyinsomniac.perlmonk.org/perl/ppm/5 +.8/Math-GM P.ppd', but it is not intended for this build of Perl (MSWin32-x86-mul +ti-thread) # OK so I don't run 5.8, and need to get the 5.6 ppd..... C:\>ppm install http://crazyinsomniac.perlmonk.org/perl/ppm/Math-GMP.p +pd Installing package 'http://crazyinsomniac.perlmonk.org/perl/ppm/Math-G +MP.ppd'... Downloading http://crazyinsomniac.perlmonk.org/perl/ppm/Math-GMP-2.03. +tar.gz ... [snip] C:\>

        cheers

        tachyon