2017-10-10 13 views
0

오렌지 Pi 2g IoT 보드, 그래픽 인터페이스 및 배포판을 사용하고 있습니다. 우분투 16.04. 보드에는 파이어 폭스 스크립트로 내 Firebase 응용 프로그램에 URL을 보내기 위해 대부분 잘 작동하는 모뎀 2G가 있지만 때때로 연결이 설정되지 않습니다. wvdial을 통한 pppd 연결입니다. 내 모뎀 2G가 연결되어 있는지 여부를 하드웨어 (avulse LED 켜기/끄기)에 관해서 알고 싶습니다.파이썬 스크립트를 통해 PPP 연결이 있는지 어떻게 식별 할 수 있습니까? 그렇다면 LED를 켭니다.

누구나 나를 도와 줄 수 있습니까?

감사합니다.

+0

get_rx_bytes() 라스베리 & 제출 = 검색) 관련 raspberry pi. 어쩌면 당신의 필요에 맞는 것을 찾을 수 있을까요? – Matthias

답변

0

저는 이것을위한 파이썬 기능에 대해 잘 모릅니다. 그러나 나는 파이썬을 사용하여 네트워크 장치의 현재 상태를 알려주는 시스템 유틸리티 중 하나를 사용하여 프로세스를 포크화할 것을 제안합니다. 다음 라인을 따라하십시오 : Calling an external command in Python 및 예를 들어 "ifconfig"를 호출하십시오. ppp 장치가 나타나야합니다.

+0

입력 해 주셔서 감사합니다. 파이썬에서 외부 명령을 호출하는 것은 괜찮습니다. 이 작업은 import os 라이브러리를 사용하고 코드에 OS.System ('mycommand')을 작성하여 수행합니다. 예를 들어 "ifconfig"라고 쓸 수 있습니다. 하지만 문제는이 출력에서 ​​ppp 연결을 식별 할 수 있도록 어떻게 파싱 할 수 있는가입니다. –

+0

장치의 '그레이 핑'으로 벗어날 수 있습니다. – Matthias

0

외부 파이썬 패키지를 사용할 수있는 경우 : pip install netifaces.

이 패키지를 사용하면 인터페이스가 존재하는지 테스트 한 다음 Google에 접속할 수 있는지 테스트 할 수 있습니다. 이 코드는 테스트되지 않았지만 매우 가까이에 있어야합니다.

import netifaces 
import requests 

ppp_exists = False 
try: 
    netifaces.ifaddresses('ppp0') # this assumes that you only have one ppp instance running 
    ppp_exists = True 
except: 
    ppp_exists = False 

# you have an interface, now test if you have a connection 
has_internet = False 
if ppp_exists == True: 
    try: 
     r = requests.get('http://www.google.com', timeout=10) # timeout is necessary if you can't access the internet 
     if r.status_code == requests.codes.ok: 
      has_internet = True 
     else: 
      has_internet = False 
    except requests.exceptions.Timeout: 
     has_internet = False 

if ppp_exists == True and has_internet == True: 
    # turn on LED with GPIO 
    pass 
else: 
    # turn off LED with GPIO 
    pass 

당신은

os.system('ifconfig > name_of_file.txt') 

당신은 당신이 좋아이 어쨌든을 구문 분석 할 수 사용하여 텍스트 파일은 ifconfig의 출력을 기록 할 수

UPDATE. ppp 인터페이스가 존재하는지 확인하는 방법도 있습니다.

import os 
import netifaces 

THE_FILE = './ifconfig.txt' 

class pppParser(object): 
    """ 
    gets the details of the ifconfig command for ppp interface 
    """ 

    def __init__(self, the_file=THE_FILE, new_file=False): 
     """ 
     the_file is the path to the output of the ifconfig command 
     new_file is a boolean whether to run the os.system('ifconfig') command 
     """ 
     self.ppp_exists = False 
     try: 
      netifaces.ifaddresses('ppp0') # this assumes that you only have one ppp instance running 
      self.ppp_exists = True 
     except: 
      self.ppp_exists = False 
     if new_file: 
      open(the_file, 'w').close() # clears the contents of the file 
      os.system('sudo ifconfig > '+the_file) 
     self.ifconfig_text = '' 
     self.rx_bytes = 0 
     with open(the_file, 'rb') as in_file: 
      for x in in_file: 
       self.ifconfig_text += x 

    def get_rx_bytes(self): 
     """ 
     very basic text parser to gather the PPP interface data. 
     Assumption is that there is only one PPP interface 
     """ 
     if not self.ppp_exists: 
      return self.rx_bytes 
     ppp_text = self.ifconfig_text.split('ppp')[1] 
     self.rx_bytes = ppp_text.split('RX bytes:')[1].split(' ')[0] 
     return self.rx_bytes 

그냥 pppParser()를 호출합니다. 파이썬 패키지 인덱스에 파이썬 패키지의 광범위한 (https://pypi.python.org/pypi?%3Aaction=search&term=가있다

+0

입력 해 주셔서 감사합니다. Matt! 사실 나는 제한된 데이터 소비를 가지고있다. 내 장치가 예를 들어 몇 분마다 요청을 시도 할 수는 없습니다. 그것은 내 제품을 손상시킬 것입니다. 나는 'ifconfig'출력을 분석하고 RX (xx.xx) 데이터가 전송되었는지 분석하는 방법을 살펴보고자한다. 가능하다고 생각하니? –

+0

예 가능합니다. 간단한 텍스트 파서 메서드를 사용하여 대답을 업데이트했습니다. –