【发布时间】:2020-12-25 20:22:17
【问题描述】:
我编写了一个脚本,该脚本将使用 python 在 ubuntu VM 上创建和打印新 TUN 接口的名称。
import fcntl
import struct
import os
import subprocess
#from scapy.all import *
TUNSETIFF = 0x400454ca
IFF_TUN = 0x0001
IFF_TAP = 0x0002
IFF_NO_PI = 0x1000
tun = os.open("/dev/net/tun", os.O_RDWR)
ifr = struct.pack('16sH', b'tun%d', IFF_TUN | IFF_NO_PI)
ifname_bytes = fcntl.ioctl(tun, TUNSETIFF, ifr)
ifname = ifname_bytes.decode('UTF-8')[:16].strip('\x00')
print("Interface Name: {}".format(ifname))
proc = subprocess.Popen(['ifconfig'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
stdout, stdin = proc.communicate()
print(stdout.decode())
输出
Interface Name: tun0 enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.204 ...... lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 ........
这将创建一个 TUN 接口并为其分配一个名称 tunX(X 是该接口新名称的可用编号) 运行此之后,我通常会得到 tun0 的答案。 然后,我打印出 'ifconfig' 的输出(我也手动尝试过),但我看不到那个 tun0。
如果我真的在创建那个 TUN 设备,有人可以向我解释一下,如果是的话,脚本结束后它会发生什么,如果有人有的话,我也对它的理论材料完全感兴趣。
谢谢。
【问题讨论】:
标签: python python-3.x linux-kernel ioctl tun