Simple script to scan devices on a network, and send an email notification if a device is unavailable

Discussions of applications and operating systems and any problems, tips or suggestions. Win XP, 9x/2k, Linux, NT, photo editing, Virus/Spyware help
Post Reply
User avatar
FlyingPenguin
Flightless Bird
Posts: 32773
Joined: Wed Nov 22, 2000 11:13 am
Location: Central Florida
Contact:

Simple script to scan devices on a network, and send an email notification if a device is unavailable

Post by FlyingPenguin »

The Casa Penguin has four (soon to be five) enterprise class Wifi access points (APs), all with the same SSIDs so that devices can seamlessly hand off between APs. However, this also makes it hard to tell if one of the APs goes down.

I discovered I had one that was acting flaky due to a bad PSU, and kept crashing, but I decided I needed some kind of monitoring system to let me know if I have a failure of any of these APs, my two NAS boxes, a couple of IP cameras, and the two NanoBeams that link my two houses via a virtual wireless network.

I found a simple PowerShell script that will send a single ping to a list of LAN IP addresses or hostnames. If any device fails to respond it tries 4 more times and if it still fails to respond, it sends an email with the name of the device and when it went offline. The script runs a scan every 45 seconds. I run the script in a DOS window on my Win7 Pro file server.

The script I found was a bit of a kludge, and it had some bugs. It also didn't support modern SSL encrypted SMTP email servers, so I wound up re-writing it.

The script itself is IPMONITOR.PS1 and it needs to be launched from PowerShell, which can be done via a Windows shortcut and placed in your Startups folder.

I put the script in C:\IP_MONITOR\

and the shortcut to launch it from that folder is:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\IP_Monitor\IPMonitor.ps1" -WindowsStyle Hidden

There is one other necessary file located in the same folder. It's a text file called HOSTS.TXT and contains the list of IPs, and friendly names for each device being scanned.

NOTE: It's important that there are no blank lines in the text file - make sure the last line doesn't have a carriage return at the end of the text file or the script will think there is a blank IP and give an error. Also no spaces on either side of the comma separating the IP address from the description. For example

192.168.1.28,Garage AP

I've included the script and my HOSTS.TXT file as examples. The script is setup to send an email via a GMail account, although it can easily be modified for some other email SMTP server. I've left place holders in the script for the email addresses and password - fill in your own.

NOTE: This script requires PowerShell 5.1 which comes built-in to Win10, but Win7 comes with 2.0 and requires an update. So does Win 8.1. I've included instructions and the necessary files.

The files are all in this ZIP file: http://www.pcabusers.org/shares/IPMonitor%202.0.zip

Yeah I know there's some free services and apps out there that will do this, but they usually require an account and send God knows what back to the mothership. I know there's exactly NOTHING in this script but what I want there to be.

Screenshots:

Image

Image
"Turns out I’m 'woke.' All along, I thought I was just compassionate, kind, and good at history. "

Image
User avatar
Losbot
Almighty Member
Posts: 4991
Joined: Sun Jul 13, 2014 8:59 am
Location: South Florida

Re: Simple script to scan devices on a network, and send an email notification if a device is unavailable

Post by Losbot »

This is awesome. Thank you!
Post Reply