Thank you, Jordan. That was a great help.
I couldnāt get the Java InetAddress function to work so I replaced it with an os.system call which works fine on my Windows setup.
For anyone else trying this here are a few more detailed steps to get you going.
###1. Create the dataset tag
In the Tag Browser create a tag for the dataset tag for the ping results. I created the inside a tag folder called PingTest:
- Name: Results
- Data Type: DataSet
- Enabled: Yes
- Access Rights: Read/Write
- Scan Class: Default
###2. Create the gateway event script
In the Project Browser | Project | Scripts double-click Gateway Event Scripts.
####2.1 Startup script code
In the Startup script add the code:
# This script initializes a dataset tag so that all results are 'Never'
# tag to hold results
resultsTag = '[default]PingTest/Results'
# initialize dataset
headers = ['ip', 'last_ping']
data = []
#list of IP ranges. More than one range can be specified.
ipList = ['192.168.0', '192.168.1']
for ipRange in ipList:
for i in range(200,253): # I'm only interested in addresses above 200.
ip_addr = ipRange + '.' + str(i)
data.append([ip_addr, 'Never'])
dataOut = system.dataset.toDataSet(headers, data)
system.tag.write(resultsTag, dataOut)
####2.2 The timer event code
Click the Timer icon | New ā+ā button.
- Give the timer a name.
- Delay: 30000. (Pings can take a while.)
- Enabled: True.
- Delay Type: Fixed delay
- Threading: Dedicated.
As Jordan has pointed out, you need to set this to ādedicatedā thread otherwise all other scripts will pause while this one runs (and many pings can take quite a while).
from datetime import datetime
import os
resultsTag = '[default]PingTest/Results'
dataIn = system.dataset.toPyDataSet(system.tag.read(resultsTag).value)
headers = ['ip', 'last_ping']
data = []
for row in dataIn:
r = os.system("ping -n 1 -w 1000 " + row['ip']) == 0;
if r:
data.append([row['ip'], str(datetime.now())])
else:
data.append(list(row))
print row['ip'], r
dataOut = system.dataset.toDataSet(headers, data)
system.tag.write(resultsTag, dataOut)
###3. Create the display
- Create a form.
- Add a table.
- Drag and drop the Results tag onto the table.
- Save the project.
The table should update with the results when the last ping is finished.