Loading ...
Sorry, an error occurred while loading the content.

Re: How to login from one system (A) into remote system (B) and do a ping to the third system (C) with Perl and CGI !

Expand Messages
  • Amit Saxena
    ... Hello all, Thanks for the replies and the help. Since last few weeks, I am also trying for a solution to the same. So far, what I am trying and did is to
    Message 1 of 3 , Apr 12, 2009
    • 0 Attachment
      On Fri, Apr 10, 2009 at 11:48 AM, Zhao, Bingfeng <Bingfeng.Zhao@...>wrote:

      > With windows platform, there is also a similar way, you can use psexec
      > utility from sysinternals, now acquired by Microsoft -
      > http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx
      >
      > I had done a lot remote management with it and, of course, perl:)
      >
      > -----Original Message-----
      > From: kevin liu [mailto:lwtbenben@...]
      > Sent: Friday, April 10, 2009 10:13
      > To: Amit Saxena
      > Cc: Perl Beginners
      > Subject: Re: How to login from one system (A) into remote system (B) and
      > do a ping to the third system (C) with Perl and CGI !
      >
      > I think this depends on what kind of OS you are using.
      > If you are working between Unix like operating system, you could
      > directly
      > use
      > ssh, even remsh to log on to another system.
      >
      > On Thu, Apr 2, 2009 at 1:04 AM, Amit Saxena <learn.tech123@...>
      > wrote:
      >
      > > Hello everybody,
      > >
      > > I need to write a Perl CGI script to login from one system (A) into
      > remote
      > > system (B) and do a ping to the third system (C).
      > >
      > > As of now, I have successfully written the code in Perl CGI to Ping
      > any
      > > host
      > > / IP using built it Perl Net::Ping module.
      > >
      > > However I am not sure how to login (preferably using ssh) into a
      > remote
      > > system using Perl and execute our ping script over there.
      > >
      > > I searched on CPAN and found one module
      > > Net::SSH::Perl<
      > >
      > http://search.cpan.org/~turnstep/Net-SSH-Perl-1.34/lib/Net/SSH/Perl.pm<h
      > ttp://search.cpan.org/%7Eturnstep/Net-SSH-Perl-1.34/lib/Net/SSH/Perl.pm>
      > > >which
      > > seems to allow login into remote system and execute commands over
      > > there. However I am looking for any built-in module for the same as
      > the
      > > administrators will not allow external modules to be installed in
      > > production
      > > environment.
      > >
      > > Thanks & Regards,
      > > Amit Saxena
      > >
      >
      Hello all,
      Thanks for the replies and the help. Since last few weeks, I am also trying
      for a solution to the same.
      So far, what I am trying and did is to use the module Net::SSH in Perl on
      rhel 5. Net::SSH allows me to connect to a remote system using a username
      and host only with already setup ssh public/private keys (rsa keys without a
      passphrase). The program is working fine in the console mode but I am unable
      to connect in the Perl CGI.
      The program is copied below . The program though prints the message
      "Connected to server [####] using the user [####] and executing the command
      [uname -a] " in the output web page but it does not goes inside the while
      (<READER>) loop. In the console output, the same program works fine !
      Note :-
      1. None of the other modules which I have seen on CPAN allows me to connect
      to remote system, execute commands and get the output without putting
      hardcoded (and clear text) password in the code (which is preferred
      approach). Unless there is no other solution and this solution does not
      works, I will lookout finally for solution with the password in the code.
      2. The Perl CGI program is launched through a right-click menu from the
      output rows in IBM netcool output. IBM Netcool allows a CGI to be launched
      from the output interface.
      3. I also tried executing remote commands using ssh through system /
      backticks but it does not seems to be working.
      Please suggest.
      Code :----
      #! /usr/bin/perl -w

      use strict;
      use warnings;

      use Net::Ping;
      use Time::HiRes;
      use Net::SSH qw(sshopen2);

      use CGI;
      use CGI::Carp qw(fatalsToBrowser warningsToBrowser);

      $| = 1;
      $ENV{PATH} = "/usr/local/bin:/usr/bin:/bin";
      $ENV{HOME} = ( getpwuid $< )[ -2 ];
      $ENV{SSH_ASKPASS} = "/usr/libexec/openssh/gnome-ssh-askpass";

      my $cgi_object = new CGI;

      print $cgi_object->header;

      print $cgi_object->start_html
      (
      -title => 'IBM Tivoli Netcool/Webtop',
      -onLoad => "window.moveTo(0,0); window.resizeTo(screen.availWidth,
      screen.availHeight)"
      );

      my $host_ip_name1;
      my $number_of_ping_requests1;
      my $timeout1;

      my $Submit_Data;
      my $display_results = 1;

      if ( ( ! defined $cgi_object->param('Submit_Data') ) &&
      ( defined $cgi_object->param("\$selected_rows.Node") )
      )
      {
      $host_ip_name1 = $cgi_object->param("\$selected_rows.Node");
      if ( ( $host_ip_name1 =~ /^ *$/ ) or ( ! defined $host_ip_name1 ) )
      {
      $display_results = 0;
      }
      else
      {
      $display_results = 1;
      }
      }
      elsif ( ( ! defined $cgi_object->param('Submit_Data') ) &&
      ( ! defined $cgi_object->param("\$selected_rows.Node") )
      )
      {
      $display_results = 0;
      }
      elsif ( defined $cgi_object->param('Submit_Data') )
      {
      $display_results = 1;

      $Submit_Data = $cgi_object->param('Submit_Data');
      $host_ip_name1 = $cgi_object->param('host_ip_name');
      $number_of_ping_requests1 = $cgi_object->param('number_of_ping_requests');
      $timeout1 = $cgi_object->param('timeout');
      }

      $host_ip_name1 = "" if ! defined $host_ip_name1;
      $number_of_ping_requests1 = 4 if ! defined $number_of_ping_requests1;
      $timeout1 = 2 if ! defined $timeout1;

      print <<__STYLE__;
      <STYLE type="text/css">
      h1.headerTool
      {
      font-size: 30pt;
      font-style: italic;
      font-weight: bold;
      letter-spacing: -4px;
      }

      .headerReg, .headerTM
      {
      font-size: 12pt;
      vertical-align: super;
      }

      .headerWebtop
      {
      font-size: 28;
      font-weight: 500;
      letter-spacing: 0px;
      }

      body
      {
      background-color: #FFFFFF;
      color: #000000;
      font-family: Arial, Verdana, sans-serif;
      }

      .systemMsg
      {
      font-size: 14pt;
      font-weight: bold;
      }
      </STYLE>
      __STYLE__

      print '<h1 class="headerWebtop">IBM Tivoli Netcool <span class="headerReg">
      ®</span>/ <span class="headerWebtop">Webtop</span><span
      class="headerTM">™</span></h1>';

      print $cgi_object->hr ( { -height => 15, -align => 'center' } );

      # foreach my $key (sort keys(%ENV)) { print "[$key] = [$ENV{$key}] <br> \n";
      }
      # print "\n";
      # print "\$cgi_object->user_name() NOT DEFINED !" if ! defined
      $cgi_object->user_name();
      # print "\n";
      # my @arr1 = ( getpwuid $< );
      # print "@arr1\n";

      my $user = "#####";
      my $host = "#####";
      # my $cmd = "ping -c 4 -W 2 $host";
      my $cmd = "uname -a";

      print "Trying to connect to server [$host] using the user [$user] and
      executing the command [$cmd] <br> \n";

      if ( sshopen2("$user\@$host", *READER, *WRITER, "$cmd") )
      {
      print "Connected to server [$host] using the user [$user] and executing the
      command [$cmd] <br> \n";
      }
      else
      {
      print "Unable to connect to server [$host] using the user [$user] and hence
      unable to execute the command [$cmd] \n";
      }

      while (<READER>) {
      chomp();
      print "=>[$_] <br> \n";
      }

      close(READER);
      close(WRITER);

      print "Connecting to server [$host] using the user [$user] and executing the
      command [$cmd] done !!! <br> \n";

      # system ("ssh amit_saxena\@#### uname -a");
      `ssh amit_saxena\@#### uname -a`;
      # system ("uname -a");

      my $string_style;
      my $url1 = $cgi_object->url();
      print $cgi_object->start_multipart_form( -method => 'GET', -action => $url1
      );

      print $cgi_object->table(
      { -width => '100%', -border => 0, -cellspacing => 0, -cellpadding => 6 },
      $cgi_object->Tr(
      $cgi_object->td('Host'),
      $cgi_object->td(
      $cgi_object->textfield(
      -name => 'host_ip_name',
      -values => "$host_ip_name1",
      -size => 20,
      -maxlength => 20
      )
      ),
      $cgi_object->td('Number of Ping Requests (4-10) default 4'),
      $cgi_object->td(
      $cgi_object->popup_menu(
      -name => 'number_of_ping_requests',
      -values => ['4', '5', '6', '7', '8', '9', '10'],
      # -values => $number_of_ping_requests1,
      -default => ['4']
      )
      ),
      $cgi_object->td('Timeout (2-10) default 2'),
      $cgi_object->td(
      $cgi_object->popup_menu(
      -name => 'timeout',
      -values => ['2', '3', '4', '5', '6', '7', '8', '9', '10'],
      # -values => $timeout1,
      -default => ['2']
      )
      ),
      $cgi_object->td(
      $cgi_object->submit(
      -name => 'Submit_Data',
      -value => 'Submit'
      )
      )
      )
      );

      print "\n";

      print $cgi_object->end_form;

      if ( $display_results == 0 )
      {
      print $cgi_object->end_html;

      exit (0);
      }

      print $cgi_object->hr ( { -height => 15, -align => 'center' } );

      my $host_ip_name;
      my $number_of_ping_requests;
      my $timeout;

      if ($Submit_Data)
      {
      $host_ip_name = $cgi_object->param('host_ip_name');
      $number_of_ping_requests = $cgi_object->param('number_of_ping_requests');
      $timeout = $cgi_object->param('timeout');
      }
      else
      {
      $host_ip_name = $host_ip_name1;
      $number_of_ping_requests = $number_of_ping_requests1;
      $timeout = $timeout1;
      }

      # print "You entered : host_ip_name = [$host_ip_name],
      number_of_ping_requests = [$number_of_ping_requests]";

      # The following row is never needed but still kept for the sake of future
      changes
      # $host_ip_name = '127.0.0.1' if ! defined $host_ip_name;

      $number_of_ping_requests = 10 if ! defined $number_of_ping_requests;
      $timeout = 2 if ! defined $timeout;

      # High precision syntax (requires Time::HiRes)
      my $p = Net::Ping->new();
      $p->hires();

      my ( $ret, $duration, $ip );

      my $table_rowcount = 0;

      while (1)
      {
      my @display_body;

      $table_rowcount = $table_rowcount + 1;

      ( $ret, $duration, $ip ) = ( undef, undef, undef );
      ( $ret, $duration, $ip ) = $p->ping( $host_ip_name, $timeout );

      if ($ret)
      {
      my $success_message;
      $success_message = sprintf( "Sending packet number:%3s|", $table_rowcount
      );
      $success_message .=
      sprintf( "$host_ip_name [ip: $ip] is alive (packet return time: %.2f ms)",
      1000 * $duration );

      push( @display_body, $cgi_object->td( { -bgcolor => '#00FF00' }, [
      $cgi_object->b("$success_message") ] ) );

      # printf("$host_ip_name [ip: $ip] is alive (packet return time: %.2f
      ms)<br>\n", 1000 * $duration);

      }
      else
      {
      my $failure_message;
      $ip = $host_ip_name if ! defined $ip;
      $failure_message = sprintf( "Sending packet number:%3s|", $table_rowcount
      );
      $failure_message .= sprintf("$host_ip_name [ip: $ip] is NOT alive");

      push( @display_body, $cgi_object->td( { -bgcolor => '#FF0000' }, [
      $cgi_object->b("$failure_message") ] ) );

      # printf("$host_ip_name [ip: $ip] is NOT alive<br>\n");

      }

      print $cgi_object->table( { -border => '0' }, $cgi_object->Tr(
      \@display_body ) );

      print "\n";

      # sleep 1;

      last if ( $table_rowcount == $number_of_ping_requests );
      }

      print $cgi_object->hr;

      $p->close();

      print $cgi_object->end_html;


      *Thanks & Regards,*
      *Amit Saxena*
      * *


      [Non-text portions of this message have been removed]
    • leebinkley
      #!/usr/bin/perl use Net::SSH::Perl; my $host = perlhowto.com ; my $user = user ; my $password = password ; #-- set up a new connection my $ssh =
      Message 2 of 3 , Apr 12, 2009
      • 0 Attachment
        #!/usr/bin/perl

        use Net::SSH::Perl;

        my $host = "perlhowto.com";
        my $user = "user";
        my $password = "password";

        #-- set up a new connection
        my $ssh = Net::SSH::Perl->new($host);
        #-- authenticate
        $ssh->login($user, $pass);
        #-- execute the command
        my($stdout, $stderr, $exit) = $ssh->cmd("ls -l /home/$user");

        =====================
        Posted through Grouply, the better way
        to access your Yahoo Groups like this one.
        http://www.grouply.com/?code=post


        [Non-text portions of this message have been removed]
      Your message has been successfully submitted and would be delivered to recipients shortly.