Results 1 to 5 of 5

Thread: Developing a tool for looking glass using Perl

  1. #1
    Join Date
    Jan 2010
    Posts
    46

    Developing a tool for looking glass using Perl

    I am currently developing a tool for looking glass. I chose Perl connections to telnet and send commands to the apparatus. It works pretty good at the moment apart from a small thing I'd like to improve.

    When I send a ping via @res=telnet->cmd("ping 192.168.1.1 count 5"); it expects to finish the command before displaying the result.
    Code:
    for($i=0;$i<@res;$i++)
    {
    	print @res[$i]."<br />";
    }
    I saw looking glass in perl (or I know) a response in real time or it displayed line by line the result, as when we make a ping to a machine.

    So I think the thread by doing:
    Code:
    $thr = threads->new(\&execute_cmd,"ping 192.168.1.1 count 5");
    sub execute_cmd 
    {
    	print "In the thread ".@_[0]."<br />";
    	@DataReferred=$telnet->cmd(@_[0]);
    	for($i=0;$i<@DataReferred;$i++)
    	{
    		print @DataReferred[$i]."<br />";
    	}
    }
    First that displays nothing and anyway that not solve my problem I think. So how?

  2. #2
    Join Date
    Nov 2008
    Posts
    1,022

    Re: Developing a tool for looking glass using Perl

    Use Net::Ping. Net::Ping checks a remote host for approachability. You can use it as follows:
    Code:
        use Net::Ping;
    
        $p = Net::Ping->new();
        print "$host is alive.\n" if $p->ping($host);
        $p->close();
    
        $p = Net::Ping->new("icmp");
        $p->bind($my_addr); # Specify source interface of pings
        foreach $host (@host_array)
        {
            print "$host is ";
            print "NOT " unless $p->ping($host, 2);
            print "reachable.\n";
            sleep(1);
        }
        $p->close();
    
        $p = Net::Ping->new("tcp", 2);
        # Try connecting to the www port instead of the echo port
        $p->port_number(getservbyname("http", "tcp"));
        while ($stop_time > time())
        {
            print "$host not reachable ", scalar(localtime()), "\n"
                unless $p->ping($host);
            sleep(300);
        }
        undef($p);
    
        # Like tcp protocol, but with many hosts
        $p = Net::Ping->new("syn");
        $p->port_number(getservbyname("http", "tcp"));
        foreach $host (@host_array) {
          $p->ping($host);
        }
        while (($host,$rtt,$ip) = $p->ack) {
          print "HOST: $host [$ip] ACKed in $rtt seconds.\n";
        }
    
        # High precision syntax (requires Time::HiRes)
        $p = Net::Ping->new();
        $p->hires();
        ($ret, $duration, $ip) = $p->ping($host, 5.5);
        printf("$host [ip: $ip] is alive (packet return time: %.2f ms)\n", 1000 * $duration)
          if $ret;
        $p->close();
    
        # For backward compatibility
        print "$host is alive.\n" if pingecho($host);
    Source: cpan.org

  3. #3
    Join Date
    Jan 2010
    Posts
    46

    Re: Developing a tool for looking glass using Perl

    Yes but the ping does not connect my web server but on a equipment (type router and switch). It is for this reason that I use telnet. Indeed my topics might be mention of this:
    Code:
    $telnet  = new Net::Telnet (Timeout=>10);
    $telnet->open($ip_juniper);
    if($telnet->login($login_juniper, $pass_juniper))
    {
    	...
    	print "You are now logged on ".$ip_juniper."<br />";
    	$thr = threads->new(\&execute_cmd,"ping 192.168.1.1 count 5");
    	...
    }
    And this gives me:
    You are now logged on 192.168.1.1

  4. #4
    Join Date
    May 2008
    Posts
    685

    Re: Developing a tool for looking glass using Perl

    Basically it is a problem of how you use Net:: Telnet you do not need thread (to avoid Perl usually). cmd () expects that the prompt is returned (so that the command has finished running) before returning the command output. Instead you must use print () to send the command and getline () to read line by line until you see the prompt again.

    Something like:
    Code:
    $telnet->print("ping blabla");
    while( my $line = $telnet->getline() ) {
      last if $line =~ m/[\$%#>] $/;
      print $line;
    }

  5. #5
    Join Date
    Jan 2010
    Posts
    46

    Re: Developing a tool for looking glass using Perl

    The concern when using getline is that I have a timeout warning. There will have no way to configure Apache to tell him to return the results as and when before the script is finished running. That will be as a 2nd problem too?

Similar Threads

  1. developing SMS Server
    By Rahuman28 in forum Software Development
    Replies: 2
    Last Post: 31-08-2011, 06:15 PM
  2. Developing .cs from .aspx
    By Balamohan in forum Software Development
    Replies: 3
    Last Post: 04-12-2009, 06:59 PM
  3. Application tool for developing Website
    By Ignacio in forum Windows Software
    Replies: 3
    Last Post: 26-11-2009, 10:33 PM
  4. Developing new softwares
    By swapy in forum Software Development
    Replies: 7
    Last Post: 10-07-2009, 10:45 PM
  5. Developing a tool/application
    By sahil sherwani in forum Software Development
    Replies: 1
    Last Post: 10-04-2009, 09:47 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,583,667.11644 seconds with 17 queries