Single command set up for IIIT VPN

All are probably familiar with vpn.iiit.ac.in. It clearly details steps to follow for setting up IIIT VPN which includes going through a series of steps every time you want to connect. So, I made this small procedure to automate it.

  1. Follow first two steps on vpn.iiit.ac.in for Linux. Here are the two steps:
    1. Install openvpn package using yum, apt-get, synaptic, yast etc. based on which Linux distribution you are using. All standard Linux distributions support openVPN installation using standard repositories.
    2. After installation, copy following files to /etc/openvpn/ directory. ca.crtall.iiit.ac.in.crtall.iiit.ac.in and linux_client.conf. (You need to be root to be able to write in this directory).
  2. cd into etc/openvpn/. Create a file called login.conf and add your IIIT email ID in first line and password in second line. For security, make it accessible only to root:
    sudo chown root login.conf
    sudo chgrp root login.conf #Not really needed
    sudo chmod 400 login.conf
    
  3. Open linux_client.conf. Find line which says auth-user-pass(should be around line 126). Replace the line with auth-user-pass login.conf.
  4. Go to home directory and open .bashrc. Copy the following lines at the end of it.
    #This function is helpful if you want to run any function as root.
    function execsudo ()
    {
        ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
        #
        # LOCAL VARIABLES:
        #
        ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
    
        #
        # I use underscores to remember it's been passed
        local _funcname_="$1"
    
        local params=( "$@" )               ## array containing all params passed here
        local tmpfile="/dev/shm/$RANDOM"    ## temporary file
        local filecontent                   ## content of the temporary file
        local regex                         ## regular expression
        local func                          ## function source
    
        ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
        #
        # MAIN CODE:
        #
        ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ##
    
        #
        # WORKING ON PARAMS:
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
        #
        # Shift the first param (which is the name of the function)
        unset params[0]              ## remove first element
        # params=( "${params[@]}" )     ## repack array
    
        #
        # WORKING ON THE TEMPORARY FILE:
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
        content="#!/bin/bash\n\n"
    
        #
        # Write the params array
        content="${content}params=(\n"
    
        regex="\s+"
        for param in "${params[@]}"
        do
            if [[ "$param" =~ $regex ]]
                then
                    content="${content}\t\"${param}\"\n"
                else
                    content="${content}\t${param}\n"
            fi
        done
    
        content="$content)\n"
        echo -e "$content" > "$tmpfile"
    
        #
        # Append the function source
        echo "#$( type "$_funcname_" )" >> "$tmpfile"
    
        #
        # Append the call to the function
        echo -e "\n$_funcname_ \"\${params[@]}\"\n" >> "$tmpfile"
    
        #
        # DONE: EXECUTE THE TEMPORARY FILE WITH SUDO
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        sudo bash "$tmpfile"
        disown 2> /dev/null
        rm "$tmpfile"
    }
    
    function vpnfunc {
    	echo "Starting VPN!!"
    	cd /etc/openvpn
    	openvpn --config linux_client.conf | while read line
    	do
    		[[ "${line}" == *"Initialization Sequence Completed"* ]] && echo 'nameserver 10.4.20.204' | cat - /etc/resolv.conf > temp && mv temp /etc/resolv.conf && echo 'Started VPN successfully!!' && kill -STOP $$
    	done
    }
    
    function startvpn {
    	exec 3>&2          # 3 is now a copy of 2
    	exec 2> /dev/null  # 2 now points to /dev/null
    	execsudo vpnfunc   # run script with redirected stderr
    	exec 2>&3          # restore stderr to saved
    	exec 3>&-          # close saved version
    }
    
    function stopvpn {
    	sudo killall openvpn
    	sudo sed -i '/nameserver 10.4.20.204$/d' /etc/resolv.conf
    }
    
    
  5. Open a new terminal or execute source .bashrc.
  6. From now, startvpn command should start your VPN. (It may ask for sudo password).
  7. stopvpn will terminate VPN.

I am not a bash God, so please spare me if I made any blunders. But, do leave a comment if it can be done better.

 

Credits for execsudo function: stackoverflow answer

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s