2017-10-10 12 views
0

私はOrange Pi 2g IoTボードを使用していますが、グラフィカルインターフェイスとディストリビューションはありませんUbuntu 16.04。ボードには2Gのモデムがあり、PythonスクリプトでFirebaseアプリケーションにURLを送信するのにほとんどうまく動作しますが、接続が確立されないことがあります。これはwvdialを介したpppd接続です。私はモデム2Gが接続されているかどうかをハードウェア(avulse LEDのオン/オフ)に関して認識したいと思います。PPP接続があるかどうかをPythonスクリプトで確認するにはどうすればいいですか?

誰でも私を助けてくれますか?

多くの感謝!

+0

pppParser()を呼び出しますラズベリー&提出=検索)ラズベリーパイに関連する。おそらく、あなたのニーズに合ったものを見つけるでしょうか? – Matthias

答えて

0

私はこのためのpythonの機能について知りません。しかし、私はあなたがネットワークデバイスの現在の状態を与えるシステムユーティリティの1つを使ってプロセスをforkする必要があるなら、Pythonを使うことを提案します。あなたはこの行に従うことができます:Calling an external command in Pythonと例えば "ifconfig"を呼び出します。あなたのpppデバイスがそこに現れるはずです。

+0

入力いただきありがとうございます。 Pythonで外部コマンドを呼び出すことはOKです。私はこれをimport osライブラリを使って行い、OS.System( 'mycommand')をコードに書いています。私は例えば "ifconfig"と書くことができます。しかし、問題はどのように私はこの出力でppp接続を識別することができます私はそれを解析することができますか? –

+0

デバイスの「グロッピング」だけで逃げることができます。 – Matthias

0

外部Pythonパッケージを使用できる場合は、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 

UPDATE

あなたは、あなたが好きこのとにかくを解析することができます

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

を使用してテキストファイルにifconfigコマンドの出力をログに記録することができます。 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 

だけのpythonパッケージインデックス上のPythonパッケージの拡張セット(https://pypi.python.org/pypi?%3Aaction=search&term=があります。get_rx_bytes()

+0

ありがとう、Matt!実際には私は限られたデータ消費量しか持っていません。私のデバイスが例えば数分おきにリクエストを試みることは実現できません。それは私の製品を妥協するでしょう。私は 'ifconfig'の出力を解析し、RX(xx.xx)データが送られてきたかどうかを解析する方法を知りたいと思います。それが可能だと思いますか? –

+0

はい、可能です。私は単純なテキストパーサメソッドで答えを更新しました。 –

関連する問題