Script to ping computer

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...
sudo systemctl edit Ignition-Gateway.service
  1. Added this text... then saved changes.
[Service]
AmbientCapabilities=CAP_NET_RAW CAP_NET_BIND_SERVICE
  1. Restarted the Ignition gateway...
sudo /usr/local/bin/ignition/gwcmd.sh -r
  1. Confirmed the service was running without error
sudo systemctl status Ignition-Gateway.service
  1. Confirmed the new settings appeared in the service configuration
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
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:

	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 @kcollins1 for your forum posts that helped me sort this out.
Now to see if I can do similar in a docker container.

3 Likes