A pal of mine just messaged me to say he’d asked ChatGPT to write a script to check network drives for space and usage – and it worked.
Brilliant I thought – so off I went to ask ChatGPT to write a python script to check for loss of Internet connection. Four attempts and it has still to write exactly what I asked for..
Attempt 1: “write a python script to ping Google every 5 minutes and return a result if there is no valid response.”
import subprocess import time def ping_google(): try: # Run the ping command and capture the output output = subprocess.check_output(["ping", "-c", "4", "google.com"], stderr=subprocess.STDOUT, text=True) # Check if the output contains "0% packet loss" which indicates a successful ping if "0% packet loss" in output: print("Ping to Google successful.") else: print("Ping to Google failed.") except subprocess.CalledProcessError as e: # If an error occurs, print the error message print(f"Error: e.output") if __name__ == "__main__": # Run the ping_google function every 5 minutes while True: ping_google() time.sleep(300) # Sleep for 5 minutes (300 seconds)
Ok, the script told me when all was ok, which was NOT what I asked for – so I tried again: “write a python script to ping Google every 5 minutes and return a result if there is no internet connection.”
import requests import time def check_internet_connection(): try: # Try making a request to Google's DNS server (8.8.8.8) response = requests.get("http://8.8.8.8", timeout=5) response.raise_for_status() # Raise an error for HTTP errors (4xx, 5xx) print("Internet connection is active.") except requests.RequestException: print("No internet connection.") if __name__ == "__main__": # Run the check_internet_connection function every 5 minutes while True: check_internet_connection() time.sleep(300) # Sleep for 5 minutes (300 seconds)
Still no – so now I got more specific – surely this would do the job: “write a python script to ping Google every 5 minutes and return a result ONLY if there is no internet connection.”
import requests import time def check_internet_connection(): try: # Try making a request to Google's DNS server (8.8.8.8) response = requests.get("http://8.8.8.8", timeout=5) response.raise_for_status() # Raise an error for HTTP errors (4xx, 5xx) except requests.RequestException: print("No internet connection.") return False else: return True if __name__ == "__main__": # Run the check_internet_connection function every 5 minutes while True: check_result = check_internet_connection() if not check_result: # Perform additional actions if there is no internet connection # For example, you can send a notification, log the event, etc. pass time.sleep(300) # Sleep for 5 minutes (300 seconds)
By now I was getting annoyed… my final attempt: “Write a python script to ping Google every 5 minutes and return a friendly result only if there is no valid response from Google, no result otherwise.”
import subprocess import time def ping_google(): try: # Run the ping command and capture the output output = subprocess.check_output(["ping", "-c", "4", "google.com"], stderr=subprocess.STDOUT, text=True) # Check if the output contains "0% packet loss" which indicates a successful ping if "0% packet loss" not in output: print("No valid response from Google. Check your internet connection.") except subprocess.CalledProcessError as e: # If an error occurs, print the error message print(f"Error: e.output") if __name__ == "__main__": # Run the ping_google function every 5 minutes while True: ping_google() time.sleep(300) # Sleep for 5 minutes (300 seconds)
And the result this time – almost but no prizes. If that’s a friendly response I’d hate to think what a non-friendly response might be.
python3 goog2.py Error: PING google.com (142.250.200.110) 56(84) bytes of data. --- google.com ping statistics --- 4 packets transmitted, 0 received, 100% packet loss, time 139ms
I think the problem here is that ChatGPT is acting as humans (in particular – mobile phone company support staff) mostly do – not actually listening to the question, but only part-listening then descending into a mental model of what it THINKS I should be asking. People do this all the time.
What I specifically asked for was a FRIENDLY result if no response. Eventually I changed the two print statements manually and…
python3 goog3.py No internet
But that kind of defeats the point.
So next, I thought I’d try a different approach: “Write a Nod-Red flow to check for slow (under 40Mbps download) or no Internet.” Yes, I spelled Node-Red incorrectly. And yet… It not only told me all about Node-Red and which nodes I’d need to install. “node-red-node-ping” – I already had that. “node-red-contrib-speedtest”. I’d ignored that as it WAS hopelessly out of date last time I looked. Well, it’s been updated. Next – it provided me with a complete flow:
["id":"69cf1915.55fe18","type":"inject","z":"b48ff32d.9757d8","name":"","props":["p":"payload","p":"topic","vt":"str"],"repeat":"300","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":140,"wires":[["21d9c670.7b0a38"]],"id":"21d9c670.7b0a38","type":"ping","z":"b48ff32d.9757d8","mode":"timed","name":"Ping Google DNS","host":"8.8.8.8","timer":"10","inputs":1,"x":330,"y":140,"wires":[["95d10ac0.f2c56"]],"id":"95d10ac0.f2c56","type":"switch","z":"b48ff32d.9757d8","name":"Ping Result","property":"payload","propertyType":"msg","rules":["t":"lte","v":"0","vt":"str","t":"else"],"checkall":"true","repair":false,"outputs":2,"x":530,"y":140,"wires":[["a5d2a31a.79c8c8"],["d344b313.27fc6"]],"id":"a5d2a31a.79c8c8","type":"function","z":"b48ff32d.9757d8","name":"No Internet","func":"msg.payload = \"No Internet or slow connection\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":710,"y":100,"wires":[["ba50e9c8.5ac53"]],"id":"d344b313.27fc6","type":"speedtest","z":"b48ff32d.9757d8","x":530,"y":180,"wires":[["e46a50e1.3cf68"]], 0;\n\nif (downloadSpeed < 40) \n msg.payload = \"Download speed is under 40 Mbps\";\n else \n msg = null; // No issue with speed\n\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":730,"y":180,"wires":[["ba50e9c8.5ac53"]],"id":"ba50e9c8.5ac53","type":"debug","z":"b48ff32d.9757d8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":910,"y":140,"wires":[]]
Could this be any good?
I noted that if I removed the Internet connection it did indeed produce a friendly message but also error messages – so I changed the debug node (green) to show only msg.payload.
I’m still not sure how to suppress msg.error and neither is chatGPT- but msg.payload certainly came up with the goods.
If anyone has a quick response at to how to suppress the error output… I’m all ears…. I guess I could add in “suppress error output”.
Ok here goes..
“Write a Node-Red flow to check for slow (under 40Mbps download) or no Internet. Suppress error output.”
Same flow slightly amended I guess. Check the output below:
REALLY? NOPE – completely failed to suppress error outputs despite clarifying that it would. The messages are quite reasonable – I just don’t want them.
Wednesday Update:
Alright – you’ll see in the comments that BARD should do better – in my experience (all 24 hours of it) no. So I continued with Node-Red. I asked chatGPT a variation on my last attempt above…
“Write a Node-Red flow to check for slow (under 40Mbps download) or no Internet. Suppress error output. Suppress msg.err output.”
Well, didn’t I think I was being clever. And so did ChatGPT…
Certainly! Below is a modified Node-RED flow that checks for slow (under 40Mbps) or no internet, suppresses error output, and also suppresses the msg.err
property:
["id":"1c6e1223.7670c5","type":"inject","z":"c92e0a21.46a508","name":"","props":["p":"payload","p":"topic","vt":"str"],"repeat":"300","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":140,"wires":[["762f72fc.d98df8"]],"id":"762f72fc.d98df8","type":"ping","z":"c92e0a21.46a508","mode":"timed","name":"Ping Google DNS","host":"8.8.8.8","timer":"10","inputs":1,"x":330,"y":140,"wires":[["cc2fb2c6.57e7d"]],"id":"cc2fb2c6.57e7d","type":"switch","z":"c92e0a21.46a508","name":"Ping Result","property":"payload","propertyType":"msg","rules":["t":"gte","v":"0","vt":"str","t":"else"],"checkall":"true","repair":false,"outputs":2,"x":530,"y":140,"wires":[["4f2a24e8.19c2a4"],["ffca7dd0.c846b8"]],"id":"4f2a24e8.19c2a4","type":"speedtest","z":"c92e0a21.46a508","x":530,"y":180,"wires":[["2c1807e7.7785ca"]],"id":"ffca7dd0.c846b8","type":"function","z":"c92e0a21.46a508","name":"No Internet","func":"msg.payload = \"No Internet or slow connection\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":710,"y":100,"wires":[["ca3de91b.7adfc"]],,"id":"ca3de91b.7adfc","type":"debug","z":"c92e0a21.46a508","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":910,"y":140,"wires":[],"id":"9d26d8c7.224f98","type":"suppress","z":"c92e0a21.46a508","name":"","path":"","pathType":"str","invert":false,"gap":"","strategy":"filter","x":310,"y":220,"wires":[["cc2fb2c6.57e7d"]],"id":"8736f7fc.5f77b","type":"suppress","z":"c92e0a21.46a508","name":"","path":"err","pathType":"msg","invert":false,"gap":"","strategy":"remove","x":310,"y":260,"wires":[["cc2fb2c6.57e7d"]]]
In this flow, two suppress
nodes have been added:
- The first
suppress
node (id: 9d26d8c7.224f98
) suppresses output related to errors or failures from theping
node. This ensures that the flow continues running even if there are intermittent connection issues. - The second
suppress
node (id: 8736f7fc.5f77b
) suppresses themsg.err
property, ensuring that errors are not propagated further down the flow. This can be useful for preventing unnecessary error messages in downstream nodes.
The above is the output from ChatGPT. The only problem here is… I can find no such thing as node-red-node-suppress or node-red-contrib-suppress.
There is a CATCH node in node-red (in the common area) but that fell over after first few errors..
If neither ChatGPT nor BARD can solve such a simple request – how will they save the world? 🙂