** Are you all suffering from a sudden "no internet access"? ** **
I was bothered. I have created a script to automate routine tasks and have it resident on an old PC. When I left the PC on, I was in a state of "no internet access" before I knew it, and I had to manually disconnect and reconnect the PC network. It's scary that the process stops before you know it, even though it's automated and left unattended.
Therefore, I tried to check the network status and reconnect in the script, so I will introduce it.
(I don't know why you don't have internet access in the first place, so please let me know who you are.)
・ Windows7 ・ Anaconda3
The content is mostly Windows commands, but I am calling the commands from Python because it is part of the script.
def is_ping_ok():
proc = subprocess.run(["ping", "yahoo.co.jp"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ret = proc.stdout.decode("cp932")
if "Not found" in ret:
return False
else: return True
def restart_network():
subprocess.run(["netsh", "wlan", "disconnect"])
subprocess.run(["netsh", "wlan", "connect", 'name="SSID you want to connect to"'])
time.sleep(5)
def check_network():
if not is_ping_ok():
restart_network()
if is_ping_ok():
return "Restarted!"
else: return "Error"
return "OK"
For the time being, I try it once and give up if it doesn't come back, but if you don't like losing, I think you can recurse thoroughly until it comes back.
Use the ping command to check if you have internet access. Please refer to this article for the subprocess that suddenly appeared.
The following is for internet access
>>> import subprocess
>>> proc = subprocess.run(["ping","yahoo.co.jp"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> print(proc.stdout.decode("cp932"))
yahoo.co.jp [182.22.59.229]Pinging to 32 bytes of data:
182.22.59.Response from 229:Number of bytes=32 hours=11ms TTL=50
182.22.59.Response from 229:Number of bytes=32 hours=16ms TTL=50
182.22.59.Response from 229:Number of bytes=32 hours=21ms TTL=50
182.22.59.Response from 229:Number of bytes=32 hours=21ms TTL=50
182.22.59.229 ping statistics:
Number of packets:Send=4, receive=4, loss= 0 (0%Loss)、
Approximate round trip time(millisecond):
minimum=11ms, maximum=21ms, average= 17ms
Without internet access
>>> proc = subprocess.run(["ping","yahoo.co.jp"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> print(proc.stdout.decode("cp932"))
Host yahoo in ping request.co.jp was not found. Check the host name and try again.
If the return value contains "not found", we have decided to determine that there is no internet access. I think this depends on the language of the terminal.
def is_ping_ok():
proc = subprocess.run(["ping", "yahoo.co.jp"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
ret = proc.stdout.decode("cp932")
if "Not found" in ret:
return False
else: return True
If you do not have internet access, try reconnecting the network with the following command. For details of the command, refer to this article.
> netsh wlan disconnect
Interface"Wireless network connection"Disconnect request completed successfully.
> netsh wlan connect name="SSID you want to connect to"
The connection request completed successfully.
When called with python, it will be as follows. I put sleep so that I will not move to the next process without completing the connection.
def restart_network():
subprocess.run(["netsh", "wlan", "disconnect"])
subprocess.run(["netsh", "wlan", "connect", 'name="SSID you want to connect to"'])
time.sleep(5)
I introduced how to fight the sudden "no internet access".
We are looking for a smarter way.
Recommended Posts