Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Script Using Ping To Monitor Network
WeaponX
post Aug 17 2007, 01:29 AM
Post #1


Way Out Of Control - You need a life :)
Group Icon

Group: Members
Posts: 1,086
Joined: 21-June 05
From: New York
Member No.: 6,440



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.
Go to the top of the page
 
+Quote Post
develCuy
post Aug 17 2007, 03:37 AM
Post #2


Member - Active Contributor
Group Icon

Group: Members
Posts: 88
Joined: 5-April 07
From: Cusco - Peru
Member No.: 21,283



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!
Go to the top of the page
 
+Quote Post
dserban
post Aug 17 2007, 07:36 AM
Post #3


Premium Member
Group Icon

Group: [HOSTED]
Posts: 286
Joined: 17-June 07
Member No.: 22,702



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.


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


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):
CODE
while true
do
  for ipaddress in `cat ips.txt`
  do
    ping $ipaddress
    if [ $? != 0 ]
    then
      net send 192.168.1.2 $ipaddress is down
    fi
  done
  sleep 3600
done

and the popup that I get looks like this:


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.

This post has been edited by dserban: Aug 17 2007, 07:53 AM
Go to the top of the page
 
+Quote Post
WeaponX
post Aug 17 2007, 02:08 PM
Post #4


Way Out Of Control - You need a life :)
Group Icon

Group: Members
Posts: 1,086
Joined: 21-June 05
From: New York
Member No.: 6,440



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 down
then 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.
Go to the top of the page
 
+Quote Post
faulty.lee
post Aug 17 2007, 03:10 PM
Post #5


Premium Member
Group Icon

Group: [HOSTED]
Posts: 458
Joined: 5-November 06
Member No.: 17,016



The dos batch file should look like this
CODE
ping 10.0.0.3 -n 1 > c:\ping.log
IF %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
Go to the top of the page
 
+Quote Post
WeaponX
post Aug 23 2007, 02:01 PM
Post #6


Way Out Of Control - You need a life :)
Group Icon

Group: Members
Posts: 1,086
Joined: 21-June 05
From: New York
Member No.: 6,440



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.
Go to the top of the page
 
+Quote Post
dserban
post Aug 23 2007, 02:30 PM
Post #7


Premium Member
Group Icon

Group: [HOSTED]
Posts: 286
Joined: 17-June 07
Member No.: 22,702



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.

This post has been edited by dserban: Aug 23 2007, 02:34 PM
Go to the top of the page
 
+Quote Post
faulty.lee
post Aug 23 2007, 04:09 PM
Post #8


Premium Member
Group Icon

Group: [HOSTED]
Posts: 458
Joined: 5-November 06
Member No.: 17,016



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.
Go to the top of the page
 
+Quote Post
wutske
post Aug 25 2007, 08:57 AM
Post #9


Super Member
Group Icon

Group: [HOSTED]
Posts: 974
Joined: 2-August 05
From: Kapellen (Antwerp, Belgium)
Member No.: 7,585



Why using a batch file while there are plenty of tools on the internet available that can do all this things for you ?
Go to the top of the page
 
+Quote Post
faulty.lee
post Aug 27 2007, 07:25 PM
Post #10


Premium Member
Group Icon

Group: [HOSTED]
Posts: 458
Joined: 5-November 06
Member No.: 17,016



QUOTE(wutske @ Aug 25 2007, 04:57 PM) *
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.
Go to the top of the page
 
+Quote Post

Fast ReplyReply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How Can I Create A "number Of Visitors" Script(8)
  2. Auto-click Script(6)
  3. Looking For Script(1)


 



- Lo-Fi Version Time is now: 20th July 2008 - 07:41 AM