Script for connecting to openVPN and updating nameserver appropriately

From Notes_Wiki

Home > Shell scripting > Useful bash shell scripts > Script for connecting to openVPN and updating nameserver appropriately

Home > CentOS > CentOS 6.x > Openvpn server configuration > Script for connecting to openVPN and updating nameserver appropriately

Following script can be used for connecting to openVPN and updating nameserver appropriately:

#!/bin/bash

sudo /sbin/service openvpn start
echo "Waiting for connection establishment to complete"
STATUS=$(ifconfig | grep '10\.7\.1\.1')
while [[ "$STATUS" = "" ]]; do
	echo -n ".";
	sleep 1
	STATUS=$(ifconfig | grep '10\.7\.1\.1')
done
echo "Connection successful"
echo "Going to replace nameserver"
sudo mv /etc/resolv.conf /etc/resolv.conf.backup
echo "nameserver 10.4.3.222" > /tmp/resolv.conf
sudo mv /tmp/resolv.conf /etc/resolv.conf
echo "Nameserver replaced"
echo "Press enter to disconnect..."
read A
sudo /sbin/service openvpn stop
echo "Going to restore nameserver"
sudo mv /etc/resolv.conf.backup /etc/resolv.conf
echo "Nameserver restored"

exit 0

Note:

  • The script assumes that after successful connection client would get IP 10.7.1.1. Replace the "grep '10\.7\.1\.1'" line with appropriate condition such as "ifconfig | grep -A 1 tun0 | grep 'inet addr'" for the case.
  • The script assumes nameserver to be configured is 10.4.3.222. This should be replaced with correct nameserver based on situation.



Home > Shell scripting > Useful bash shell scripts > Script for connecting to openVPN and updating nameserver appropriately

Home > CentOS > CentOS 6.x > Openvpn server configuration > Script for connecting to openVPN and updating nameserver appropriately