这是一个简单实现,仅支持 Windows 和 Linux 系统,对操作系统的基础指令存在强依赖,或可导致结果误报。
如有可靠三方包支持该功能,烦请@maozexijr,不胜感激!!
# !/usr/bin/python3
# coding: utf-8
import os
import sys
def is_adapter_up(ip):
"""
ip like 127.0.0.1
"""
if 'win32' == sys.platform:
cmd = 'ipconfig |findstr "%s"'
# Using `ipconfig -all` to show all, includes not running
elif 'linux' == sys.platform:
cmd = 'ifconfig |grep "%s"'
# Using `ifconfig -a` to show all, includes not running
# Using `ifconfig NAME up` to start
# Using `ifconfig NAME down` to close
else:
print('Unsupported system type %s' % sys.platform)
return False
with os.popen(cmd % ip, 'r') as f:
if '' != f.read():
# UP and RUNNING
return True
else:
print('Network adapter %s is not running' % ip)
return False