ping the machine using TCP pings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • imughal
    New Member
    • Jun 2007
    • 1

    ping the machine using TCP pings

    I got the perl script which does following task.

    This solution reads in a line from a text file that is passed in as input parameter 1 to a Perl script. This script will then ping the machine using TCP pings to the remote hosts echo port and will return up/down and the ping response time. A second optional attribute may be passed in that will set the default timeout for the ping response.

    Text file format:
    127.0.0.0 localhost
    192.168.100.1 abc

    Now i want to add some more colums like serial no like
    1 127.0.0.0 localhost
    2 192.168.100.1 abc
    but when scripts reads text file does not give required information.


    #!/usr/bin/perl
    # This script uses perl's Ping library.
    # The first parameter to pass in is a text file to open that contains a list
    # of ip serverIDs. A sample would look like:
    # 1.2.3.4 LDAPServer
    # 127.0.0.1 localhost
    # The second argument is the timeout that the ping command should have. This
    # value should be 1,2,3,4, or 5 seconds. If nothing is specified the default of
    # 5 seconds is used.
    # The resulting output will result in each line of the script bing printed with
    # "up" or "down"

    [CODE=perl]
    use strict;
    use Net::Ping;
    use Time::HiRes;

    my $filename = $ARGV[0];
    my $interval = $ARGV[1];
    my @ips = undef;
    my $line = undef;
    my $p = undef;
    my $ret = undef;
    my $duration = undef;
    my $ip = undef;

    open(FILE, "< $filename") or die "Can't open $filename : $!";
    @ips = <FILE>;
    close FILE;

    if (($interval eq undef) || ($interval le 0) || ($interval gt 5)) {
    $interval = 5;
    }

    foreach $line (@ips) {
    $p = Net::Ping->new("tcp",$int erval);
    $p->hires();
    ($ret, $duration, $ip) = $p->ping ($line);
    if ($ret) {
    chomp($line);
    printf("$line up %.2f\n", 1000 * $duration)
    } else {
    chomp ($line);
    print "$line down 0.00\n";
    }
    $p->close();
    }
    [/CODE]

    guide me where to make changes to achieve desire results.
    Last edited by miller; Jun 14 '07, 06:49 PM. Reason: Code Tag and Reformatting
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    Just use the split command to break the $line into it's constituent parts.

    However, there are a few issues that I would take with your script as it is right now. Some of these are design issues, some of them are actually functional.

    1) Declaring all your variables up front.

    This is definitely something you occasionally see from C programmers coming to perl. However, this is not the convention from most perl programemrs. Instead wait to declare your variables when they are actually used. There are many reasons why this is better. For one it's easier. You only have to list the variables once. Secondly, it allows variables to be scoped purely in the block of code that they are used. For example, the $p variable is only used within the foreach. So by declaring it in that block it will bo out of scope and automatically be collected at the end of the foreat. At least it will if you declare it there.

    Ultimately, I just believe that it's clearer what variables are used for if you wait to declare them until they are actually being used.

    2) $interval eq undef.

    This is bad. If you truly want to test if something is defined. use "! defined $interval". The reason this is better is because doing a string comparison "eq" is the equivalent if saying $interval eq ''. You can test this yourself.

    [CODE=perl]
    my $arg = $ARGV[0];

    print '' . ($arg eq undef) ? "eq undef\n" : "ne undef\n";
    print '' . (! defined $arg) ? "Not Defined\n" : "Is defined\n";
    [/CODE]

    Output
    Code:
    >perl scratch.pl
    eq undef
    Not Defined
    
    >perl scratch.pl ""
    eq undef
    Is defined
    3) Using string comparison operators with numbers.

    The operaters used to compare numbers are < > <= => <=>. Not lt gt le ge cmp. They would technically work in the instance that you are using them, but they will often not produce the results that you expect.

    Additionally, because you are simply searching for 5 exact numbers, I would recommend that you simply use a regular expression.

    That brings that brings the following changes to you script:

    [CODE=perl]
    #!/usr/bin/perl
    # This script uses perl's Ping library.
    # The first parameter to pass in is a text file to open that contains a list
    # of ip serverIDs. A sample would look like:
    # 1.2.3.4 LDAPServer
    # 127.0.0.1 localhost
    # The second argument is the timeout that the ping command should have. This
    # value should be 1,2,3,4, or 5 seconds. If nothing is specified the default of
    # 5 seconds is used.
    # The resulting output will result in each line of the script bing printed with
    # "up" or "down"

    use Net::Ping;
    use Time::HiRes;

    use strict;

    my $filename = $ARGV[0];
    my $interval = $ARGV[1];

    $interval = 5 if $interval !~ /^[0-5]$/;

    open(FILE, "< $filename") or die "Can't open $filename : $!";

    while (<FILE>) {
    chomp;
    my $line = $_;

    my $p = Net::Ping->new("tcp", $interval);
    $p->hires();
    my ($ret, $duration, $ip) = $p->ping ($line);

    if ($ret) {
    printf "$line up %.2f\n", 1000 * $duration;
    } else {
    print "$line down 0.00\n";
    }
    $p->close();
    }

    close FILE;
    [/CODE]

    - Miller
    Attached Files

    Comment

    • somsub
      New Member
      • Nov 2008
      • 17

      #3
      I have used that code .. and that worked fine..........
      ............... ............... .... but .. it is showing the same result
      that is "machine name is down " for the

      1>invalid entry (machine name that is not found by the $p->ping in the network ) in the $line in ($p->ping ($line))

      and ....

      2>for those machines which are actualy down.

      Does any one know any another opertaor to check the machine staus. Because $p->ping is returning same value in ($ret, $duration, $ip) parameter for above two cases.

      Lots of thanks in advance for any help on this.....

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Ok, you have done two thing wrong here. The first is only slightly wrong, you have posted to a thread that is over a year old. The op has more than likely solved their issue and moved on long ago.

        The second is a bit more wrong and that is, you have hijacked someone elses thread. You did not start the thread but all of a sudden come in, post and then ask a question to solve your problem.

        The better thing to do would be to start your own thread, reference this thread, saying that you tried its suggestion(s), give you results, and then ask your questions. You do not go around hijacking other peoples threads.

        That said, this thread is now closed. Please post your question in a new thread of your own.

        Regards,

        Jeff (Moderator)

        Comment

        Working...