Get the job done

Robot avatar images lovingly delivered by

tao  •  23 May 2023   •    

I have been writing dirty code again, just to get the job done and make my life easier.

The systems I support are getting replaced and I have been given access to a script that tests network connectivity across the Internet to various services and ports. I do have access to run this manually, and it generates some XML or JSON files I can look at. And if I wait 24 hours, those report files are uploaded to a central monitoring system where I am able to get a better view of them. Trying to work through long XML files is not easy, especially when there are multiple server names being tested, multiple IP addresses per server name and multiple ports too.

So rather than having to wait 24 hours for the “pretty version” to be generated, I started to write my own parser for the JSON data. I want to be able to run the network test script and easily see the output in real time.

But, I got really stuck. Running JSON back into PHP doesn’t always work, especially when the JSON is multi-level, child-parent with attributes and titles in there too. I kinda got it working, but then realised I needed to iterate through the different IP addresses within each host and then the different port numbers within each IP too.

I got stuck. I tried using the XML data and importing that, which was better, but my head just hurt in the end. I was close to giving up.

So I changed tactics and just regex’ed my way through the XML file.

Each test was marked with blah blah XML. So I regex matched all of these lines from the original XML and got an array of the individual tests, which included their details and results.

From there, it was just a case of iterating through each one, doing some further preg_match_all for multiple IPs and then multiple ports within those IPs - but it worked!

In the end, it took me 1.5 hours to complete when using this new technique. I run the network tests, copy and paste the XML into my tool and bingo, it spits out a basic HTML table.

Sure, it ain’t pretty, but it gets the job done. Which means, I can get my job done too.


Discover more

Sourced from other writers across Lifelog

Ooops we couldn't find any related post...