469,323 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

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: localhost abc

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

# 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:
# LDAPServer
# 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"

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use Net::Ping;
  3. use Time::HiRes;
  5. my $filename = $ARGV[0];
  6. my $interval = $ARGV[1];
  7. my @ips = undef;
  8. my $line = undef;
  9. my $p = undef;
  10. my $ret = undef;
  11. my $duration = undef;
  12. my $ip = undef;
  14. open(FILE, "< $filename") or die "Can't open $filename : $!";
  15. @ips = <FILE>;
  16. close FILE;
  18. if (($interval eq undef) || ($interval le 0) || ($interval gt 5)) {
  19.     $interval = 5;
  20. }
  22. foreach $line (@ips) {
  23.     $p = Net::Ping->new("tcp",$interval);
  24.     $p->hires();
  25.     ($ret, $duration, $ip) = $p->ping ($line);
  26.     if ($ret) {
  27.         chomp($line);
  28.         printf("$line up %.2f\n", 1000 * $duration)
  29.     } else {
  30.         chomp ($line);
  31.         print "$line down 0.00\n";
  32.     }
  33.     $p->close();
  34. }
guide me where to make changes to achieve desire results.
Jun 14 '07 #1
3 8133
1,089 Expert 1GB
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.

Expand|Select|Wrap|Line Numbers
  1. my $arg = $ARGV[0];
  3. print '' . ($arg eq undef) ? "eq undef\n" : "ne undef\n";
  4. print '' . (! defined $arg) ? "Not Defined\n" : "Is defined\n";
Expand|Select|Wrap|Line Numbers
  1. >perl scratch.pl
  2. eq undef
  3. Not Defined
  5. >perl scratch.pl ""
  6. eq undef
  7. 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:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. # This script uses perl's Ping library.
  3. # The first parameter to pass in is a text file to open that contains a list
  4. # of ip serverIDs. A sample would look like:
  5. # LDAPServer
  6. # localhost
  7. # The second argument is the timeout that the ping command should have. This
  8. # value should be 1,2,3,4, or 5 seconds. If nothing is specified the default of
  9. # 5 seconds is used.
  10. # The resulting output will result in each line of the script bing printed with
  11. # "up" or "down"
  13. use Net::Ping;
  14. use Time::HiRes;
  16. use strict;
  18. my $filename = $ARGV[0];
  19. my $interval = $ARGV[1];
  21. $interval = 5 if $interval !~ /^[0-5]$/;
  23. open(FILE, "< $filename") or die "Can't open $filename : $!";
  25. while (<FILE>) {
  26.     chomp;
  27.     my $line = $_;
  29.     my $p = Net::Ping->new("tcp", $interval);
  30.     $p->hires();
  31.     my ($ret, $duration, $ip) = $p->ping ($line);
  33.     if ($ret) {
  34.         printf "$line up %.2f\n", 1000 * $duration;
  35.     } else {
  36.         print "$line down 0.00\n";
  37.     }
  38.     $p->close();
  39. }
  41. close FILE;
- Miller
Attached Files
File Type: txt scratch.txt (1,019 Bytes, 644 views)
Jun 14 '07 #2
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.....
Nov 16 '08 #3
3,503 Expert Mod 2GB
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.


Jeff (Moderator)
Nov 17 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Mike Mallory | last post: by
8 posts views Thread by JC | last post: by
reply views Thread by scotty | last post: by
3 posts views Thread by joja15 | last post: by
3 posts views Thread by amaccormack | last post: by
7 posts views Thread by Linus Cohen | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.