linux - Python Check if Condition in Loop Changes -


i have created small python script monitor if os (linux) able ping ip address of 10.0.0.1 (router on lan). first script works fine have execute script have evaluate condition each time.

i think understand script exits after condition evaluated. tried including while else loop in second example below evaluates return of os call , place function call in loop keep checking if router plugged lan.

is correct approach define function evaluate ping status? tried adding function in last example doesn't seem update.

my goal run script once , have detect 10.0.0.1 or down when unplug network or plug in.

    import os     import time      ip = "10.0.0.1"     response = os.system("ping -c 1 " +ip)      if response != 0:             print ip, 'is down'             time.sleep(3)      else:        print ip, 'is up!' 

i tried adding while else loop condition doesn't seem updated.

    import os     import time      ip = "10.0.0.1"     response = os.system("ping -c 1 " +ip)      while response != 0:             print ip, 'is down'             time.sleep(3)      else:     print ip, 'is up!' 

i tried defining fuction evaluate condition every iteration of loop..

    import os     import time       ip = "10.0.0.1"     response = os.system("ping -c 1 " +ip)      def checkstatus():             response = os.system("ping -c 1 " +ip)      while response != 0:             print ip, 'is down'             time.sleep(3)             checkstatus()# trying evaluate if status has changed      while response != 1:             print ip, 'is up'             time.sleep(3)             checkstatus()# trying evaluate if status has changed  

edit

this how changed script , works..just need slow down network unreachable stdout.

    import os     import time      ip = "10.0.0.1"     while true:             response = os.system("ping -c 1 " + ip)             if response == 0:                     print'connected'                     time.sleep(.5) 

new output notice print statement connected appears connected

    --- 10.0.0.1 ping statistics ---     1 packets transmitted, 1 received, 0% packet loss, time 0ms     rtt min/avg/max/mdev = 0.383/0.383/0.383/0.000 ms     **connected**     ping 10.0.0.1 (10.0.0.1) 56(84) bytes of data.     64 bytes 10.0.0.1: icmp_seq=1 ttl=64 time=0.308 ms      --- 10.0.0.1 ping statistics ---     1 packets transmitted, 1 received, 0% packet loss, time 0ms     rtt min/avg/max/mdev = 0.308/0.308/0.308/0.000 ms     ***connected*** 

new output after unplugged few seconds (floods screen)

    connect: network unreachable     connect: network unreachable     connect: network unreachable     connect: network unreachable     connect: network unreachable 

nothing bad worried use many cpu cycles script running on raspberry pi.

the mistake make changes in response, doesn't reflect network status changes unless repeat assignment via response = os.system("ping -c 1 " +ip).

apart including assignment inside loop (as suggested deepspace) can change loop check correctly reflect network status changes (according method):

while os.system("ping -c 1 " +ip):     print ip, 'is down'     time.sleep(3) 

similarly, when using checkstatus() you'd need either include response = checkstatus() in loop or use in check: while checkstatus():.

the last version of code has couple of problems:

  • typically return code of program 0 on success, may have multiple non-zero return codes, != 1 check might not work you'd expect, better use == 0 check instead.

  • the sequenced potentially endless while statements won't complete unless sequence of connection state happens. since goal run once , find out current state of connection code better not depend on such sequence.

maybe instead?

if os.system("ping -c 1 " +ip):     print ip, 'is down' else:     print ip, 'is up' 

or, if prefer one-liners:

print ip, 'is ' + 'down' if os.system("ping -c 1 " +ip) else 'up' 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -