tiny-test-fw: implement env.get_pc_nic_info
:
this method is used to get mac/ipv4/ipv6 address for one NIC
This commit is contained in:
parent
c522f44276
commit
3b3a915552
2 changed files with 26 additions and 6 deletions
|
@ -17,6 +17,8 @@ import os
|
||||||
import threading
|
import threading
|
||||||
import functools
|
import functools
|
||||||
|
|
||||||
|
import netifaces
|
||||||
|
|
||||||
import EnvConfig
|
import EnvConfig
|
||||||
|
|
||||||
|
|
||||||
|
@ -130,17 +132,34 @@ class Env(object):
|
||||||
"""
|
"""
|
||||||
return self.config.get_variable(variable_name)
|
return self.config.get_variable(variable_name)
|
||||||
|
|
||||||
|
PROTO_MAP = {
|
||||||
|
"ipv4": netifaces.AF_INET,
|
||||||
|
"ipv6": netifaces.AF_INET6,
|
||||||
|
"mac": netifaces.AF_LINK,
|
||||||
|
}
|
||||||
|
|
||||||
@_synced
|
@_synced
|
||||||
def get_pc_nic_info(self, nic_name="pc_nic"):
|
def get_pc_nic_info(self, nic_name="pc_nic", proto="ipv4"):
|
||||||
"""
|
"""
|
||||||
get_pc_nic_info(nic_name="pc_nic")
|
get_pc_nic_info(nic_name="pc_nic")
|
||||||
try to get nic info (ip address, ipv6 address, mac address)
|
try to get info of a specified NIC and protocol.
|
||||||
|
|
||||||
:param nic_name: pc nic name. allows passing variable name, nic name value or omitted (to get default nic info).
|
:param nic_name: pc nic name. allows passing variable name, nic name value.
|
||||||
:return: a dict of address ("ipv4", "ipv6", "mac") if successfully found. otherwise None.
|
:param proto: "ipv4", "ipv6" or "mac"
|
||||||
|
:return: a dict of nic info if successfully found. otherwise None.
|
||||||
|
nic info keys could be different for different protocols.
|
||||||
|
key "addr" is available for both mac, ipv4 and ipv6 pic info.
|
||||||
"""
|
"""
|
||||||
# TODO: need to implement auto get nic info method
|
interfaces = netifaces.interfaces()
|
||||||
return self.config.get_variable("nic_info/" + nic_name)
|
if nic_name in interfaces:
|
||||||
|
# the name is in the interface list, we regard it as NIC name
|
||||||
|
if_addr = netifaces.ifaddresses(nic_name)
|
||||||
|
else:
|
||||||
|
# it's not in interface name list, we assume it's variable name
|
||||||
|
_nic_name = self.get_variable(nic_name)
|
||||||
|
if_addr = netifaces.ifaddresses(_nic_name)
|
||||||
|
|
||||||
|
return if_addr[self.PROTO_MAP[proto]][0]
|
||||||
|
|
||||||
@_synced
|
@_synced
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|
|
@ -128,6 +128,7 @@ The following 3rd party lib is required:
|
||||||
* pyserial
|
* pyserial
|
||||||
* pyyaml
|
* pyyaml
|
||||||
* xunitgen
|
* xunitgen
|
||||||
|
* netifaces
|
||||||
|
|
||||||
To build document, we need to install ``Sphinx`` and ``sphinx-rtd-theme`` (you may replace this with your own theme).
|
To build document, we need to install ``Sphinx`` and ``sphinx-rtd-theme`` (you may replace this with your own theme).
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue