Advertisement
Guest User

Untitled

a guest
Aug 21st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Quick info:
  5. # - script that is running OpenVPN client in a loop, breaking out of it when
  6. # the "Network is unreachable" message comes up
  7. # - after each client termination it wipes out left tunnel interfaces (tap0, tap1, .., tapN)
  8. # - also it kills all of the running OpenVPN clients, preventing consecutive logons.
  9. #
  10. # Some Background:
  11. # Simple script made when I had been suffering from unstable link/connection
  12. # because of my ISP which made my running in the background daemons and scanners become
  13. # unstable after dealing with continuous connection closing.
  14. # I figured out that a simple re-running of the OpenVPN client from a pristine environment
  15. # will be sort of a solution. And it actually worked.
  16. #
  17.  
  18. VPN_CONFIGURATION=/etc/openvpn/default.conf
  19. VPN_USERNAME=<username>
  20. VPN_PASSWORD=<password>
  21.  
  22. cat << EOF > /tmp/expect
  23. #!/usr/bin/expect -f
  24. match_max 1000000
  25. set timeout -1
  26.  
  27. spawn openvpn $VPN_CONFIGURATION
  28. expect "*?sername:*"
  29. send -- "$VPN_USERNAME"
  30. send -- "\r"
  31.  
  32. expect "*?assword:*"
  33. send -- "$VPN_PASSWORD"
  34. send -- "\r"
  35.  
  36. # Awaiting for link to be become closed.
  37. expect "*Network is unreachable*"
  38. close
  39. EOF
  40.  
  41. i=0
  42. while true; do
  43. i=$((i+1))
  44. echo -ne "\n[+] Starting another instance of OpenVPN client ($i)\n"
  45.  
  46. # This is needed to prevent concurrent VPN connections in the background.
  47. killall -9 openvpn @> /dev/null
  48. sleep 2
  49.  
  50. B=$IFS
  51. IFS=$'\n'
  52.  
  53. # Removing other tunnel interfaces to make the OpenVPN client stick to the "tap0".
  54. # We want this in order to make daemons (and programs) running in the background which
  55. # already sticked to the "tap0" interface not become unstable.
  56. for iface in $(find /sys/class/net -name "tap*" -exec basename {} \;); do
  57. sudo ip link delete $iface
  58. done
  59.  
  60. IFS=$B
  61. sleep 2
  62.  
  63. # Spawning the client.
  64. /usr/bin/expect -f /tmp/expect
  65.  
  66. # Awaiting for user interruption (Ctrl+C), which will break out of the loop
  67. test $? -gt 128 && break
  68. done
  69.  
  70. echo "Script has fininshed."
  71. killall -9 openvpn @> /dev/null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement