2017-03-08 19 views
-1

Python與netifaces很適合在localhost上收集ip,netmask信息,但是我很難從遠程服務器收集相同的信息。我猜netifaces不喜歡paramiko以及遠程服務器上的子進程或os.system。遠程Linux主機上的Python - netifaces模塊

def interface_details(): 

    for iface in netifaces.interfaces(): 

     if iface == 'lo': 

      continue 

     iface_details = netifaces.ifaddresses(iface) 

     if iface_details.has_key(netifaces.AF_INET): 

      ipv4 = iface_details[netifaces.AF_INET] 

     return ipv4 

,這是它是如何失敗的:

# Using Paramiko to execute cmd. 
>>> host_ssh.exec_command(interface_details()) 

    File "build/bdist.linux-x86_64/egg/paramiko/client.py", line 441, in exec_command 
    File "build/bdist.linux-x86_64/egg/paramiko/channel.py", line 60, in _check 
    File "build/bdist.linux-x86_64/egg/paramiko/channel.py", line 231, in exec_command 
    File "build/bdist.linux-x86_64/egg/paramiko/message.py", line 285, in add_string 
    File "build/bdist.linux-x86_64/egg/paramiko/common.py", line 170, in asbytes 
Exception: Unknown type 

感謝

回答

0

exec_command執行命令,而不是一個Python函數。例如,您可以執行ifconfig,然後解析其輸出。或者,您可以使用RPyC遠程執行實際的Python代碼。它需要在服務器上運行一項特殊服務,所以它不像SSH那麼簡單。但是運行Python代碼並直接與結果交互可能會有所幫助。

+1

您也可以通過ssh將本地python腳本通過管道連接到遠程python。 – pvg