Recovering from Lost Internet

Posted on
Thu Mar 01, 2018 5:20 pm
DaveL17 offline
User avatar
Posts: 4299
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Recovering from Lost Internet

I'm getting results that I don't understand. Using this construct in terminal works as expected:
Code: Select all
import os
from time import sleep

response = '1'

while response != 0:
    response = os.system("ping -c 1 google.com")
    sleep(2)
    print('.')

On the first trip through, the result with Internet is instant:
Code: Select all
PING google.com (172.217.4.78): 56 data bytes
64 bytes from 172.217.4.78: icmp_seq=0 ttl=49 time=29.679 ms

--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 29.679/29.679/29.679/0.000 ms
.

Remove Internet, run, and then restore Internet:
Code: Select all
ping: cannot resolve google.com: Unknown host
.
ping: cannot resolve google.com: Unknown host
.
ping: cannot resolve google.com: Unknown host
.
ping: cannot resolve google.com: Unknown host
.
ping: cannot resolve google.com: Unknown host
.
ping: cannot resolve google.com: Unknown host
.
ping: cannot resolve google.com: Unknown host
.
PING google.com (172.217.4.78): 56 data bytes
64 bytes from 172.217.4.78: icmp_seq=0 ttl=49 time=47.809 ms

--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 47.809/47.809/47.809/0.000 ms
.

Same code, modified to run in a plugin:
Code: Select all
except requests.exceptions.ConnectionError:

    while response != 0:
        response = os.system("ping -c 1 google.com")
        self.sleep(2)
        indigo.server.log(u"{0}".format(response))

When Internet is removed, requests throws the exception and the while loop is called. However, when Internet is restored, the while loop is never broken, the ping response never reverts to 0 (zero) and instead just keeps reporting the non-zero number.

What am I missing?

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Thu Mar 01, 2018 6:10 pm
Colorado4Wheeler offline
User avatar
Posts: 2747
Joined: Jul 20, 2009
Location: Colorado

Re: Recovering from Lost Internet

How about:

Code: Select all
while int(response) != 0:

or
Code: Select all
while unicode(response) != '0':

or
Code: Select all
while not response == '0'

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Thu Mar 01, 2018 7:30 pm
FlyingDiver offline
Posts: 2610
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Recovering from Lost Internet

Are you sure it's actually calling ping in that loop? Change it to "/sbin/ping" and see what happens. You might be getting an OS or shell error rather than a ping error.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Thu Mar 01, 2018 8:01 pm
DaveL17 offline
User avatar
Posts: 4299
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Recovering from Lost Internet

FlyingDiver wrote:
Are you sure it's actually calling ping in that loop? Change it to "/sbin/ping" and see what happens. You might be getting an OS or shell error rather than a ping error.

Thanks Joe - calling it from /sbin/ was the answer. Now it's working perfectly. Appreciate it.

I would've probably figured it out on my own if Matt made the font bigger. :D

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests