2017-04-23 21 views
0

私は関数get_ethnameを別の関数get_ethSpeedに呼び出そうとしていますが、呼び出す方法を理解できません。一つの関数をPython 2.6の別の関数に呼び出す方法

アドバイスをいただきありがとうございました。

The output of the first function returns the name of the NIC interface on the system as below..

[[email protected]/]# cat intDetail1.py 
#!/usr/bin/python 
import ethtool 

def get_ethname(): 
    inames = ethtool.get_devices() 
    inameCurr = inames[1] 
    print inameCurr 
    return inameCurr 

def main(): 
    get_ethname() 

main() 
[[email protected] /]# ./intDetail1.py 
eth0 

Below is the main code where i'm trying to call it.

#!/usr/bin/python 
    import ethtool 
    import subprocess 

    def get_ethname(): 
     inames = ethtool.get_devices() 
     inameCurr = inames[1] 
     print inameCurr 
     return inameCurr 

    def get_ethSpeed(): 
     spd = subprocess.popen("['ethtool', 'get_ethname']", stdout=subprocess.PIPE).communicate()[0] 
     print spd 
     return spd 

    def main(): 
     get_ethname() 
     get_ethSpeed() 

    main() 

When i run the above code it gives the below error .

File "/usr/lib64/python2.6/subprocess.py", line 1234, in _execute_child 
    raise child_exception 
OSError: [Errno 2] No such file or directory 

私の目的は、システム上のメインの実行中のインタフェース名を取得し、その後、速度はLinuxシステムのユーティリティを使用してNICを決定しますですethtoolインターフェイスの速度を示す:

[[email protected] /]# /sbin/ethtool eth0| grep Speed 
       Speed: 1000Mb/s 

Output look of ethtool eth0 is Below:

[[email protected] /]# ethtool eth0 
Settings for eth0: 
     Supported ports: [ TP ] 
     Supported link modes: 10baseT/Half 10baseT/Full 
           100baseT/Half 100baseT/Full 
           1000baseT/Full 
     Supported pause frame use: No 
     Supports auto-negotiation: Yes 
     Advertised link modes: 10baseT/Half 10baseT/Full 
           100baseT/Half 100baseT/Full 
           1000baseT/Full 
     Advertised pause frame use: No 
     Advertised auto-negotiation: Yes 
     Speed: 1000Mb/s 
     Duplex: Full 
     Port: Twisted Pair 
     PHYAD: 1 
     Transceiver: internal 
     Auto-negotiation: on 
     MDI-X: Unknown 
     Supports Wake-on: g 
     Wake-on: g 
     Link detected: yes 
+0

ファイル名とそのツリー構造/関係は何ですか? –

+1

'popen'はargsのリストを必要とします。代わりに文字列を指定しました。二重引用符を削除しても問題ありません。さらに、 'python2.6'はもはやサポートされていません。あなたは 'python2.7'に移動するか、遅くなく、より早く' python3'に移動するべきです。 –

+0

@DaniSpringer、申し訳ありませんが、私はあなたの質問を正確に得ることができませんでした。私は 'eth0'をLinuxのシステムコマンドである' ethtool'コマンドで受信させようとしていますので、 '/ sbin/ethtool eth0'出力の出力を取り出す必要があります。コードにはファイルが呼び出されていません。 – krock1516

答えて

1

No such device Settings for get_ethname(): No data available

これはまだ元の質問と同じ問題です。あなたはリテラル文字列を渡していて、シェルがPython関数を呼び出すことを期待していますか?

実際のシェルコマンド

spd = subprocess.Popen(['/sbin/ethtool', get_ethname()], stdout=subprocess.PIPE).communicate()[0] 

それとも、あなたはまだgrep検索(やPythonを使って出力をスキャン)する必要があります別の

iface = get_ethname() 
# Or 
iface = ethtool.get_devices()[1] 

spd = subprocess.Popen(['/sbin/ethtool', iface], stdout=subprocess.PIPE).communicate() 
return spd[0] 

変数ノートを作るの周りを除き、ここには引用符がありません"スピード"について

+0

cricket_007 .....あなたは正しくそれを捕まえましたが、私はそれらのリテラル文字列をすでに削除しましたが、私はあなたの答えの後に実現した関数の周りの引用符を削除することができませんでした。実際のソースはまだ「スピード」なものである必要があります。あなたの入力のおかげで。 – krock1516

+0

サブプロセスをgrepを使用する別のサブプロセスにパイプするか、regexを使うか、出力の行をループします。 –

+0

cricket_007 ..ヒントのおかげで、 'popen'を' Popen'に編集してください。あなたが提供したコードで、誰かがそれを貼り付ける場合に備えて。 – krock1516

関連する問題