Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #
- # Quick info:
- # - script that is running OpenVPN client in a loop, breaking out of it when
- # the "Network is unreachable" message comes up
- # - after each client termination it wipes out left tunnel interfaces (tap0, tap1, .., tapN)
- # - also it kills all of the running OpenVPN clients, preventing consecutive logons.
- #
- # Some Background:
- # Simple script made when I had been suffering from unstable link/connection
- # because of my ISP which made my running in the background daemons and scanners become
- # unstable after dealing with continuous connection closing.
- # I figured out that a simple re-running of the OpenVPN client from a pristine environment
- # will be sort of a solution. And it actually worked.
- #
- VPN_CONFIGURATION=/etc/openvpn/default.conf
- VPN_USERNAME=<username>
- VPN_PASSWORD=<password>
- cat << EOF > /tmp/expect
- #!/usr/bin/expect -f
- match_max 1000000
- set timeout -1
- spawn openvpn $VPN_CONFIGURATION
- expect "*?sername:*"
- send -- "$VPN_USERNAME"
- send -- "\r"
- expect "*?assword:*"
- send -- "$VPN_PASSWORD"
- send -- "\r"
- # Awaiting for link to be become closed.
- expect "*Network is unreachable*"
- close
- EOF
- i=0
- while true; do
- i=$((i+1))
- echo -ne "\n[+] Starting another instance of OpenVPN client ($i)\n"
- # This is needed to prevent concurrent VPN connections in the background.
- killall -9 openvpn @> /dev/null
- sleep 2
- B=$IFS
- IFS=$'\n'
- # Removing other tunnel interfaces to make the OpenVPN client stick to the "tap0".
- # We want this in order to make daemons (and programs) running in the background which
- # already sticked to the "tap0" interface not become unstable.
- for iface in $(find /sys/class/net -name "tap*" -exec basename {} \;); do
- sudo ip link delete $iface
- done
- IFS=$B
- sleep 2
- # Spawning the client.
- /usr/bin/expect -f /tmp/expect
- # Awaiting for user interruption (Ctrl+C), which will break out of the loop
- test $? -gt 128 && break
- done
- echo "Script has fininshed."
- killall -9 openvpn @> /dev/null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement