AustinA2
Posts: 7
Joined: Mon Mar 04, 2019 7:12 pm

Ping sweep of my network

Mon Mar 04, 2019 7:16 pm

My boss has requested that i build a script that does the following

Pings various network devices every 5 minutes, if the ping succeeds, send a "high" to the IO pin, if it fails send a "low" to the IO pin. The pin will then in turn tell another network device whether or not to send an alarm.

My questions is, how can i configured this, what is the best way? can i write a python script? if so, what will it look like , how will i run it, where do i put the program?

I am a new user, so forgive me for any mistakes,
AustinA2

Andyroo

Re: Ping sweep of my network

Tue Mar 05, 2019 11:37 am

You could start with https://raspberrypi.stackexchange.com/q ... fline#8755 or https://stackoverflow.com/questions/295 ... n#10402323

To control the GPIO have a look at https://www.raspberrypi.org/magpi/issue ... o-zero-v1/

The latter book with show you how to create the programs and store them on the Pi.

Once you have these working come back about starting it up.

It’s rather a crude method of system monitoring and not 100% reliable as it only check for Ping and not services. The older AS/400 boxes would respond the a ping while off in some cases as the network card had its own processor on it! Other machine may have disabled ping...

For ‘propper’ Monitoring, consider something like https://www.nagios.org or for a simpler solution https://www.zabbix.com

IanS
Posts: 234
Joined: Wed Jun 20, 2012 2:51 pm
Location: Southampton, England

Re: Ping sweep of my network

Tue Mar 05, 2019 12:09 pm

Zabbix is a lot easier to get up and configured than the base Nagios build, but there is now a Raspberry targeted version of Nagios (https://nemslinux.com/) with an improved interface which might make things easier.

User avatar
DougieLawson
Posts: 36522
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Ping sweep of my network

Tue Mar 05, 2019 12:14 pm

A much better way to scan networks is with nmap. You can drive that from python with https://pypi.org/project/python-nmap/

The os.system spawn of a ping for each address will be terribly slow.

Using nagios is better if you can find an easy way to drive a GPIO from it.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

tpyo kingg
Posts: 637
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Ping sweep of my network

Tue Mar 05, 2019 12:21 pm

I agree about nmap. One feature I haven't used but seems like a good fit would be the Nmap Scripting Engine (NSE). It has its own section in the manual page and seems like it could be used to trigger certain actions.

User avatar
NGC6543
Posts: 75
Joined: Thu Feb 28, 2019 7:30 pm
Location: Lancashire, UK

Re: Ping sweep of my network

Tue Mar 05, 2019 5:33 pm

Assuming we stick to the simple ping method in the OP, wouldn't a simple bash script be the most efficient way?

Something like this:

(Please note that I write bash scripts about as often as I jump off tall buildings without an umbrella, plus I'm a complete newcomer to the Pi, so please feel free to correct my code.)

Code: Select all

#!/bin/bash

# Setup output pin 18
echo "18" > /sys/class/gpio/export                  
echo "out" > /sys/class/gpio/gpio18/direction

# Loop every 5 mins
while sleep 300
do
	# Reset failure flag
	FAIL=0
	# Ping an address once
	ping -c1 google.com
	# If failure, set failure flag
	if [ $? -ne 0 ]; then FAIL=1; fi
	
	# Repeat ping and failure test lines for all addresses
	
	# Test fail flag
	if [ $FAIL -eq 1 ]
	then
		echo "1" > /sys/class/gpio/gpio18/value
	else
		echo "0" > /sys/class/gpio/gpio18/value
	fi
done

bholland84
Posts: 10
Joined: Tue Mar 14, 2017 3:43 am

Re: Ping sweep of my network

Wed Mar 06, 2019 3:37 pm

This is actually the exact opposite way to do it. You don't want to check to see if something doesn't work. You want the thing that doesn't work to report it doesn't work. What I would do is figure out a way to physically attach something to the network devices that check to see it is running and report to a central server.

Pings fail. Network connections are not guaranteed. The best solution is that when something breaks, you report it to somewhere and keep on reporting it until the report goes through.

Now having said that, if you absolutely must do this, I would create a cron job that runs say, a python script. BTW, the reason for the python script is that you have access to the gpio module from python and the command line. Cron will run something every given period of time. That something will be something like python <your python code>. I can't remember how to access the CLI from python (it changed from when I used it to I think the subprocess module) and from there, you can access your gpio pin depending on the result.

5 minutes seems entirely arbitrary and some networking hardware will kill ICMP packets. For example, most of our servers at work simply drop ping packets. This is like, security 101.

AustinA2
Posts: 7
Joined: Mon Mar 04, 2019 7:12 pm

Re: Ping sweep of my network

Wed Mar 06, 2019 11:21 pm

Thank you all very much for your replies!!! i did not get notified that this post had any action, so i hadn't checked it, but i am going to read through the suggestions.

again, thank you very much for the help!

jbudd
Posts: 1025
Joined: Mon Dec 16, 2013 10:23 am

Re: Ping sweep of my network

Sat Mar 09, 2019 1:10 pm

You could consider Node-Red for this. It has an easy web based drag and drop interface. A flow to monitor several network devices might look like this
Untitled 2.jpg
Untitled 2.jpg (52.72 KiB) Viewed 925 times
(Though you would probably want to make it a bit fancier, so that the notification tells you which device is down, etc).
You can also easily make a "dashboard" web page to represent the devices' status

AustinA2
Posts: 7
Joined: Mon Mar 04, 2019 7:12 pm

Re: Ping sweep of my network

Wed Mar 13, 2019 5:48 pm

@jbudd:

Is this something run through the pi?

my problem is this; I am new to both Pis and Python. I know this is possible because i have seen pieces of what i want to do in various instances but i am having trouble putting it together how i want to..

I want to tie 1 ip address to 1 pin and have a code that says hey this ping failed, send a HIGH or LOW. I understand that there is varying logic here, but i think it makes more sense for the pin to be "on" when it is working. that way when it is "off" we know either the ping failed or the devices has lost power.

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Ping sweep of my network

Wed Mar 13, 2019 6:22 pm

AustinA2 wrote:
Wed Mar 13, 2019 5:48 pm
@jbudd:

Is this something run through the pi?

my problem is this; I am new to both Pis and Python. I know this is possible because i have seen pieces of what i want to do in various instances but i am having trouble putting it together how i want to..

I want to tie 1 ip address to 1 pin and have a code that says hey this ping failed, send a HIGH or LOW. I understand that there is varying logic here, but i think it makes more sense for the pin to be "on" when it is working. that way when it is "off" we know either the ping failed or the devices has lost power.

I can save you time by answering for @jbudd: Yes, it is.


In respect of your own ideas:
i am having trouble putting it together how i want to..

I want to tie 1 ip address to 1 pin and have a code that says hey this ping failed, send a HIGH or LOW.

Perhaps put together your best effort and ask for help if/when you get stuck.

Be aware there is a design limitation of a maximum of 26 devices that can be monitored, since that is (at least for a trivial RPi-only solution) the total number of gpio pins you can control.

jbudd
Posts: 1025
Joined: Mon Dec 16, 2013 10:23 am

Re: Ping sweep of my network

Wed Mar 13, 2019 6:43 pm

Node-red runs on the pi yes. There are Pi specific installation instructions at node-red.org (to get an up to date version)

I don't know why your boss wants gpio pins to indicate network device status, it sounds like only half a solution to me.
Much better imho for the pi which monitors them to send out any necessary notification messages.

I have several Pi's and microcontrollers on my network. I could test them by ping but in fact each one sends a "heartbeat" message to the pi which monitors them all.
The pi flashes LEDs on a hat (I use a pimoroni blinkt) to indicate which devices are running properly.
If certain devices are not heard from within a set time, it sends me an email.
All of this processing is in node-red with a couple of python scripts.

AustinA2
Posts: 7
Joined: Mon Mar 04, 2019 7:12 pm

Re: Ping sweep of my network

Wed Mar 13, 2019 6:52 pm

two things:

Node-RED looks interesting, and now that i see your latest comment i am assuming it does in fact use Python... the videos i watched only showed Java.

second thing is, and i think this will clear up some confusion, we already have a device that is dedicated to sending dealing with alarms. It is called a Davicom and is used, as far as i know, mostly in Broadcast. The davicom only allows for 32 devices to be pinged. we are using the Pi to extend that function to more devices (and to save like 5k) and the pins will be wired into the davicom for it to interpret.

I hope this all made sense.

jbudd
Posts: 1025
Joined: Mon Dec 16, 2013 10:23 am

Re: Ping sweep of my network

Wed Mar 13, 2019 6:59 pm

Node-red uses JavaScript. You don't need to go much programming though.
You can call other programs from it, I use python or shell scripts for that.

How much did the Davicom cost? :-)

I once caused much displeasure in a sysadmin department by doing the same monitoring tasks better than the system they'd just bought for 50K

AustinA2
Posts: 7
Joined: Mon Mar 04, 2019 7:12 pm

Re: Ping sweep of my network

Wed Mar 13, 2019 7:03 pm

jbudd wrote:
Wed Mar 13, 2019 6:59 pm

How much did the Davicom cost? :-)
between 5 and 10 grand... i think that these are older versions.

AustinA2
Posts: 7
Joined: Mon Mar 04, 2019 7:12 pm

Re: Ping sweep of my network

Wed Mar 13, 2019 7:04 pm

for the record they are a little bit more than just monitoring and to be totally honest its a bit over my head... just trying to deliver what the boss-man ordered.

Return to “Advanced users”