Advertisement
Guest User

Untitled

a guest
Apr 11th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. #! /usr/local/bin/bash
  2.  
  3. #
  4. # Script for logging into the VPN without having to use the UI client and go through
  5. # the repetition of entering in host, username, and password.
  6. #
  7. # Requires:
  8. # Bash 4.x
  9. #
  10. # Author: Matt Skinner
  11. # Isaac M
  12. #
  13. # Note: rename to vpn or some other name to make it easier to run on a regular
  14. # basis.
  15.  
  16. declare -A config
  17. # default values to use if environment variables are not defined.
  18.  
  19. #config["user_name"]=""
  20. #config["password"]=""
  21. #config["host"]=""
  22.  
  23. #Path to the VPN commandline interface
  24. vpn_cmd="/opt/cisco/anyconnect/bin/vpn -s"
  25.  
  26. #compute defaults
  27. if [ -z "$VPN_USER_NAME" ]
  28. then
  29. user_name=${config["user_name"]}
  30. else
  31. user_name=$VPN_USER_NAME
  32. fi
  33.  
  34. if [ -z "$VPN_PASSWORD" ]
  35. then
  36. password=${config["password"]}
  37. else
  38. password=$VPN_PASSWORD
  39. fi
  40.  
  41. if [ -z "$VPN_HOST" ]
  42. then
  43. host=${config["host"]}
  44. else
  45. host=$VPN_HOST
  46. fi
  47.  
  48. function print_user_config_error {
  49. echo "Unable to continue because username and/or password is not configured properly. Either define an environment variable (VPN_USER_NAME, VPN_PASSWORD) or modify this script located at $0."
  50. exit 1
  51. }
  52.  
  53. function print_host_config_error {
  54. echo "Unable to continue because the VPN host is not configured properly. Either define an environment variable (VPN_HOST) or modify this script located at $0."
  55. exit 1
  56. }
  57.  
  58. if [[ -z "$user_name" || -z "$password" ]]
  59. then
  60. print_user_config_error
  61. fi
  62.  
  63. if [ -z "$host" ]
  64. then
  65. print_host_config_error
  66. fi
  67.  
  68. function determine_connection_status() {
  69.  
  70. disconnected=$(/usr/bin/expect<<EOF
  71. log_user 0
  72.  
  73. # uncomment the line below to show debug info for expect command
  74. # exp_internal 1
  75. spawn /opt/cisco/anyconnect/bin/vpn
  76.  
  77. expect -re "VPN> $"
  78. send "state\r\n"
  79.  
  80. expect {
  81. -re "Disconnected\r\n\rVPN> $" {
  82. send_user "\r \033\[31mNot connected to VPN.\033\[0m\n";
  83. exit 1
  84. }
  85. -re "Connected\r\n\rVPN> $" {
  86. send_user "\r \033\[32mConnected to VPN.\033\[0m\n";
  87. exit 0
  88. }
  89. default {
  90. send_user "\r******* Get anyconnect status failed. Quitting ...\n";
  91. exit
  92. }
  93. }
  94. EOF
  95. )
  96.  
  97. ret_code="$?"
  98. # echo "exit status = $ret_code"
  99. echo "$disconnected"
  100.  
  101. if [ "$ret_code" -gt 0 ]
  102. then
  103. disconnected=true
  104. else
  105. disconnected=false
  106. fi
  107. }
  108.  
  109. function connect() {
  110. export host
  111. export user_name
  112. export password
  113.  
  114. /usr/bin/expect<<EOF
  115. log_user 0
  116. spawn /opt/cisco/anyconnect/bin/vpn
  117.  
  118. expect -re "VPN> $"
  119. send "connect $::env(host)\n"
  120.  
  121. expect {
  122. -re "Username:" { send "$::env(user_name)\n" }
  123. -re "Another AnyConnect application is running" { send_user "\r \033\[31mAnother AnyConnect is running. Quitting...\033\[0m\n";exit 1 }
  124. }
  125.  
  126. expect -re "Password:"
  127. send "$::env(password)\ry"
  128.  
  129. expect {
  130. -re "state: Disconnected" { send_user "\r \033\[31mFailed connecting to $::env(host).\033\[0m\n";exit 1 }
  131. -re "state: Connected" { send_user "\r \033\[32mConnected to $::env(host).\033\[0m\n";exit 0 }
  132. default { send_user "\r ******* Quitting ...\n";exit }
  133. }
  134. EOF
  135. }
  136.  
  137. function disconnect() {
  138. echo -e "disconnect" | $vpn_cmd
  139. }
  140.  
  141. function start() {
  142. if [ $disconnected = true ]
  143. then
  144. connect
  145. else
  146. echo "Cannot connect. Already connected."
  147. fi
  148. }
  149.  
  150. function stop() {
  151. if [ $disconnected = true ]
  152. then
  153. echo "Already disconnected."
  154. else
  155. disconnect
  156. fi
  157. }
  158.  
  159. function print_usage() {
  160. local me
  161. me=$(basename "$0")
  162. echo "USAGE: $me start | stop"
  163. exit 1
  164. }
  165.  
  166. determine_connection_status
  167.  
  168. if [ "$1" == "start" ]
  169. then
  170. start
  171. exit 0
  172. elif [ "$1" == "stop" ]
  173. then
  174. stop
  175. exit 0
  176. else
  177. print_usage
  178. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement