2017-04-23 45 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系統實用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'需要一個參數列表。你已經給它一個字符串。刪除雙引號,它應該沒問題。此外,'python2.6'不再被支持。你應該儘快移到'python2.7'或最好是'python3',而不是稍後。 –

+0

@DaniSpringer,對不起,我不明白你的問題。我只是試圖讓'eth0'與'ethtool'命令是Linux系統命令,以便接收,說第二個函數需要獲取'/ sbin目錄/的ethtool eth0'輸出的輸出。代碼中沒有調用文件。 – krock1516

回答

1

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

這仍然是同樣的問題與原來的問題。你正在傳遞一個字符串並期待shell調用Python函數?

這裏有沒有引號唯獨身邊的實際shell命令

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

或者,使另一個變量

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

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

請注意,您仍然需要到grep(或掃描與蟒蛇輸出)對於「速度」

+0

cricket_007 .....你正確地抓住了它,雖然我已經刪除了這些字面意思字符串,但我錯過了刪除引用的功能,我認識到你的答案後。雖然真正的調味汁仍然需要extarcted :)''速度''的東西。感謝您的輸入..這將是偉大的任何歷史。 – krock1516

+0

你可以將子進程傳遞給使用grep的另一個子進程,或者使用正則表達式,或者循環輸出 –

+0

cricket_007 ..感謝提示,還請將'popen'編輯爲'Popen',這是一個打字錯誤在你提供的代碼中,以防有人複製粘貼。 – krock1516

相關問題