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

Freek

New Member
Since LET is broken beyond repairs, I'm hoping someone can help me out here with the OpenVPN script I am trying to expand. Basically I took this bash script: http://www.putdispenserhere.com/openvpn-debianubuntu-setup-script-for-openvz/ and expanded it with some features.

What I am trying to do is detecting of tun/tap is (properly) enabled on OpenVZ and if not, it'll try to fix it.
Here's what I have:


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

The problem is the nested check is not being executed. Is my nesting wrong or am I making an thinking error here?

Thanks :)
 

acd

New Member
Your first link doesn't work.

First, your case check is wrong. the string "lscpu" is never going to result in VT-x. Perhaps you meant the following?


case `lscpu` in
which still isn't really right, because case expects a full string match unless you're using wildcards, like so:


case `lscpu` in
*VT-x* ) echo "vtx is enabled"
;;
* ) echo "no vtx :("
;;
esac
but that leads us to the following. On some ovzs, lscpu doesn't exactly work:


[email protected]:~$ lscpu
lscpu: error: /sys filesystem is not accessable.
I believe this does what you actually want.

Code:
#!/bin/bash
if [ "$(whoami)" != "root" ] ; then
echo "You must be root to execute this script."
exit 1
fi

if [ ! -c /dev/net/tun ] ; then
  rm -f /dev/net/tun
  mkdir -p /dev/net
  mknod /dev/net/tun c 10 200
  chmod 600 /dev/net/tun
  if [ "$?" -ne 0 ] ; then
    echo "Error creating tun device file"
    exit 1
  fi
fi

case `cat /dev/net/tun 2>&1` in
*"File descriptor in bad state" )
  echo "TUN device is ready." ;;
* )
  echo "TUN device looks disabled." ;;
esac
 
Top