2017-03-08 7 views
-1

Python with netifacesは、ローカルホスト上でip、netmask情報を収集するのに最適ですが、リモートサーバから同じ情報を収集するのは非常に困難です。私はnetifacesがparamikoとサブプロセスやos.systemのようなリモートサーバを好まないと思う。Python - リモートLinuxホスト上の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を使用して、実際のP​​ythonコードをリモートで実行することもできます。サーバー上で特別なサービスを実行する必要があるため、SSHほどシンプルではありません。しかし、Pythonコードを実行し、結果と直接対話することは直接役立つ可能性があります。

+1

また、ローカルのpythonスクリプトをssh経由でリモートのPythonにパイプすることもできます。 – pvg

関連する問題