提问人:PhilBot 提问时间:11/11/2023 最后编辑:PhilBot 更新时间:11/14/2023 访问量:67
python pyroute2 将 WiFi 接口移动到网络命名空间
python pyroute2 move WiFi interface into network namespace
问:
我想使用 python pyroute2 在 Linux 上发现一个 USB WiFi 加密狗并将其移动到网络命名空间中。
我可以使用以太网接口来做到这一点,但我知道如果它是 wifi 接口,我必须使用 PHY 与 pyroute2 一起移动它。我检查了文档,没有看到如何执行此操作的示例。
以下是我目前所拥有的。如何使用 pyroute2 将我的 USB WiFi 加密狗移动到网络命名空间中?
# Inspect networking interfaces
import netifaces
# Setup netowrking namespaces
from pyroute2 import NetNS, NSPopen, netns, IPDB
from pyroute2 import IW
from pyroute2 import IPRoute
from pyroute2.netlink import NetlinkError
# Get a list of all the network interfaces
interfaces = netifaces.interfaces()
# Manage interfaces
ipdb = IPDB()
# Loop over all the interfaces and print their details
for iface in interfaces:
iface_details = netifaces.ifaddresses(iface)
if iface.find("wlxc") >= 0:
print(f"Found USB Wi-Fi interface: {iface} with details: {iface_details}")
# Truncate the long name
truncated_name = iface[-8:-1]
# Create a networking namespace
ipdb_netns = IPDB(nl=NetNS(truncated_name))
# Command to run
program_command = ["ifconfig"]
# Get the interface
print(f"Truncated name is: {truncated_name}")
link_iface = ipdb.interfaces[iface]
print(f"Link interface: {link_iface} ...")
# Look for the wireless index
ip = IPRoute()
iw = IW()
index = ip.link_lookup(ifname=iface)[0]
try:
wifi_if = iw.get_interface_by_ifindex(index)
print(f"Wireless interface: {wifi_if}")
with ipdb.interfaces[iface] as if_to_move:
print(f"Moving interface: {if_to_move} to: {truncated_name}")
if_to_move.net_ns_fd = truncated_name
print(f"Done moving to: {truncated_name} ...")
except NetlinkError as e:
if e.code == 19: # 19 'No such device'
print("not a wireless interface")
finally:
iw.close()
ip.close()
错误:
Found USB Wi-Fi interface: wlxc87f548d9633 with details: {17: [{'addr': 'c8:7f:54:8d:96:33', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]}
Truncated name is: 548d963
Link interface: {'address': 'c8:7f:54:8d:96:33', 'broadcast': 'ff:ff:ff:ff:ff:ff', 'ifname': 'wlxc87f548d9633', 'mtu': 1500, 'qdisc': 'noqueue', 'txqlen': 1000, 'operstate': 'DOWN', 'linkmode': 1, 'group': 0, 'promiscuity': 0, 'num_tx_queues': 1, 'num_rx_queues': 1, 'carrier': 0, 'carrier_changes': 1, 'proto_down': 0, 'gso_max_segs': 65535, 'gso_max_size': 65536, 'xdp': {'attrs': [('IFLA_XDP_ATTACHED', None)]}, 'carrier_up_count': 0, 'carrier_down_count': 1, 'min_mtu': 256, 'max_mtu': 2304, 'perm_address': 'c8:7f:54:8d:96:33', 'parent_dev_name': '3-3:1.0', 'parent_dev_bus_name': 'usb', 'index': 10, 'flags': 4099, 'ipdb_scope': 'system', 'ipdb_priority': 0, 'vlans': (), 'ipaddr': (), 'ports': (), 'family': 0, 'ifi_type': 1, 'state': 'up', 'neighbours': ()} ...
Wireless interface: ({'cmd': 7, 'version': 1, 'reserved': 0, 'attrs': [('NL80211_ATTR_IFINDEX', 10), ('NL80211_ATTR_IFNAME', 'wlxc87f548d9633'), ('NL80211_ATTR_WIPHY', 1), ('NL80211_ATTR_IFTYPE', 2), ('NL80211_ATTR_WDEV', 4294967297), ('NL80211_ATTR_MAC', 'c8:7f:54:8d:96:33'), ('NL80211_ATTR_GENERATION', 17), ('NL80211_ATTR_4ADDR', '00'), ('NL80211_ATTR_WIPHY_TX_POWER_LEVEL', 1600), ('NL80211_ATTR_TXQ_STATS', '08:00:01:00:00:00:00:00:08:00:02:00:00:00:00:00:08:00:03:00:00:00:00:00:08:00:04:00:00:00:00:00:08:00:05:00:00:00:00:00:08:00:06:00:00:00:00:00:08:00:08:00:00:00:00:00:08:00:09:00:00:00:00:00:08:00:0a:00:00:00:00:00')], 'header': {'length': 188, 'type': 34, 'flags': 0, 'sequence_number': 255, 'pid': 14840, 'error': None, 'target': 'localhost', 'stats': Stats(qsize=0, delta=0, delay=0)}, 'event': 'NL80211_CMD_NEW_INTERFACE'},)
Moving interface: {'address': 'c8:7f:54:8d:96:33', 'broadcast': 'ff:ff:ff:ff:ff:ff', 'ifname': 'wlxc87f548d9633', 'mtu': 1500, 'qdisc': 'noqueue', 'txqlen': 1000, 'operstate': 'DOWN', 'linkmode': 1, 'group': 0, 'promiscuity': 0, 'num_tx_queues': 1, 'num_rx_queues': 1, 'carrier': 0, 'carrier_changes': 1, 'proto_down': 0, 'gso_max_segs': 65535, 'gso_max_size': 65536, 'xdp': {'attrs': [('IFLA_XDP_ATTACHED', None)]}, 'carrier_up_count': 0, 'carrier_down_count': 1, 'min_mtu': 256, 'max_mtu': 2304, 'perm_address': 'c8:7f:54:8d:96:33', 'parent_dev_name': '3-3:1.0', 'parent_dev_bus_name': 'usb', 'index': 10, 'flags': 4099, 'ipdb_scope': 'system', 'ipdb_priority': 0, 'vlans': (), 'ipaddr': (), 'ports': (), 'family': 0, 'ifi_type': 1, 'state': 'up', 'neighbours': ()} to: 548d963
fail <class 'NoneType'>
fail <class 'NoneType'>
更新了带有建议答案的代码:
#!/usr/bin/python3
# Configure logging
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format="%(asctime)s:%(levelname)s:%(message)s")
# Inspect networking interfaces
import netifaces
# Setup netowrking namespaces
from pyroute2 import NetNS, NSPopen, netns, IPDB, IPRoute
# Get a list of all the network interfaces
interfaces = netifaces.interfaces()
# Managing network interfaces
with IPDB() as ipdb:
# Loop over all the interfaces and logger.info their details
for iface in interfaces:
# Determine interface information
iface_details = netifaces.ifaddresses(iface)
if iface.find("wlxc") >= 0:
logger.info(f"Found USB Wi-Fi interface: {iface} with details: {iface_details}")
# Attempt to remove a networking namespace by the same name
iface_truncate = iface[-4:]
net_namespace = f"{iface_truncate}-ns"
try:
netns.remove(net_namespace)
except Exception as err:
logger.warning(f"Namespace does not exist and we could not remove it: {net_namespace} ...")
# Create a new network namespace or identify an existing one
logger.info(f"Trying to create network namespace: {net_namespace} ...")
netns.create(net_namespace)
# Get the interface index
with IPRoute() as ip:
index = ip.link_lookup(ifname=iface)[0]
logger.info(f"Index of USB WiFi is: {index} ...")
logger.info(f"Interfaces: {ipdb.interfaces[iface]} ...")
# Move the interface to the new network namespace
with ipdb.interfaces[index] as interf:
interf.net_ns_fd = net_namespace
logger.info(f"Moved {iface} to {net_namespace}")
logger.info("Done moving interface ...")
# Close IPDB
ipdb.release()
使用最新代码进行回溯:
2023-11-13 21:05:36,676:WARNING:Deprecation warning https://docs.pyroute2.org/ipdb_toc.html
2023-11-13 21:05:36,676:WARNING:To remove this DeprecationWarning exception, start IPDB(deprecation_warning=False, ...)
2023-11-13 21:05:36,678:INFO:Found USB Wi-Fi interface: wlxc87f548d9633 with details: {17: [{'addr': 'c8:7f:54:8d:96:33', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]}
2023-11-13 21:05:36,680:INFO:Trying to create network namespace: 9633-ns ...
2023-11-13 21:05:36,686:INFO:Index of USB WiFi is: 10 ...
2023-11-13 21:05:36,700:INFO:Interfaces: {'address': 'c8:7f:54:8d:96:33', 'broadcast': 'ff:ff:ff:ff:ff:ff', 'ifname': 'wlxc87f548d9633', 'mtu': 1500, 'qdisc': 'noqueue', 'txqlen': 1000, 'operstate': 'DOWN', 'linkmode': 1, 'group': 0, 'promiscuity': 0, 'num_tx_queues': 1, 'num_rx_queues': 1, 'carrier': 0, 'carrier_changes': 1, 'proto_down': 0, 'gso_max_segs': 65535, 'gso_max_size': 65536, 'xdp': {'attrs': [('IFLA_XDP_ATTACHED', None)]}, 'carrier_up_count': 0, 'carrier_down_count': 1, 'min_mtu': 256, 'max_mtu': 2304, 'perm_address': 'c8:7f:54:8d:96:33', 'parent_dev_name': '3-3:1.0', 'parent_dev_bus_name': 'usb', 'index': 10, 'flags': 4099, 'ipdb_scope': 'system', 'ipdb_priority': 0, 'vlans': (), 'ipaddr': (), 'ports': (), 'family': 0, 'ifi_type': 1, 'state': 'up', 'neighbours': ()} ...
2023-11-13 21:05:36,700:INFO:Moved wlxc87f548d9633 to 9633-ns
fail <class 'NoneType'>
fail <class 'NoneType'>
Traceback (most recent call last):
File "/home/user/Desktop/network-stress-test/network-stress-test.py", line 52, in <module>
with ipdb.interfaces[index] as interf:
File "/usr/local/lib/python3.10/dist-packages/pyroute2/ipdb/transactional.py", line 206, in __exit__
self.commit()
File "/usr/local/lib/python3.10/dist-packages/pyroute2/ipdb/interfaces.py", line 1136, in commit
raise error
File "/usr/local/lib/python3.10/dist-packages/pyroute2/ipdb/interfaces.py", line 1036, in commit
run(nl.link, 'update', **request)
File "/usr/local/lib/python3.10/dist-packages/pyroute2/ipdb/interfaces.py", line 515, in _run
raise error
File "/usr/local/lib/python3.10/dist-packages/pyroute2/ipdb/interfaces.py", line 510, in _run
return cmd(*argv, **kwarg)
File "/usr/local/lib/python3.10/dist-packages/pyroute2/iproute/linux.py", line 1672, in link
ret = self.nlm_request(msg, msg_type=msg_type, msg_flags=msg_flags)
File "/usr/local/lib/python3.10/dist-packages/pyroute2/netlink/nlsocket.py", line 870, in nlm_request
return tuple(self._genlm_request(*argv, **kwarg))
File "/usr/local/lib/python3.10/dist-packages/pyroute2/netlink/nlsocket.py", line 1214, in nlm_request
for msg in self.get(
File "/usr/local/lib/python3.10/dist-packages/pyroute2/netlink/nlsocket.py", line 873, in get
return tuple(self._genlm_get(*argv, **kwarg))
File "/usr/local/lib/python3.10/dist-packages/pyroute2/netlink/nlsocket.py", line 550, in get
raise msg['header']['error']
pyroute2.netlink.exceptions.NetlinkError: (22, 'Invalid argument')
答:
您正在使用 创建一个新的网络命名空间,然后尝试使用上下文管理器将接口移动到新命名空间,这似乎不起作用。
使用 ,假设这会将接口移动到新的命名空间,可能适用于以太网接口。但是,由于WiFi接口与无线PHY(物理层实体)相关联,因此需要不同的处理方式。IPDB(nl=NetNS(truncated_name))
with ipdb.interfaces[iface] as if_to_move:
if_to_move.net_ns_fd = truncated_name
您应该首先使用其 or 索引来识别 WiFi 接口,您已经这样做了(例如,参见“如何使用 pyroute2 IPDB 函数从内核获取接口名称?”,以及 IPRoute 模块)。
然后创建新的网络命名空间或标识现有命名空间。并将 WiFi 接口移动到新的网络命名空间。ifname
from pyroute2 import NetNS, IPDB, IW, IPRoute
# Get a list of all the network interfaces
interfaces = netifaces.interfaces()
# Create a new network namespace or identify an existing one
net_namespace = 'my_new_namespace'
netns.create(net_namespace)
# Managing network interfaces
with IPDB() as ipdb:
for iface in interfaces:
if iface.startswith("wl"):
print(f"Found USB Wi-Fi interface: {iface}")
# Get the interface index
with IPRoute() as ip:
index = ip.link_lookup(ifname=iface)[0]
# Move the interface to the new network namespace
with ipdb.interfaces[iface] as i:
i.net_ns_fd = net_namespace
print(f"Moved {iface} to {net_namespace}")
# Close IPDB
ipdb.release()
这应该将您的 USB WiFi 加密狗移动到指定的网络命名空间中。
[ Updated Setup ]
┌──────────────┐ ┌───────────────────┐
│ USB WiFi ├──────►│ New Namespace │
│ Dongle │ │ (my_new_namespace)│
└──────────────┘ └───────────────────┘
我没有创建新的命名空间作为 IPDB 实例的一部分,而是使用 netns.create(net_namespace)
来显式创建或标识命名空间。这种方法更清晰,将命名空间创建和接口操作的关注点分开。
然后,我用于管理接口并处理要移动的接口(从“将接口移动到命名空间”)。with IPDB() as ipdb:
with ipdb.interfaces[iface] as i:
使用您建议的代码,我从 .
我没有看到任何错误。
之后我没有看到索引的任何用法,我也尝试在那里使用索引。
在尝试将接口移动到命名空间中后,将发生回溯。pyroute2: pyroute2.netlink.exceptions.NetlinkError: (22, 'Invalid argument')
dmesg
ip.link_lookup
更新的代码中的错误表示在将接口移动到新网络命名空间的操作过程中传递了无效参数。NetlinkError: (22, 'Invalid argument')
在您的代码中,您正在尝试使用其索引移动接口。但是,Pyroute2 通常需要接口名称,而不是此类操作的索引。
在将接口移动到命名空间时,尝试使用接口名称而不是索引:with ipdb.interfaces[index] as interf:
with ipdb.interfaces[iface] as interf:
interf.net_ns_fd = net_namespace
那就是:
from pyroute2 import NetNS, IPDB, netns
# Your existing code for setting up logging and getting interfaces
with IPDB() as ipdb:
for iface in interfaces:
if iface.find("wlxc") >= 0:
logger.info(f"Found USB Wi-Fi interface: {iface} with details: {iface_details}")
# Your existing code for handling network namespace
# Move the interface to the new network namespace
try:
with ipdb.interfaces[iface] as interf:
interf.down() # Make sure the interface is down before moving
interf.net_ns_fd = net_namespace
logger.info(f"Moved {iface} to {net_namespace}")
except Exception as e:
logger.error(f"Error moving interface: {e}")
# Your code for further operations
# Your code for closing IPDB
此外,请确保您尝试将接口移动到的命名空间存在且有效:创建命名空间后,请验证其是否存在或更精确地处理与命名空间创建相关的潜在异常。
from pyroute2 import NetNS, IPDB, netns
import logging
# Configure logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format="%(asctime)s:%(levelname)s:%(message)s")
import netifaces
# Get a list of all the network interfaces
interfaces = netifaces.interfaces()
# Managing network interfaces
with IPDB() as ipdb:
for iface in interfaces:
if iface.find("wlxc") >= 0:
iface_details = netifaces.ifaddresses(iface)
logger.info(f"Found USB Wi-Fi interface: {iface} with details: {iface_details}")
iface_truncate = iface[-4:]
net_namespace = f"{iface_truncate}-ns"
# Check if namespace exists, if not, create it
try:
if net_namespace not in netns.listnetns():
logger.info(f"Creating network namespace: {net_namespace} ...")
netns.create(net_namespace)
else:
logger.info(f"Namespace {net_namespace} already exists.")
except Exception as e:
logger.error(f"Error during namespace handling: {e}")
continue # Skip to next interface if there's an error
# Move the interface to the new network namespace
try:
with ipdb.interfaces[iface] as interf:
interf.down() # Ensure the interface is down before moving
interf.net_ns_fd = net_namespace
logger.info(f"Moved {iface} to {net_namespace}")
except Exception as e:
logger.error(f"Error moving interface: {e}")
# Close IPDB
ipdb.release()
在尝试创建新命名空间之前,代码会使用 检查它是否已存在。这样可以防止与尝试创建已存在的命名空间相关的错误。
这包括围绕命名空间操作的 try-except 块,用于处理在创建或验证命名空间期间可能发生的任何异常。还有一些日志记录语句,以便更好地跟踪进程,尤其是关于命名空间操作。netns.listnetns()
评论