473,395 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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"

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
3 8347
miller
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];
  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, 696 views)
Jun 14 '07 #2
somsub
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
3,509 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.

Regards,

Jeff (Moderator)
Nov 17 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike Mallory | last post by:
Can Java perform two pings at one time and display the best ping time with the best result? Also, can Java perform two trace routes at one time and display the trace with the fewest hops? ...
8
by: JC | last post by:
Hi, I need to write an application that pings servers and displays the results. Can someone point me in the right direction? thanks, jc
0
by: Ed | last post by:
I've attached some VB.NET code I've hacked together (some taken from MS examples & newsgroup postings) that will perform a ping or IcmpSendEcho using the icmp.dll (see this for more info:...
0
by: scotty | last post by:
I need to do a ping scan of a subnet. I can Enum through the IPs and do this, but it takes over 5 minutes as I can only create 1 process at a time. Does anyone know how I can create multiple Ping...
5
by: Deepak | last post by:
I am programing a ping application which pings various centers . I used timer loop and it pings one by one. Now when i finish pinging one center it should wait for the ping_completed function to...
3
by: joja15 | last post by:
I am working on a Python script to perform as a remote computer manager. So far I have a WOL function working and I would like to add the ability to show if a machine is on or off (I figured I...
5
by: Michael M. | last post by:
I have the following code (listed at bottom of post) that pings a small range of IP address to see which ones are alive. To speed things up a little I am trying to use more than one thread,...
3
by: amaccormack | last post by:
I wrote a quick script to check the "up-ness" of a list of machines, and timeout after 1 second. However, with a lot of timeouts, the script takes a logn time, so I thought to parallelise it....
7
by: Linus Cohen | last post by:
Hi all, I'm a newbie to python and programming in general, so I wanted a simple project to start off. What I'm trying to do here is write a python command-line ping program, much like the Unix and...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.