def ping(address):
import subprocess
command = 'ping', '%s' % address
# shell=True hides the shell window, stdout to PIPE enables communicate() to get the ping command result
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
# trimming it to the actual lines with information
process_out = process.communicate()[0].strip().split('\r\n')
# print out the return of the ping
print process_out[1:5]
But as brandon1 says is better for you to call this script in a Gateway Timer Script.
Just set the address you need inside the variable “address” and you’ll be good to go
How would it be more reliable Jordan? InetAddress tries to use ICMP and if not available falls back to echo on port 7 - which I would think is even less likely to be available nowadays.
Ever? Pfft. I have running equipment from the 90s here, so yeah, I kinda have. Just life in the trenches. Lol
Heck, I’m old enough to have installed and used ARCnet, back in the day. ARCnet was bulletproof enough we once strung three PCs together with spiral notebook wire, and it still worked.
Sorry for aimlessly wandering. Part of getting old…
I’ll need to revisit at some point, but if I recall, isReachable doesn’t require an echo service to be running. The reset packet from the target was enough to return True.
Or something like that. I first looked into this five or six years ago. And since it worked for my needs, with aforementioned equipment, I didn’t look any further into it.
I have enhanced the script to use a dataset with a list of ping. On a schedule script it will ping just one device by turn not to flood the network.
# Script to get the network status of a device. If the
# the device is online, Last_Response will be true otherwise false for
# disconnected device
def ping():
from java.net import InetAddress
# index as int
# Ip_List as dataset(index, IP, Last_Response,TimeOut)
# get Table data
ds = system.tag.read("Ip_List").value
index = system.tag.read("Index").value
if index < 0:
index=0
system.tag.write("Index", index)
if index > (len(ds.data)-2):
index=0
system.tag.write("Index", index + 1)
timeout = ds.getValueAt( index, "TimeOut")
host= ds.getValueAt(index,"IP" )
# Java Inet, ping
ip = InetAddress.getByName(host)
Response = ip.isReachable(timeout)
# Write back the value
system.tag.write("Index", index + 1)
system.tag.write("Ip_List", system.dataset.setValue(ds, index, "Last_Response", Response))