Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.40 KB | None | 0 0
  1. #!/bin/bash
  2. ### TO DO ###
  3. #Split server list into arrays.
  4. #Load array list into this script.
  5. #Change $serverinput to search through arrays#
  6. #############
  7. #### Patches ####
  8. #I started actually logging these today. 2017/5/30
  9. #v1.1 2017/5/30 Added logic to allow users to choose "None" out of server selection and if the input resolves a cpanel server but is not an EXACT match for one of the servers in the list. SSH into the add server lines.
  10. #v.1.2 2017/6/2 Added logic to qualify input before sending to the add server function on certain lines. Also changed serverchoice logic to not exit improperly on choice, (Bad else catchall, changed to elif with a condition)
  11. #v.1.3 2017/6/2 Added a line at the very start of the logic to determine if input is an exact match to a line in server list and then just go to it.
  12. #v.1.4 2017/6/9 Added a -a flag, which will directly add your input into the server list before running any checks.
  13. ## Add colors as variables ##
  14. Red='\e[31m'
  15. Green='\e[32m'
  16. Default='\e[39m'
  17. Blue='\e[34m'
  18. Yellow='\e[93m'
  19. ############################
  20.  
  21. ##### Usgae ################
  22. if [ $# -eq 0 ]; then # If no input is enterted, prunt usage.
  23. echo -e ""$Red"Usage s: Searchterm|IP|Domain [-a] [-p] port number [-g] group \n -a, --directly add your input into the server list.\n -p, --specify port to connect on.\n -g, --specify ssh group to connect with."$Default""
  24. exit 0
  25. fi
  26. ############################
  27. #### Flag Specification ####
  28.  
  29. OPTIND=2 #Start looking at user input after the first argument (allows it to skip the argument entered for connecting)
  30. while getopts "dap:g:" flag
  31. do
  32. case $flag in
  33. p) port="-p $OPTARG" # If user enters -p, set -p to the $port variable for later use.
  34. echo -e ""$Yellow"Port set to $port"$Default""
  35. ;;
  36. g) group=$OPTARG # If user enters -p, set -g to the $group variable for later use.
  37. echo -e ""$Yellow"Group set to $group"$Default""
  38. ;;
  39. a) echo -e ""$Yellow" Starting add sequence, be careful with this flag, it does very little error checking and is mostly meant for quickly adding servers you entered exactly right and you know are valid. "$Default""
  40. sleep 2
  41. if [[ -n $(grep -i ^$1$ ~/SERVERS.txt) ]] ;then
  42. echo -e ""$Red"Server already added, not adding again."$Default""
  43. elif [[ -z $(dig +short $1) ]] ;then
  44. echo -e ""$Red"This doesn't resolve. "$Blue"Do you still want to add it?"$Default""
  45. select answer in Yes No
  46. do
  47. if [[ $answer == "Yes" ]] ;then
  48. echo -e ""$Yellow"Adding server. "$Default""
  49. echo $1 >> ~/SERVERS.txt
  50. elif [[ $answer == "No" ]] ;then
  51. echo -e ""$Yellow"Not adding, proceeding as normal..."$Default""
  52. else
  53. echo -e ""$Red"Not sure what you want, Exiting..."$Default""
  54. fi
  55. break
  56. done
  57. else
  58. echo -e ""$Yellow"Adding server to list."$Default""
  59. echo $1 >> ~/SERVERS.txt
  60. fi
  61. ;;
  62. d) echo -e ""$Yellow" No lookups, just going to ssh."$Default""
  63. ssh $group $1 $port
  64. exit
  65. ;;
  66. esac
  67. done
  68. ############################
  69.  
  70. ### Variables ####
  71. servername=$(curl -s $1:2087 |grep -o -P '(?<=https\:\/\/).*(?=\:2087)')
  72. serverinput=$(grep -i $1 ~/SERVERS.txt |sort -u)
  73. servercount=$(grep -i $1 ~/SERVERS.txt |sort -u | wc -l)
  74. hostlookup=$(dig +short $1)
  75. ##################
  76.  
  77. #### Function to add the server found with curl's if user wants. ####
  78. addserver ()
  79. {
  80. curservcount=$(wc -l < ~/SERVERS.txt) # Saves current line count of server file.
  81. if [[ -n $(grep -i "^$servername$" ~/SERVERS.txt) ]] ;then
  82. echo -e ""$Green""Found: $servername" in list. Resolves to $hostlookup. \nConnecting ... "$Default""
  83. ssh $group $servername $port
  84. exit 0
  85. elif [[ -n $servername ]]; then
  86. echo -e ""$Green"Host lookup found: $servername"$Default"\n"$Blue"Do you want me to add this to serverlist?"$Default"" # Echos serverlookup result and asks if user wants to save it in the server list.
  87. select response in Yes No # Prompts user with Yes or No and records their answer
  88. do
  89. if [[ "$response" == "Yes" ]] ;then # If their answer is yes.
  90. echo -e ""$Yellow" Copying current server list to a backup file"$Default""
  91. cp -aiv ~/SERVERS.txt{,add-$(date +%m.%d.%Y_%H-%M)} # Copy a backup of the file to ensure we dont loose it.
  92. echo "$servername" >> ~/SERVERS.txt # Add the server lookup result to the file.
  93. if [[ -n $(grep -i ^$servername$ ~/SERVERS.txt) ]] ;then # If the server lookup result is now in the file, let the user know that and then proceed to the next line after the function call.
  94. echo -e ""$Green"Added properly, continuing"$Defualt""
  95. else
  96. echo -e ""$Red"Did not add correctly, try manually."$Default"" >&2 # If it is not added, let the user know, then proceed to the next line after the function call.
  97. exit 1
  98. fi # End add check if block.
  99. if [[ $(( $curservcount + 1 == $(wc -l < ~/SERVERS.txt) )) ]]; then # If the current server list count is 1 higher than it was before we started, Remove the backup file.
  100. echo -e ""$Yellow" Removing backup file."$Default""
  101. /bin/rm -v ~/SERVERS.txtadd-$(date +%m.%d.%Y_%H-%M)
  102. else
  103. echo -e ""$Red"Something went wrong with the add, backup copy of the file was saved as ~/SERVERS.txtadd-$(date +%m.%d.%Y_%H-%M) "$Default"" >&2 # If it is not, let the user know and do not remove backup file.
  104. exit 1
  105. fi # End the file check if block.
  106. elif [[ "$response" == "No" ]] ;then # If the user choose not to add the server lookup result, then proceed to the next line after the function call.
  107. echo -e ""$Yellow"Ok, Not adding"$Default""
  108. else
  109. echo -e ""$Red"Not sure what you mean. Exiting..."$Default"" >&2 # If niether choice is matches, exit entirely.
  110. exit 1
  111. fi # End the Selection if block.
  112. break # Stop selection from looping.
  113. done # End selection loop.
  114. fi
  115. } # End function definition.
  116.  
  117. ######################################################################
  118.  
  119. #### Where the work gets done ####
  120. if [[ -n "$hostreturn" ]] ;then
  121. ssh $group $hostreturn $port
  122. fi
  123. if [[ -n $(grep -i ^$1$ ~/SERVERS.txt) ]] ;then
  124. echo -e ""$Green"Found "$1" \nConnecting ... "$Default"";
  125. ssh $group $1 $port; #go server
  126. exit 0
  127. fi
  128. if [[ -n "$serverinput" ]] ; then # If server search returns a string
  129. echo -e ""$Green"Searching..."$Default""
  130. if [[ "$servercount" -gt 10 ]] ;then # If return is greater than 5 lines
  131. echo -e ""$Red"Search term too ambigious, try refining your search"$Default"" >&2
  132. echo -e ""$Red"Found the following servers matching this search term"$Default":\n$serverinput" >&2
  133. exit 1
  134.  
  135. elif [[ "$servercount" -eq 1 ]] ; then #If result returns 1 result
  136. echo -e ""$Green"Found $serverinput\nConnecting ... "$Default"";
  137. ssh $group $serverinput $port; #ssh server
  138. exit 0
  139.  
  140. elif [[ "$servercount" -lt 10 ]] && [[ "$servercount" -gt 1 ]]; then # If result returns less than 5 but more than 1
  141.  
  142. echo -e ""$Yellow" Found multiple servers. Choose from below list."$Default""
  143. select selection in $serverinput None # Prompt the user with the choices we have from the serverinput variable and record answer.
  144. do
  145. serverchoice=$selection # Specify a variable for user selection
  146. echo -e ""$Blue"You chose $serverchoice"$Default""
  147. if [[ $selection == "None" ]] && [[ -n "$servername" ]] ;then
  148. echo -e ""$Yellow"Attempting to connect to server..."$Default""
  149. addserver
  150. ssh $group $servername $port
  151. exit 0
  152. elif [[ $selection == "None" ]] && [[ -z "$servername" ]] ;then
  153. echo -e ""$Red"No server found or choose, Exiting ..."$Default"" >&2
  154. exit 1
  155. elif [[ $selection != "None" ]] ;then
  156. ssh $group $serverchoice $port # ssh to input (This is after completing search, therefore result is a full hostname, no need to iterate through search)
  157. break # Stop it from looping
  158. fi
  159. done # End selection loop
  160. exit 0 # Exit with success
  161. fi # End Selection block.
  162.  
  163. if [[ -n "$serverinput" ]] && [[ -z $(grep -i "^$servername$" ~/SERVERS.txt) ]]; then
  164. addserver
  165. ssh $group $servername $port
  166. fi
  167.  
  168. elif [[ $1 =~ ^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9]\.[a-zA-Z]{2,}$ ]] ;then
  169. if [[ -n $servername ]] ;then
  170.  
  171. echo -e ""$Yellow" Server: $servername "$Default"" #curl input to find hostname, and echo before connecting
  172. addserver
  173. echo -e ""$Green"Connecting to $hostlookup ..."$Default"" #Dig input and determine IP and echo beforing connecting
  174. ssh $group $1 $port; # ssh to the server
  175. exit 0 # Exit success
  176. elif [[ -n $hostlookup ]] && [[ -z $servername ]] ;then
  177. echo -e ""$Yellow"Domain resolves to $hostlookup , but I am unable to connect to cPanel"$Default""
  178. ssh $group $1 $port
  179. else
  180. echo -e ""$Red"Domain does not resolve, or cannot find applicable server."$Default"" >&2
  181. echo -e ""$Yellow" Attempt to ssh anyway?"$Default""
  182. select selection in Yes No
  183. do
  184. if [[ $selection == "Yes" ]] ;then
  185. ssh $group $1 $port
  186. else
  187. echo -e ""$Yellow" Exiting ..."$Default""
  188. exit 0
  189. fi
  190. break
  191. done
  192. fi
  193. elif [[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]] ;then #If input is an IP.
  194.  
  195. if [[ -n $servername ]] && [[ -n $(grep -i "^$servername$" ~/SERVERS.txt) ]] ;then
  196. echo -e ""$Green"Server: $servername is in list\n"$Green"Connecting... "$Default""
  197. ssh $group $1 $port # If the search resulted in no hits and the search term was not a domain, curl input to find server hostname, and echo before connecting
  198. elif [[ -n $servername ]] && ! [[ -n $(grep -i "^$servername$" ~/SERVERS.txt) ]] ;then
  199. addserver
  200. echo -e ""$Green"Connecting to $servername..."$Default"" #Dig input and determine IP and echo beforing connecting
  201. ssh $group $1 $port
  202. else
  203. echo -e ""$Red"No cPanel server found, attempting to connect anyway."$Default"" >&2
  204. ssh $group $1 $port
  205. fi
  206.  
  207. else
  208. if [[ -n $servername ]] ;
  209. then
  210. addserver
  211. ssh $group $1 $port
  212. else
  213. echo -e ""$Red"Server lookup failed, server curl failed, domain does not resolve. Attempting to connect anyway.\n"$Green"Connecting ..."$Default""
  214. ssh $group $1 $port; #ssh input
  215. exit # exit success
  216. fi
  217. fi # End search block
  218.  
  219. ##################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement