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"]]},{"id":"e46a50e1.3cf68","type":"function","z":"b48ff32d.9757d8","name":"Check Download Speed","func":"const downloadSpeed = msg.payload.download || 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":"2c1807e7.7785ca","type":"function","z":"c92e0a21.46a508","name":"Check Download Speed","func":"const downloadSpeed = msg.payload.download || 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":[["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? ๐
Even though things aren’t always working the way we expect, us rookie developers can learn a few new tricks from the stuff these AIs return to us. The silver lining point of view…
Hi Doug
Your last response.. you used “write a python script to test if my internet connection is active every 5 minutes and return a result only if there is no internet connection”
Well, I tried that identical request – copied from yours.. – assuming we’re talking chatGPT…
“Certainly! You can use the requests library in Python to perform a simple HTTP request to a reliable server (e.g., Google’s DNS server) to check for internet connectivity. Here’s a Python script that tests your internet connection every 5 minutes and prints a result only if there is no internet connection:”
It just so happens my RPi has requests installed already…
here’s what it came up with for ME…
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.
print(“Performing additional actions for no internet connection.”)
time.sleep(300) # Sleep for 5 minutes (300 seconds)
So I ran it and …
python3 goog3.py
No internet connection.
Performing additional actions for no internet connection.
Unfortunately for chatGPT there WAS and IS a connection – to be sure I immediately did THIS.
ping google.com
PING google.com (142.250.185.14) 56(84) bytes of data.
64 bytes from mad41s11-in-f14.1e100.net (142.250.185.14): icmp_seq=1 ttl=118 time=816 ms
64 bytes from mad41s11-in-f14.1e100.net (142.250.185.14): icmp_seq=2 ttl=118 time=1043 ms
64 bytes from mad41s11-in-f14.1e100.net (142.250.185.14): icmp_seq=3 ttl=118 time=1629 ms
64 bytes from mad41s11-in-f14.1e100.net (142.250.185.14): icmp_seq=4 ttl=118 time=742 ms
64 bytes from mad41s11-in-f14.1e100.net (142.250.185.14): icmp_seq=5 ttl=118 time=2030 ms
Some day maybe ๐
As usual, the google version provides better results. We’ve often “asked Alexa” stuff and got worthless results only for me to pull out my phone and “ok google” which returns far better results. YMMV
I asked Bard your very first question and got this result:
import os
import time
while True:
response = os.system(“ping -c 1 google.com”)
if response != 0:
print(“Google is not responding.”)
time.sleep(300) # Sleep for 5 minutes (300 seconds)
Good morning Doug
Before we get too starry-eyed about Google – I just asked Bard my exact same first question as you did – and got a DIFFERENT ANSWER – this one failed miserably.
I changed 5 minutes to 1 minute as I have a reset button for my 4G router (which feeds my main router) and it only turns off for 30 seconds – add another minute for it to get a signal again..
Below you’ll see me invoke goog.py as I called what it gave me….and the awful result once the connection was lost….
python3 goog.py
Traceback (most recent call last):
File “goog.py”, line 14, in
ping_google()
File “goog.py”, line 6, in ping_google
response = subprocess.check_output([“ping”, “-c”, “1”, “google.com”], stderr=subprocess.STDOUT, universal_newlines=True)
File “/usr/lib/python3.7/subprocess.py”, line 395, in check_output
**kwargs).stdout
File “/usr/lib/python3.7/subprocess.py”, line 487, in run
output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command ‘[‘ping’, ‘-c’, ‘1’, ‘google.com’]’ returned non-zero exit status 2.
pi@mainpi:~:09:43[1]>
So I tried your code – calling it goog2.py – failed due to no indents – thanks WordPress. So I put in what I thought were the correct indents in your code and… I’ve included a screenshot of the code.. (only change – 60 seconds)
and here’s the incorrect result… which came out immediately..
python3 goog2.py
PING google.com (142.250.201.78) 56(84) bytes of data.
64 bytes from mad07s25-in-f14.1e100.net (142.250.201.78): icmp_seq=1 ttl=118 time=122 ms
— google.com ping statistics —
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 121.668/121.668/121.668/0.000 ms
It’s still looking like there’s no way out of having to know what you’re doing… which kind of defeats the object.. is there something wrong with my indenting (image)?
Note – I then disconnected the broadband and note the last 2 lines of output – so every minute I’m getting status info – and on failure I get 2 additional lines to signify no connection.. all good but again not what I wanted – which was nothing until loss of connection then just the one-liner…
Do we have a version issue or incorrect indenting?
Over to you…
I once had a chatGPT helper script which let me pick the resolution of the response(accuracy) but didn’t see that kind of setting for Bard. It might help congeal to a more similar response.
Regarding the echoing of the pinging, ya that’s a side effect and even asking the question stating to only provide the result when there is no connection failed to do so. I added ” > /dev/null” to the ping command to get the intended result of only a result upon failure.
Instead of asking it to use my idea of checking for the internet connection(ping google.com), I tried asking it just that: โwrite a python script to test if my internet connection is active every 5 minutes and return a result only if there is no internet connection.โ and it used ping again and would give me results when the connection returned.
Software developers don’t have to worry about AI taking their jobs just yet. But AI is providing results which can become a start to the solution, for software developers.
See my reply to you. If I put it right here we endup in a spiraling narrow page. So it’s at the end ๐
Ok, this gets better – I asked Bard to do my LAST example – a Node-Red flow… it made a flow (JSON) but when I tried to pull it into Node-Red it came without wires ๐ Just 3 nodes sitting there..
[
{
“id”: “n1”,
“type”: “http request”,
“url”: “https://www.speedtest.net/api/sync?units=Mbps”,
“method”: “GET”,
“ret”: “txt”,
“x”: 140,
“y”: 100,
“wires”: [
{
“target”: “n2”
}
]
},
{
“id”: “n2”,
“type”: “function”,
“func”: “function(msg) {\n\tvar data = JSON.parse(msg.payload);\n\tif (data.download.speed < 40) {\n\t\tmsg.payload = \"Slow Internet Detected\";\n\t} else if (data.download.speed === 0) {\n\t\tmsg.payload = \"No Internet Detected\";\n\t}\n\tmsg.payload = data.download.speed + \" Mbps\";\n\treturn msg;\n}", "wires": [ { "target": "n3" } ] }, { "id": "n3", "type": "debug", "name": "Internet Speed", "wires": [] } ]
Pete, I have often found that providing feedback to chatgpt regarding flaws in its initial response can elicit a correct response the second time
…which, I agree, may not be that helpful. Also, I’ve found that in my first prompt, asking it to double check that its answer complies with the request, or even ‘do a code walkthrough, simulating execution, to validate…’ can work. Odd and not intuitive, yes.
Not gone off teh boil with this – but strangely, my checking for internet connection has coincided with the worst 4G connectivity I’ve ever had. I’ve tested 2 different 4g routers, 2 different accounts with different companies and running Speedtest from the routers themselves to remove any doubts about my PC (hadrwired) connectivity… I’m getting pings as high as 600ms, effectively reducing my 4G speed from 100-150Mbps download, to almost zero at times – day 3 and counting.
Maybe someone has found a great place to store their acorns: https://www.youtube.com/watch?v=cZkAP-CQlhA
I’m not a ChatGPT guru, but I agree with Dave: tell the AI what it did wrong or what is missing, don’t start with a fresh command each time.
Ok and thanks for the great feedback gentlemen… DID YOU WATCH THE LAUNCH???