# Ping from tag-script

**URL:** <https://forum.inductiveautomation.com/t/ping-from-tag-script/20945>\
**Category:** Ignition\
**Created:** [November 19, 2018, 5:00pm UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945 "2018-11-19T17:00:32Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![pat.stave1](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@pat.stave1](https://forum.inductiveautomation.com/u/pat.stave1)\
**Post date:** [November 19, 2018, 5:00pm UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945/1 "2018-11-19T17:00:32Z")

</div>

Hi,

I want to perform a cyclic “ping” to check if a device is available on the network. If the device is not responding, i want to create an alarm.

Is there any way to perform a ping operation from a script run in a tag?

I know there is a ping driver available on the marketplace, but the cost is rather high.

---

<div class="post-metadata">

**Author:** ![brandon1](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@brandon1](https://forum.inductiveautomation.com/u/brandon1)\
**Post date:** [November 19, 2018, 5:23pm UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945/2 "2018-11-19T17:23:59Z")

</div>

I would not run this from an event script on a tag.  
Instead run it in Gateway Timer script that calls the ping asynchronously.

---

<div class="post-metadata">

**Author:** ![c.bertulli](https://avatars.discourse-cdn.com/v4/letter/c/f19dbf/32.png) [@c.bertulli](https://forum.inductiveautomation.com/u/c.bertulli)\
**Post date:** [November 20, 2018, 11:09am UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945/3 "2018-11-20T11:09:04Z")

</div>

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

---

<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:** [November 20, 2018, 3:43pm UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945/4 "2018-11-20T15:43:37Z")

</div>

But, not all devices accept ping or they may block ICMP. Java’s InetAddress is more reliable in this instance. And you don’t have to subprocess it.

```python
def ping(host, timeout=1000):
  from java.net import InetAddress

  ip = InetAddress.getByName(host)
  r = ip.isReachable(timeout)

  return r
 

```

---

<div class="post-metadata">

**Author:** ![AlThePal](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/althepal/32/3603_2.png) [@AlThePal](https://forum.inductiveautomation.com/u/AlThePal)\
**Post date:** [November 20, 2018, 6:21pm UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945/5 "2018-11-20T18:21:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![awaege](https://avatars.discourse-cdn.com/v4/letter/a/bc8723/32.png) [@awaege](https://forum.inductiveautomation.com/u/awaege)\
**Post date:** [November 20, 2018, 8:55pm UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945/6 "2018-11-20T20:55:38Z")

</div>

Do something like this. Make sure to run asynchronous:

```auto
import os
def ping():
status = (os.system("ping " + deviceIP) == 0)

    def checkStatus():
    	if deviceOnline: 
    		output = deviceName + " with IP: " + deviceIP + " is online."
    		state = 1
    	else:
    		output = deviceName + " with IP: " + deviceIP + " is not connected."
    		state = 0
    	
    system.util.invokeLater(checkStatus)

 deviceOnline = system.util.invokeAsynchronous(ping)

```

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [November 20, 2018, 9:12pm UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945/7 "2018-11-20T21:12:04Z")

</div>

These approaches are all pretty much equivalent - an ICMP echo request.

I like that @awaege has set it up to be async.

---

<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:** [November 20, 2018, 9:20pm UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945/8 "2018-11-20T21:20:23Z")

</div>

It’s based on my admittedly anecdotal evidence from working with the devices around here in the facility in which I work. 🙂

In many cases (again, around here), a managed switch may block ICMP, but be transparent to TCP port 7. Same with NAT devices.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [November 20, 2018, 10:25pm UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945/9 "2018-11-20T22:25:58Z")

</div>

> [@JordanCClark](#):
>
> In many cases (again, around here), a managed switch may block ICMP, but be transparent to TCP port 7. Same with NAT devices.

Yeah, but... have you ever actually seen a server or device running the echo service (in.... the last 10 years? more? ever?)

---

<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:** [November 21, 2018, 12:08am UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945/10 "2018-11-21T00:08:16Z")

</div>

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…

---

<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:** [November 21, 2018, 1:11am UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945/11 "2018-11-21T01:11:19Z")

</div>

See? I’m so old I sidetracked myself!

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.

---

<div class="post-metadata">

**Author:** ![pat.stave1](https://avatars.discourse-cdn.com/v4/letter/p/f19dbf/32.png) [@pat.stave1](https://forum.inductiveautomation.com/u/pat.stave1)\
**Post date:** [November 21, 2018, 11:10pm UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945/12 "2018-11-21T23:10:16Z")

</div>

Ended up using Jordans script triggered from a gateway timed event. Works great! Thanks.

---

<div class="post-metadata">

**Author:** ![Dannymichaud](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@Dannymichaud](https://forum.inductiveautomation.com/u/Dannymichaud)\
**Post date:** [November 26, 2018, 6:21pm UTC](https://forum.inductiveautomation.com/t/ping-from-tag-script/20945/13 "2018-11-26T18:21:15Z")

</div>

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.

```auto

# 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))

```
