Ariel Gluzman
Ariel Gluzman

Reputation: 191

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

I wrote a script that would create and print the name of a new TUN interface on an ubuntu VM using python.

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())

the 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
        ........

this creates a TUN interface and assigns it a name of tunX (X is the available number for a new name of the interface) after running this I usually get an answer of tun0. Then, I print out the output of 'ifconfig' (I also tried it manually) and and I cannot see that tun0.

Can someone explain to me if I am truly creating that TUN device and if so what happens to it after the script ends, I am also completely interested in theoretic material on it if someone's got any.

Thank you.

Upvotes: 2

Views: 851

Answers (1)

Ramadhan Gunia
Ramadhan Gunia

Reputation: 21

Your script is very okay... The reason why you cannot see the tub interface when the script has finished running is because, the script is not looping...your turn interface will show so long as the server(script) is active,after that it dies

Upvotes: 2

Related Questions