他の質問はまったく同じではありません。
私が達成しようとしているのは、システム上のすべての IP アドレスのリストを返し、次の動作をエミュレートする Python 関数です。
ifconfig | grep 'inet addr:' | grep -v 127.0.0.1 | cut -d: -f2 | awk '{ print $1}'
他の質問はまったく同じではありません。
私が達成しようとしているのは、システム上のすべての IP アドレスのリストを返し、次の動作をエミュレートする Python 関数です。
ifconfig | grep 'inet addr:' | grep -v 127.0.0.1 | cut -d: -f2 | awk '{ print $1}'
subprocess
これを実現するには、Pythonモジュールを使用できます。
import subprocess
cmd = "ifconfig | grep 'inet addr:' | grep -v 127.0.0.1 | cut -d: -f2 | awk '{ print $1}'"
co = subprocess.Popen([cmd], shell = True, stdout = subprocess.PIPE)
ips = co.stdout.read().strip().split("\n")
これで、IPアドレスのリストが表示されます。
PS:代わりに次のコマンドを使用する方が良いでしょう
ifconfig | grep inet | grep -v inet6 | grep -v 127.0.0.1 | awk '{print $2}' | cut -d\: -f2 | cut -d\ -f1
これにより、IPV6アドレスがあれば除外されます。
純粋なPythonの方法
これを完全にPythonで実行する場合は、netifacespythonモジュールをチェックアウトしてください。