By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,678 Members | 1,920 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,678 IT Pros & Developers. It's quick & easy.

ping the machine using TCP pings

P: 1
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"

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use Net::Ping;
  3. use Time::HiRes;
  4.  
  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;
  13.  
  14. open(FILE, "< $filename") or die "Can't open $filename : $!";
  15. @ips = <FILE>;
  16. close FILE;
  17.  
  18. if (($interval eq undef) || ($interval le 0) || ($interval gt 5)) {
  19.     $interval = 5;
  20. }
  21.  
  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. }
  35.  
guide me where to make changes to achieve desire results.
Jun 14 '07 #1
Share this Question
Share on Google+
3 Replies


miller
Expert 100+
P: 1,089
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];
  2.  
  3. print '' . ($arg eq undef) ? "eq undef\n" : "ne undef\n";
  4. print '' . (! defined $arg) ? "Not Defined\n" : "Is defined\n";
  5.  
Output
Expand|Select|Wrap|Line Numbers
  1. >perl scratch.pl
  2. eq undef
  3. Not Defined
  4.  
  5. >perl scratch.pl ""
  6. eq undef
  7. Is defined
  8.  
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. # 1.2.3.4 LDAPServer
  6. # 127.0.0.1 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"
  12.  
  13. use Net::Ping;
  14. use Time::HiRes;
  15.  
  16. use strict;
  17.  
  18. my $filename = $ARGV[0];
  19. my $interval = $ARGV[1];
  20.  
  21. $interval = 5 if $interval !~ /^[0-5]$/;
  22.  
  23. open(FILE, "< $filename") or die "Can't open $filename : $!";
  24.  
  25. while (<FILE>) {
  26.     chomp;
  27.     my $line = $_;
  28.  
  29.     my $p = Net::Ping->new("tcp", $interval);
  30.     $p->hires();
  31.     my ($ret, $duration, $ip) = $p->ping ($line);
  32.  
  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. }
  40.  
  41. close FILE;
  42.  
- Miller
Attached Files
File Type: txt scratch.txt (1,019 Bytes, 394 views)
Jun 14 '07 #2

P: 16
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

numberwhun
Expert Mod 2.5K+
P: 3,503
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)
Nov 17 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.