# Script to ping computer

**URL:** <https://forum.inductiveautomation.com/t/script-to-ping-computer/16612>\
**Category:** Ignition\
**Created:** [January 16, 2018, 6:27pm UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612 "2018-01-16T18:27:35Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vince](https://avatars.discourse-cdn.com/v4/letter/v/b5e925/32.png) [@Vince](https://forum.inductiveautomation.com/u/Vince)\
**Post date:** [January 16, 2018, 6:27pm UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/1 "2018-01-16T18:27:35Z")

</div>

Hi everyone!

I’d like to do a page that will show the health of our main system. I need to ping some devices and show the response time.

Is it possible to do this using in a script?

Thanks.

---

<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:** [January 16, 2018, 6:32pm UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/2 "2018-01-16T18:32:24Z")

</div>

There is a 3rd party ping module that has been created.  
[http://forum.inductiveautomation.com/t/byes-apps-driver-ping-module-ignition-7-8/7998](http://forum.inductiveautomation.com/t/byes-apps-driver-ping-module-ignition-7-8/7998)

---

<div class="post-metadata">

**Author:** ![Vince](https://avatars.discourse-cdn.com/v4/letter/v/b5e925/32.png) [@Vince](https://forum.inductiveautomation.com/u/Vince)\
**Post date:** [January 16, 2018, 9:18pm UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/3 "2018-01-16T21:18:38Z")

</div>

Yes I found it but the link is not working…

---

<div class="post-metadata">

**Author:** ![salbrechtsen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/salbrechtsen/32/18660_2.png) [@salbrechtsen](https://forum.inductiveautomation.com/u/salbrechtsen)\
**Post date:** [January 16, 2018, 11:22pm UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/4 "2018-01-16T23:22:32Z")

</div>

You can do this natively in python

```auto
import os
online = os.system("ping 8.8.8.8") == 0

```

This os.system("ping ") will return 0 if successful. So comparing it to 0 will get you a boolean value of if the device can be pinged or not.

Edit: I might add there are some nuances to this depending on OS the script is running on, but in general it should work.

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [January 17, 2018, 7:06am UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/5 "2018-01-17T07:06:00Z")

</div>

I just tested this on Windows, and it returns a 0 when the ping passes or fails.

Kymera has a ping driver that might suit your needs, take a look  
[http://kymerasystems.com/kymera-pinger-driver/](http://kymerasystems.com/kymera-pinger-driver/)

---

<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:** [January 17, 2018, 7:26am UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/6 "2018-01-17T07:26:42Z")

</div>

This is the script you are looking for:

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

```

now just call it inside your script `ping('192.168.1.1`') and it will ping the address and print out the output

---

<div class="post-metadata">

**Author:** ![salbrechtsen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/salbrechtsen/32/18660_2.png) [@salbrechtsen](https://forum.inductiveautomation.com/u/salbrechtsen)\
**Post date:** [January 17, 2018, 3:05pm UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/7 "2018-01-17T15:05:25Z")

</div>

This is working for me in windows. Although I just realized the OP asked for response time too. @c.bertulli looks like he has the best answer.

```auto
import os

def ping(ip):
     return os.system("ping " + ip) == 0

print ping("8.8.8.8")
print ping("1.2.3.4")
print ping("4.4.4.4")

```

If you are running in Unix you’ll need to change the first part of the os.system call to `ping -c 5` to limit the number of ping attempts.

This should print out the results of the command and also return a boolean value for each function call. Results should be True, False, False

---

<div class="post-metadata">

**Author:** ![Vince](https://avatars.discourse-cdn.com/v4/letter/v/b5e925/32.png) [@Vince](https://forum.inductiveautomation.com/u/Vince)\
**Post date:** [January 17, 2018, 3:15pm UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/8 "2018-01-17T15:15:14Z")

</div>

Working good!

Thanks everone!

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [January 17, 2018, 3:50pm UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/9 "2018-01-17T15:50:53Z")

</div>

> [@salbrechtsen](#):
>
> import os
> 
> def ping(ip):  
> return os.system("ping " + ip) == 0
> 
> print ping("8.8.8.8")  
> print ping("1.2.3.4")  
> print ping("4.4.4.4")

Well that is weird, I tested your code last night, and it didn't work, and today it does. I stand corrected.

---

<div class="post-metadata">

**Author:** ![silverbacknet](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/silverbacknet/32/7385_2.png) [@silverbacknet](https://forum.inductiveautomation.com/u/silverbacknet)\
**Post date:** [May 29, 2020, 10:30pm UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/10 "2020-05-29T22:30:23Z")

</div>

Shelling out for this makes me feel very icky, especially when Java already has the native ability to do this and that’s an invitation to accidentally send horrible things to the command line. Why not just:

```auto
def ping(address,timeout=2000):
	from java.net import InetAddress
	ip = InetAddress.getByName(address)
	return ip.isReachable(timeout)

```

This will throw `UnknownHostException` if the host can’t be looked up, or `IOException` if the network is broken, so you can handle that case as well, instead of not even knowing it happened unless you parse unexpected command-line output. Or just swallow exceptions, of course.

[java.net](http://java.net).\* can do all the fun things you thought you needed command-line tools for, and more.

---

<div class="post-metadata">

**Author:** ![salbrechtsen](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/salbrechtsen/32/18660_2.png) [@salbrechtsen](https://forum.inductiveautomation.com/u/salbrechtsen)\
**Post date:** [June 2, 2020, 2:39pm UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/12 "2020-06-02T14:39:11Z")

</div>

Nice! I like this a lot more.

---

<div class="post-metadata">

**Author:** ![CraigP](https://avatars.discourse-cdn.com/v4/letter/c/ce73a5/32.png) [@CraigP](https://forum.inductiveautomation.com/u/CraigP)\
**Post date:** [October 12, 2020, 2:43pm UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/13 "2020-10-12T14:43:19Z")

</div>

Another downside to using os.system for ping in Windows is that it writes the ping results to the wrapper.log file. I'm pinging 100 devices every few minutes and this clogs up the log files like so:

> Blockquote  
> INFO | jvm 1 | 2020/10/09 17:45:00 | Pinging 9.9.9.9 with 32 bytes of data:  
> INFO | jvm 1 | 2020/10/09 17:45:00 | Reply from 9.9.9.9: bytes=32 time\<56ms TTL=64  
> INFO | jvm 1 | 2020/10/09 17:45:00 |  
> INFO | jvm 1 | 2020/10/09 17:45:00 | Ping statistics for 9.9.9.9:  
> INFO | jvm 1 | 2020/10/09 17:45:00 | Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),  
> INFO | jvm 1 | 2020/10/09 17:45:00 | Approximate round trip times in milli-seconds:  
> INFO | jvm 1 | 2020/10/09 17:45:00 | Minimum = 0ms, Maximum = 0ms, Average = 0ms

---

<div class="post-metadata">

**Author:** ![aaron1](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@aaron1](https://forum.inductiveautomation.com/u/aaron1)\
**Post date:** [November 16, 2021, 2:48am UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/14 "2021-11-16T02:48:07Z")

</div>

Hi @silverbacknet,

I am trying this in the Ignition script console but having issue getting it to work. (I am just trying to ping 8.8.8.8) It seems like Java always tries to use TCP Port 7 instead of ICMP. I’ve tried setting the Java Network setting to Direct Connection as the InetAddress documentation suggests that Java will only use ICMP if the privilege can be obtained, but no success.

If you have any suggestions that will be much appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![silverbacknet](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/silverbacknet/32/7385_2.png) [@silverbacknet](https://forum.inductiveautomation.com/u/silverbacknet)\
**Post date:** [November 16, 2021, 7:21am UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/15 "2021-11-16T07:21:42Z")

</div>

That’s a toughy. It always tries ICMP first, so I assume that’s blocked on the host, or perhaps just on the Java process. If it works via shelling out, you might be forced to use that.

That’s the only ICMP base Java gives you, raw sockets aren’t available (and generally locked to root/Administrator anyway). If you want to try one that never falls back, you could install icmp4j, which is a lot more flexible for pings/traceroutes and the like, but I’m not sure it’d help too much in this case.

Hm, it’s always possible it’s defaulting to the wrong interface, if there’s an overlap somehow, but that seems very unlikely.

---

<div class="post-metadata">

**Author:** ![aaron1](https://avatars.discourse-cdn.com/v4/letter/a/b5a626/32.png) [@aaron1](https://forum.inductiveautomation.com/u/aaron1)\
**Post date:** [November 18, 2021, 4:28am UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/16 "2021-11-18T04:28:36Z")

</div>

Hi @silverbacknet,

Thanks a lot for your reply. Yeah, the shelling out method is working quite well, so will probably stick to it for now. Will have to look into this a bit more when I get a chance.

---

<div class="post-metadata">

**Author:** ![tyler.bennett](https://avatars.discourse-cdn.com/v4/letter/t/f04885/32.png) [@tyler.bennett](https://forum.inductiveautomation.com/u/tyler.bennett)\
**Post date:** [February 9, 2023, 9:27am UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/17 "2023-02-09T09:27:32Z")

</div>

> [@silverbacknet](#):
>
> ```auto
> def ping(address,timeout=2000):
> from java.net import InetAddress
> ip = InetAddress.getByName(address)
> return ip.isReachable(timeout)
> 
> ```

I attempted to convert and use the [java.net](http://java.net) implementation with inetAddress and isReachable(). However it seems this method does not work as expected when running ignition as non-root user under Linux. I suppose I could run Ignition as root, or assign root privilege to my 'ignition' system account that I am using to run Ignition but those both seem like worse options than just "shelling out" to run a ping command.  
Has anyone identified an easy way to get the [java.net](http://java.net) method to work with a non-root system user account under Ubuntu?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [February 9, 2023, 1:09pm UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/18 "2023-02-09T13:09:45Z")

</div>

Add `CAP_NET_RAW` to your SystemD service's `AmbientCapabilities`. That will allow `.isReachable()` to work in a non-root environment. (I recommend also using `CAP_NET_BIND_SERVICE` so you can use the standard http and https ports with a non-root service.)

> [@Issue with java.net module import in Ignition V 8.1.5](https://forum.inductiveautomation.com/t/issue-with-java-net-module-import-in-ignition-v-8-1-5/59496/4):
>
> If on Linux and not running as root, use of any ping-like raw packet requires the CAP\_NET\_RAW capability (privilege). You can specify that in the SystemD service file (or override) with the AmbientCapabilities=CAP\_NET\_RAW setting. You can specify multiple capabilities with whitespace. I recommend AmbientCapabilities=CAP\_NET\_BIND\_SERVICE CAP\_NET\_RAW.

---

<div class="post-metadata">

**Author:** ![tyler.bennett](https://avatars.discourse-cdn.com/v4/letter/t/f04885/32.png) [@tyler.bennett](https://forum.inductiveautomation.com/u/tyler.bennett)\
**Post date:** [February 10, 2023, 2:27am UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/19 "2023-02-10T02:27:47Z")

</div>

Here is a lengthy recap of the steps that lead to success in using `java.net` (`InetAddress`, `isReachable`) on a Linux server (Ubuntu 20.04.5 LTS) with Ignition running as a non-root system user account.

1. Ran this command...

```auto
sudo systemctl edit Ignition-Gateway.service

```

> [@Change the user running the Ignition Gateway service](https://forum.inductiveautomation.com/t/change-the-user-running-the-ignition-gateway-service/68564/6):
>
> This will create an override file at `/etc/systemd/system/Ignition-Gateway.service.d/override.conf` that will persist through an Ignition upgrade (that may reset/revert that base configuration file).

1. Added this text... then saved changes.

```auto
[Service]
AmbientCapabilities=CAP_NET_RAW CAP_NET_BIND_SERVICE

```

> [@Change the user running the Ignition Gateway service](https://forum.inductiveautomation.com/t/change-the-user-running-the-ignition-gateway-service/68564/3):
>
> Consider specifying `AmbientCapabilities=CAP_NET_RAW CAP_NET_BIND_SERVICE` to allow Ignition to use ports 80 & 443 and to use built-in ping when not root.

1. Restarted the Ignition gateway...

```auto
sudo /usr/local/bin/ignition/gwcmd.sh -r

```

1. Confirmed the service was running without error

```auto
sudo systemctl status Ignition-Gateway.service

```

1. Confirmed the new settings appeared in the service configuration

```auto
sudo systemctl show Ignition-Gateway.service | grep AmbientCapabilities

```

At this point the `InetAddress` `isreachable` function was still not returning any positive results (all fails).  
I noticed that the results of the `systemctl status Ignition-Gateway.service` command was not showing a recent start date so the `gwcmd.sh -r' command must not have operated at the systemd service level...

1. Restarted the Ignition Gateway service using `systemctl`

```auto
sudo systemctl restart Ignition-Gateway.service

```

Then the `isReachable` function **finally** started returning positive results (successful pings).

For completeness here is my version of the ping function:

```python
	def ping(host, timeout=2000):
		from java.net import InetAddress 
		# (https://docs.oracle.com/javase/8/docs/api/java/net/InetAddress.html) 	
		ip = InetAddress.getByName(host)
		data = { 
			'isReachable':ip.isReachable(timeout)
			,'hostAddress':ip.getHostAddress()
			,'hostName':ip.getHostName()
		}
		return data
	result = ping('8.8.8.8')

```

Thanks to @silverbacknet , @pturmel and @kcollins for your forum posts that helped me sort this out.  
Now to see if I can do similar in a docker container.

---

<div class="post-metadata">

**Author:** ![Kevin.Collins](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.collins/32/13497_2.png) [@Kevin.Collins](https://forum.inductiveautomation.com/u/Kevin.Collins)\
**Post date:** [February 11, 2023, 2:35am UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/20 "2023-02-11T02:35:50Z")

</div>

> [@tyler.bennett](#):
>
> Now to see if I can do similar in a docker container.

Should be able to achieve a similar effect through [cap\_add](https://github.com/compose-spec/compose-spec/blob/master/spec.md#cap_add) (link is for the Compose service configuration, it looks like `--cap-add` is what you'd use for `docker run`).

---

<div class="post-metadata">

**Author:** ![Cole\_Townsend](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cole_townsend/32/53149_2.png) [@Cole\_Townsend](https://forum.inductiveautomation.com/u/Cole_Townsend)\
**Post date:** [February 9, 2024, 3:21pm UTC](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612/21 "2024-02-09T15:21:53Z")

</div>

So I followed all of these steps in order to try and get the [Java.net](http://Java.net) ping working from my edge server, but I still am getting nothing but failed pings here. CAP\_NET\_RAW is in fact set as an AmbientCapability for my Ignition Gateway service and I have reloaded the daemon and restarted the service. Still, nothing works. Is there any other solution to this issue that I am missing?

Should I just resort to running the Ignition service as root?

[Next page](https://forum.inductiveautomation.com/t/script-to-ping-computer/16612.md?page=2)
