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.
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' ?
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
what about:
if [ -c /dev/net/tun ] ; then
echo it exists
else
echo not there
fi
@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:
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
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.
@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
@Freek no problem - you might want to add 'if it still doesn't work, its your providers fault - go shout at them!'