Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Bash script to check if tun/tap is enabled and if so do 'stuff' ?
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Bash script to check if tun/tap is enabled and if so do 'stuff' ?

FreekFreek Member
edited May 2013 in General

I'm trying to write a bash script that detects/check if tun/tap has been enabled and if so, it'll do 'stuff'. I'm having issues getting this to work properly. William helped me out a bit on IRC, but I still can't get it to work.
Here's what I have so far, it's pretty basic:

#!/bin/bash
arg=$(ls /dev/net/tun >>/dev/null; echo $?)
if [ "$arg" == "2" ]
then echo it's not there
else echo it's there
fi

The idea is that if 'ls /dev/....' exists with code 2, it means tun/tap is disabled and if it exits with 0 it means it's enabled.
If I run this in a bash script, it doesn't seem to be outputting the 2 anymore, nor doing the else.... I can't see what's wrong...

Thanks

Comments

  • rds100rds100 Member

    what about:

    if [ -c /dev/net/tun ] ; then
    echo it exists
    else
    echo not there
    fi

  • FreekFreek Member

    @rds100 Thanks, that did the trick!
    Now I'm trying to make this check nested, but doing so, it will not execute both checks.
    Here's what I have so far:

    #Check if TUN/TAP is(properly) enabled on OpenVZ before continuing
    echo
    echo "############################################"
    echo "Checking if TUN/TAP is properly enabled on OpenVZ...."
    echo "############################################"
    case lscpu in
    VT-x )
        if [ -c /dev/net/tun ] ; 
        then echo TUN/TAP is ENABLED
        else
            mkdir -p /dev/net
            mknod /dev/net/tun c 10 200
            chmod 600 /dev/net/tun
                if [ -c /dev/net/tun ] ; then
                    echo TUN/TAP was not setup properly, but now it is. Yay
                else
                    echo TUN/TAP looks DISABLED.
                    echo Please check in SolusVM under 'Settings' if TUN/TAP is Enabled.
                    echo Quitting....
                    exit
                fi
        fi;;
    *) echo ;;
    esac
    

    The idea behind it is that sometimes TUN/TAP does not get setup properly even if enabled in SolusVM. To fix this, one has to execute the steps described here:
    http://wiki.vpslink.com/TUN/TAP_device_with_OpenVPN_or_Hamachi
    However, I want to integrate this in my bash script as well.
    So if the first check fails, it'll try to fix the tun/tap device and check again if it's fixed now.
    If so, it'll continue, else it'll quit with an error.

    Is my nesting wrong or am I making an thinking error here?

    Thanks :)

  • tehdantehdan Member

    I don't think you can assume tun/tap is working because of the existence of a device node. I've seen more than one provider not install the tun/tap modules on the host. In this situation all of your tests will say its working, but it won't.

    Its highly unlikely that the mknod will fail, but it gives you no guarantee that the device node will function - your nesting is structurally okay but for more reliability you should replace the tests it with something that checks the function and not just the existence of tun/tap.

    I suspect with a bit of studying the man page for OpenVPN you can get it to test this one way or another.

  • FreekFreek Member
    edited May 2013

    @tehdan Thanks, I appreciate the warning, altough the 'cat /dev/net/tun' check is sufficient for me, better than nothing. It's just to make things a bit easier :) It's supposed to be part of a larger script to automatically setup OpenVPN :)

  • tehdantehdan Member

    @Freek no problem - you might want to add 'if it still doesn't work, its your providers fault - go shout at them!'

Sign In or Register to comment.