簡體   English   中英

在 Ubuntu VM 上創建 TUN 設備似乎不起作用

[英]Creating a TUN device on Ubuntu VM doesn't seem to work

我編寫了一個腳本,該腳本將使用 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())

output

 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'的output(我也手動嘗試過),但我看不到那個tun0。

有人可以向我解釋我是否真的在創建那個 TUN 設備,如果是這樣,腳本結束后它會發生什么,如果有人有的話,我也對它的理論材料完全感興趣。

謝謝你。

您的腳本非常好...腳本運行完成后看不到浴缸界面的原因是因為腳本沒有循環...只要服務器(腳本)處於活動狀態,您的轉界面就會顯示,之后它死了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM