【问题标题】:Creating a TUN device on Ubuntu VM doesn't seem to work在 Ubuntu VM 上创建 TUN 设备似乎不起作用
【发布时间】: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


    【解决方案1】:

    你的脚本很好...脚本运行完成后看不到tub界面的原因是因为脚本没有循环...只要服务器(脚本)就会显示轮回界面是活跃的,之后它就死了

    【讨论】:

    • 谢谢,我现在明白了
    猜你喜欢
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 2018-10-26
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多