Jump to content
xisto Community
WeaponX

Script Using Ping To Monitor Network

Recommended Posts

I want a batch/dos file that will ping certain IP addresses at intervals of say 1 hour. If the IP address is not pingable after each hourly checkup, I want it to give some kind of warning. Maybe startup a notepad file and indicate the IP address that lost the network connection.I will want to improve on this later on, but for now, a simple dos batch file or something similar will do.I'm using Windows XP at the office network.Thanks.

Share this post


Link to post
Share on other sites

hey!, maybe you have to install some Unix stuff in your Box. Try using cygwin, Unix batch is very very powerful. With a cron task and php, you will be able solve this "homework" easily.

Anyway, if this is for a "live or death" request, try using some IP Monitor
Blessings!

Share this post


Link to post
Share on other sites

Here is an example of how I would solve your problem using cygwin in my setup at home.

 

192.168.1.1 is the IP address of my wireless router, so it always responds to a ping.

192.168.1.2 is the IP address of my PC - the one I would run this script on.

192.168.1.3 is - let's say - the IP address of a PC that is currently off.

 

I have cygwin installed and all the binaries are in C:\cygwin - this directory is in my PATH.

 

These binaries I'm talking about are Win32 ports of POSIX utilities - very slick, very powerful tools that I strongly believe every serious IT professional should be familiar with. They make a very good team with the pre-existing Windows command line utilities, whether they be built-in ones, or resource kit ones, or whatever other command line based power tools you may have.

 

As a prerequisite to the solution below, I have temporarily enabled the Messenger service on my 192.168.1.2 box - this one is required in order for the net send command to work.

 

Before I start describing the solution, this is a little experiment I did - please note the use of the $? return code, which may be 0 for successful and 1 meaning failure.

Posted Image

 

What I did then is create a text file called ips.txt, like this:

Posted Image

 

and I ran the following script in the cygwin bash shell (remember this is still Windows XP, you're just pretending to work the way you would in UNIX):

while truedo  for ipaddress in `cat ips.txt`  do	ping $ipaddress	if [ $? != 0 ]	then	  net send 192.168.1.2 $ipaddress is down	fi  done  sleep 3600done
and the popup that I get looks like this:

Posted Image

 

The net send command is not restricted to IP addresses, it can also recognize any kind of resolvable host name (NetBIOS, DNS, LDAP, etc...).

 

If you want to get even fancier, you can put this on a server and configure it as a Windows background service using a small resource kit utility called srvinstw.exe.

 

Good luck.

Edited by dserban (see edit history)

Share this post


Link to post
Share on other sites

Thanks for the replies guys. IP Monitor seems to only monitor the current computers IP Address.cygwin looks like the way to go. I will take a look at it. The only thing I don't like about doing it this way is that it requires the Messenger Service to be running. Is it possible to create if statements using dos commands also? I know my way around dos but when it comes to the if statements (if it applies to dos even), I'm at a loss. Maybe something like the code you have above?if 192.168.1.200 is downthen open notepad and display that it's down....Make it read a file and ping through all the IP addresses. If more than one IP Address is not able to be pinged, addend to the same notepad file indicating so.Thanks.

Share this post


Link to post
Share on other sites

The dos batch file should look like this

ping 10.0.0.3 -n 1 > c:\ping.logIF %errorlevel% == 1 c:\windows\notepad.exe c:\ping.log
-n 1 is to ping only once, cause if you ping more than once, standard is 4, it will take a long time to timeout.
%errorlevel% is a dos variable and will be set by the last exited application, to determine if it pass or fail.
ping will set %errorlevel% to 1 if it fail, and 0 if pass.
So the IF statement will check the %errorlevel% variable, then run notepad if it fail.
> c:\ping.log is to redirect the output of the ping to a text file. Using >> will append the output. ping.log must not exist, or else will be overwritten.

For the hourly checking, you can use task scheduler, most windows will have one, including XP. You can schedule it to run the batch file every hour. It's under StartMenu\Programs\Accessories\System\Scheduled Task

The hourly setting is a bit tricky. You have to set it to daily first, put a suitable starting time. If you pc is not turn on by then, it will only run the next day at that time. Then click Advance, check Repeat task, then you can select every hour. Duration you can put 10minute, or whichever time long enough for the ping to execute.

You might want to experiment a bit, I'm not sure if after the duration, whether will it close the application that's launched by the batch file, in case if the ping fail.

Good Luck

Share this post


Link to post
Share on other sites

Thanks faulty.lee. Is there any way to append to an existing log file as I don't want it to be overwritten? I will have a list of IP Addresses to ping and want to keep this log with all the messages (pass or failed). If I ping more than one IP Address, what code do I need to change or add in for this to work properly? I will set it to ping maybe 4 times for each IP address just to be sure it's still up. The batch file should go through all the IP addresses and if it finds an issue, then open up the notepad file.

Share this post


Link to post
Share on other sites

In order to append the output of a command to a file instead of overwriting the file you simply need to use the double greater than operator instead of a single greater than sign.I also think that, if you are going to do that, you may need timestamps before each command output. The native date command in Windows ("date /T" at the command prompt) only gives you the date and only in the format configured in your regional settings in control panel, but the date command in cygwin gives you both the date and the time and in a customizable format.

Edited by dserban (see edit history)

Share this post


Link to post
Share on other sites

What dserban said is right. In fact i did mentioned in my previous post "Using >> will append the output". You just need to do multiple IF for each ping, and all ">>" append to the same output log file.

Share this post


Link to post
Share on other sites

Why using a batch file while there are plenty of tools on the internet available that can do all this things for you ?

Well, why trouble yourself with downloads from the internet, where you have to test and scan (spyware) each tools as you try them out. Also, batch file is the simplest of all scripts, guaranteed to works in all windows, regardless of what's installed or not. A fool prove, fast and get the job done method in this case. Also, as WeaponX's mentioned, it's temporary. So I reckon he want to get a right a program to do a better job later.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.