The Curious Dev

Various programming sidetracks and shiny-object detours


In addition to the URL Status Check script, I’ve also written a basic script to continuously ping a system.

Whilst probably not the most efficient nor simplest way to do it, it is achieves the goal of returning the ping result. As an added bonus, it uses a little corner of groovy that most will find useful at some point or another, executing something on the system command line.

In Groovy, this is achieved with a simple script like:

def ip = ''
def response = "ping $ip".execute()

Which will call the ping command in the windows command line and then provide the response, we need extra code to parse that response into something useful.

A pretty basic regex will allow us to do that, such as this: /[0-9]+(?=ms)/

This brings in another piece of Groovy functionality, the regex find operator ~=.

The Groovy PLEAC section on Pattern Matching gives a good run down on regex that I keep going back to.

For the script we’re basically going to walk through each line in the response and for those that start with “Reply from” (note the startsWith syntactic sugar) we’ll use the regex to extract just the numerical value.

def response = "ping $ip".execute()
 response.text.eachLine() { l ->
  if (l.startsWith('Reply from')) {
    def finder = l =~ msPattern
    if (finder.count > 0) {
      (0..<finder.count).each {
        try {
          if (finder[it] != null) {
            pingList << finder[it]
            ping += new BigInteger(finder[it])
        } catch (Exception e) {
          println 'WARN: caught exception ' + e + ': ' + l

With the Windows ping util it typically returns 4 responses and we’ll just average them, but sometimes you might not get a response so in that case it doesn’t get included.

The complete script is in the bitbucket repo here.


Included file 'facebook_like.html' not found in _includes directory