2017-04-23 1 views
0

get_ethname을 다른 함수 get_ethSpeed에 호출하려고하는데,이 함수를 호출하는 방법을 이해할 수 없습니다.한 함수를 파이썬 2.6에서 다른 함수로 호출하는 방법 02

고급 입력에 감사드립니다.

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 

내 목표는 시스템의 주요 실행 인터페이스 이름에 도착하고 다음 리눅스 시스템 유틸리티 ethtool를 사용하여 속도가 NIC의 결정 얻을 수있다 인터페이스의 속도를 알려주는 :

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'을 리눅스 시스템 명령 인'ethtool' 명령으로 받으려고 노력하고 있습니다. 그래서 두번째 기능은'/ sbin/ethtool eth0' 출력의 출력을 가져와야한다고 말합니다. 코드에 호출 된 파일이 없습니다. – krock1516

답변

1

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

이 여전히 원래의 질문과 같은 문제 10 . 리터럴 문자열을 전달하고 쉘이 파이썬 함수를 호출 할 것으로 기대합니까?

는 실제 쉘 명령 주위를 제외하고 여기에는 따옴표가 없습니다

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

또는, 당신은 여전히 ​​grep을해야 다른 변수

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

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

를 기록합니다 (또는 파이썬과 출력을 스캔) "Speed"에 대해

+0

cricket_007 ..... 올바르게 리터럴 문자열을 제거했지만 올바르게 붙잡 았지만 답장을 한 후에 깨달은 함수를 따옴표를 제거하지 못했습니다. 실제 소스는 여전히 extarcted해야합니다 :) 그 "속도"'. 귀하의 입력을 주셔서 감사합니다. – krock1516

+0

grep을 사용하는 다른 서브 프로세스로 서브 프로세스를 파이프하거나 regex를 사용하거나 해당 출력의 라인을 반복 할 수 있습니다. –

+0

cricket_007 .. 힌트를 주셔서 감사합니다. 또한 'popen'을'Popen'으로 편집하십시오. 제공 한 코드에서 누군가가 붙여 넣기를 할 경우를 대비하여 – krock1516

관련 문제