# Network status display for 'ping' results

**URL:** <https://forum.inductiveautomation.com/t/network-status-display-for-ping-results/17246>\
**Category:** Ignition\
**Tags:** ignition78\
**Created:** [March 6, 2018, 10:27am UTC](https://forum.inductiveautomation.com/t/network-status-display-for-ping-results/17246 "2018-03-06T10:27:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 6, 2018, 10:27am UTC](https://forum.inductiveautomation.com/t/network-status-display-for-ping-results/17246/1 "2018-03-06T10:27:34Z")

</div>

From [Vince's ping question](http://forum.inductiveautomation.com/t/script-to-ping-computer/16612) I have tested the Python script below and it works well.

> import os  
> print '192.168.1.1 ', os.system("ping -n 1 -w 1000 192.168.3.1") == 0

It's running on a Windows host so

- "-n 1" limits it to one ping rather than the default of four
- -w 1000 times it out after 1000 ms in the event of no answer.

Question:  
How can I use this to update an indicator status to populate a form with a hundred devices to test?

```
( ) 10.99.100.1 Good.
(X) 10.99.100.2 No response
( ) 10.99.100.3 Good.
( ) 10.99.100.4 Good.
( ) 10.99.100.5 Good.

```

Is there a method of incorporating this into a memory or expression tag?

Many thanks.

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 6, 2018, 4:30pm UTC](https://forum.inductiveautomation.com/t/network-status-display-for-ping-results/17246/2 "2018-03-06T16:30:08Z")

</div>

When I did this, I used two scripts. One was to initialize the contents of a dataset tag with the IP addresses in one column and a ‘Never’ status an the second column. I wrote this, because I have 500-ish IPs to keep track of. More on the status in a moment.

The second script reads the tag holding the IP addresses and statuses, iterates and updates rows whose IPs connect with a time stamp, then writes it back to the tag. This keeps track of the last time that a device was seen.

EDIT: Sorry, I didn’t provide the quite the correct source code for the ‘ping’ script. It had a major flaw that didn’t create the dataset before trying to write to the tag. Edited to reflect correct code.

Initialization script:

```auto
# This script initializes a dataset tag so that all results are 'Never'

# tag to hold results
resultsTag = '[default]Ping/Results'

# initialize dataset
headers = ['ip', 'last_ping']
data = []

#list of IP ranges. More than one range can be specified.
ipList = ['192.168.140', '192.168.141']

for ipRange in ipList:
  for i in range(1,255):
    ip_addr = ipRange + '.' + str(i)
    data.append([ip_addr, 'Never'])
dataOut = system.dataset.toDataSet(headers, data)
system.tag.write(resultsTag, dataOut)

```

‘Ping’ script:

```auto
from java.net import InetAddress
from datetime import datetime

resultsTag = '[default]Ping/Results'

dataIn = system.dataset.toPyDataSet(system.tag.read(resultsTag).value)

headers = ['ip', 'last_ping']
data = []

timeout = 200

for row in dataIn:
  ip_addr = row['ip']
  ip = InetAddress.getByName(ip_addr)
  r = ip.isReachable(timeout)
  if r:
    data.append([row['ip'], str(datetime.now())])
  else:
    data.append(list(row))
  print ip_addr, r
dataOut = system.dataset.toDataSet(headers, data)
system.tag.write(resultsTag, dataOut)

```

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/b/b8260bd8a93e9a6f12492f430587bba1b279371d.png)

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [March 6, 2018, 4:33pm UTC](https://forum.inductiveautomation.com/t/network-status-display-for-ping-results/17246/3 "2018-03-06T16:33:44Z")

</div>

One more thing… **_Be sure you are running this in a dedicated thread._**

If it runs in a shared thread, anything that relies on that thread comes to a screeching halt. While one may ask how I know that, I refuse to answer. 😇

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [March 8, 2018, 2:21pm UTC](https://forum.inductiveautomation.com/t/network-status-display-for-ping-results/17246/4 "2018-03-08T14:21:43Z")

</div>

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:

```auto
# 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).

- Add the code below.

```auto
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.

---

<div class="post-metadata">

**Author:** ![albert.bartolome](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/albert.bartolome/32/17545_2.png) [@albert.bartolome](https://forum.inductiveautomation.com/u/albert.bartolome)\
**Post date:** [April 30, 2019, 11:05am UTC](https://forum.inductiveautomation.com/t/network-status-display-for-ping-results/17246/5 "2019-04-30T11:05:18Z")

</div>

Hi, is this ping script applicable to Linux Ubuntu as well?

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 30, 2019, 11:12am UTC](https://forum.inductiveautomation.com/t/network-status-display-for-ping-results/17246/6 "2019-04-30T11:12:45Z")

</div>

Yes, it uses Java libraries, so it should be platform independent.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 30, 2019, 1:12pm UTC](https://forum.inductiveautomation.com/t/network-status-display-for-ping-results/17246/7 "2019-04-30T13:12:27Z")

</div>

You may need to modify the os ping call for Linux.

```auto
r = os.system("ping -n 1 -w 1000 " + row['ip']) == 0;

```

to something like

```auto
r = os.system("ping -c 1 -w 1000 " + row['ip']) == 0;

```

It’s ‘-n’ in Windows and ‘-c’ in Linux. Check the [manual](https://vitux.com/linux-ping-command/).

You just want it to try once and then give up.
