2016-01-24 47 views
0

要麼分析和修復代碼,要麼指向正確的方向。如此多的錯誤,有些可以克服,有些則不會。樹莓PI - ping多個IP

程序在Raspberry PI2上運行,應嘗試ping特定的IP地址並返回結果。

編程非常新,你可以告訴!不知道如果我需要一個ping庫或可以做到這一點

import sys 
import time 
from pushbullet import Pushbullet 
import serial 

class Users(object): 
    def __init__(self, name=None, ip=None): 
     self.name = name 
     self.ip = ip 
     self.status = 'out' 
pb = Pushbullet("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") #Pushbullet ID removed 

userList = [] 
userList.append(Users("Ali", "192.18.1.14")) 
userList.append(Users("Sophie", "192.18.1.9")) 
userList.append(Users("TV", "192.18.1.7")) 

try: 
    while True: 
     print "Checking... " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()) 
     for user in userList: 
      result = os.system ("ping -n 1 " = user.ip) 
      oldStatus = user.status 
      if (result == 0): 
        #What we'll do if a device is detected 
       if (oldStatus == 'out'): 
        push = pb.push_note("Home Pi", user.name + " is home") 
        user.status = 'in' 
       print user.name + " is home" 
      else: 
       #What we'll do if a device is NOT not detected 
       if (oldStatus == 'in'): 
        push = pb.push_note("Home Pi", user.name + " has just left") 
        user.status = 'out' 
       print user.name + " is out" 

     print "Next check will be in 30 seconds" 
     time.sleep(30) 

     except (KeyboardInterrupt, SystemExit): 

回答

0

我修改你的代碼工作,我沒有pushbullet。將我的新代碼與您之前的代碼進行比較,以查看差異和錯誤


import sys 
import time 
#from pushbullet import Pushbullet 
#import serial 
#you need import os 
import os 

class Users(object): 
    def __init__(self, name=None, ip=None): 
     self.name = name 
     self.ip = ip 
     self.status = 'out' 
#pb = Pushbullet("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") #Pushbullet ID removed 

userList = [] 
userList.append(Users("Notebook", "192.168.1.2")) 
userList.append(Users("TV", "192.168.1.4")) 

try: 
    while True: 
     print "Checking... " + time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()) 
     for user in userList: 
      #result = os.system ("ping -n 1 " = user.ip) 
     # to concatenate string you need + sign 
      #result = os.system("ping -n 1 " + user.ip) 
      # -n is a wrong option for ubuntu 
      result = os.system("ping -c 1 " + user.ip) 
      oldStatus = user.status 
      if (result == 0): 
        #What we'll do if a device is detected 
       if (oldStatus == 'out'): 
        #push = pb.push_note("Home Pi", user.name + " is home") 
        user.status = 'in' 
       print user.name + " is home" 
      else: 
       #What we'll do if a device is NOT not detected 
       if (oldStatus == 'in'): 
        #push = pb.push_note("Home Pi", user.name + " has just left") 
        user.status = 'out' 
       print user.name + " is out" 

     print "Next check will be in 30 seconds" 
     time.sleep(30) 

#wrong identation 
except (KeyboardInterrupt, SystemExit): 
    sys.exit(0) 
+0

It works!非常感謝您的幫助和幫助。比較這兩者確實有助於理解錯誤,所以也學到了很多東西。有沒有簡單的方法來靜音ping輸出?返回是數據包的詳細信息,而且我不希望這些顯示,只有user.name?謝謝 – Paul

+0

試試這個:result = os.system(「ping -c 1」+ user.ip +「>/dev/null」) – bdn02

+0

完美,謝謝。鍛鍊了魅力! – Paul