sssh.sh 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #!/bin/bash
  2. # ##########################################################
  3. #
  4. # Super SSH Script
  5. #
  6. # Version 9.1 [11.2017]
  7. #
  8. # V9.2 Rewrite project menu output, change from echo to printf
  9. # V9.1 Project overview now accept project names as input [08.11.2017]
  10. # V9. [06.10.2017]
  11. # V8. Added group function again [08.2017]
  12. # V7. Rewrite script - now with functions [06.2016]
  13. # V6. Added function to pass parameters to server menu [05.2012]
  14. # V5. Added function to choose by hostname [05.2012]
  15. # V4. Added group function [11.2011]
  16. # V3. Added avaibility function
  17. # V2. color output
  18. # V1. Create script
  19. #### config ####
  20. ## Wenn true wird jeder Host 1x angepingt und der Status ausgegeben
  21. PINGHOST="true" # gueltige Werte [true,false]
  22. #### do not change ####
  23. ## used to kill from subshell
  24. MAINPID=$$
  25. ## locationOfScript
  26. locationOfScript=$(dirname "$(readlink -e "$0")")
  27. #=== FUNCTION ================================================================
  28. # NAME: get_projects
  29. # DESCRIPTION: find all projects
  30. # PARAMETERS:
  31. # RETURNS:
  32. #===============================================================================
  33. function get_projects() {
  34. for project in $(cat $locationOfScript/projects.txt | grep -ve "^#" -e "^$" | cut -d ":" -f "1" | awk '!a[$0]++'); do
  35. projects=$projects:$(echo $project)
  36. project_count=$[project_count+1]
  37. done
  38. ## remove first :
  39. projects=$(echo $projects | /bin/sed 's/://')
  40. }
  41. #=== FUNCTION ================================================================
  42. # NAME: get_groups
  43. # DESCRIPTION: find all groups of given project
  44. # PARAMETERS: project_name
  45. # RETURNS:
  46. #===============================================================================
  47. function get_groups() {
  48. ## reset groups/groupscount (neccessary for project reload)
  49. groups=""
  50. groups_count=0
  51. ## be carfully: $GROUPS is an BASH internal variable
  52. for group in $(cat $locationOfScript/projects.txt | grep -ve "^#" -e "^$" | grep "$1" | cut -d ":" -f "5" | awk '!a[$0]++'); do
  53. groups=$groups:$(echo $group)
  54. groups_count=$[groups_count+1]
  55. done
  56. ## remove first :
  57. groups=$(echo $groups | /bin/sed 's/://')
  58. # x y z sind mit sonderfunktionen belegt!
  59. arr_character=( a b c d e f g h i j k l m n o p q r s t u v w )
  60. for (( i=0;i<$groups_count;i++)); do
  61. # zur späteren Kontrolle schreiben wir alle BENUTZTEN Buchstaben in ein Array
  62. arr_character["$i"]="${arr_character[${i}]}"
  63. # Ausgabe der Gruppen in einer Zeile
  64. echo -en "[\033[1;33m"${arr_character[${i}]}"\033[0m]\033[1;32m"$( echo $groups | cut -d ":" -f "$(($i+1))" )"\033[0m "
  65. done
  66. }
  67. #=== FUNCTION ================================================================
  68. # NAME: print_projectmenu
  69. # DESCRIPTION: print out projectmenu for given project
  70. # PARAMETERS: project,group
  71. # RETURNS:
  72. #===============================================================================
  73. function print_projectmenu() {
  74. ## reset variables
  75. GROUP_FILTER=
  76. ## check if given projectname or projectnumber
  77. if [[ "$1" == ?(-)+([0-9]) ]]; then
  78. ## projectnumber, read projectname from "projects" string
  79. project=$( echo $projects | cut -d ":" -f "$1" )
  80. else
  81. ## projectname ...
  82. project=$1
  83. fi
  84. ## group filter given
  85. if [ -n "$2" ]; then
  86. for i in "${!arr_character[@]}"; do
  87. if [ "${arr_character[$i]}" == "$2" ]; then
  88. GROUP_FILTER=$( echo $groups | cut -d ":" -f $(($i+1)) )
  89. fi
  90. done
  91. fi
  92. clear
  93. echo -e " == SSH Sprungmenu ==\n\n Projekt: $project\n"
  94. ## just read out all groups
  95. get_groups $project
  96. # Ausgabe des Headers
  97. if [ "$PINGHOST" == "true" ]; then
  98. printf "\n\033[1;33m%3s %-11s %-20s %-12s %-13s %s\033[0m\n" "ID" "USER" "Host" "[Status]" "Gruppe" "Beschreibung"
  99. else
  100. echo -e "\033[1;33mID USER\t Host\tGruppe\t\t\tBeschreibung \033[0m "
  101. fi
  102. cat $locationOfScript/projects.txt | grep -ve "^#" -e "^$" | grep -E "^$project.*$GROUP_FILTER" | while read host ; do
  103. LINENUMBER=$[LINENUMBER+1]
  104. SSHHOST=`echo $host |cut -f 2 -d ':' `
  105. DESCRIBTION=`echo $host |cut -f 3 -d ':' `
  106. USERNAME=`echo $host |cut -f 4 -d ':' `
  107. GROUP=`echo $host |cut -f 5 -d ':' `
  108. SSHPORT=`echo $host |cut -f 6 -d ':' `
  109. ## check some variables
  110. [[ -z $USERNAME ]] && USERNAME="" || USERNAME="$USERNAME @"
  111. [[ -z $SSHPORT ]] && SSHPORT="22"
  112. [[ "$PINGHOST" == "true" ]] && ping_host $SSHHOST $SSHPORT || STATUS="\t"
  113. printf "%2s %12s %-20s $STATUSCOLOR%-12s\033[0m %-13s %s\n" "$LINENUMBER" "$USERNAME" "$SSHHOST" "$STATUS" "$GROUP" "$DESCRIBTION"
  114. done
  115. # Ausgabe der Standarteintraege
  116. echo -en "\n\n x Abbruch\n z Zurueck ins Basemenu\n ============================\nAuswahl: "
  117. }
  118. #=== FUNCTION ================================================================
  119. # NAME: ping_host
  120. # DESCRIPTION: check if given ssh port is open
  121. # PARAMETERS: host, portnumber
  122. # RETURNS:
  123. #===============================================================================
  124. function ping_host() {
  125. ## returncode doesn't work in subshell ...
  126. PING=$(netcat -w 1 -z $1 $2 >> /dev/null 2>&1 ; echo $? )
  127. if [ "$PING" -eq 0 ]; then
  128. STATUS="[ONLINE]"
  129. STATUSCOLOR="\033[1;32m"
  130. else
  131. STATUS="[OFFLINE]"
  132. STATUSCOLOR="\033[1;31m"
  133. fi
  134. }
  135. #=== FUNCTION ================================================================
  136. # NAME: check_input
  137. # DESCRIPTION: check if given input is correct and digit is in list range
  138. # PARAMETERS: input, INPUT2 (optional)
  139. # RETURNS: true/false
  140. #===============================================================================
  141. function check_input() {
  142. ## no project choose
  143. [ "$INPUT" == "" ] && return 1
  144. ## User cancel
  145. [ "$1" == "x" ] && ( echo "Du weißt auch nicht was du willst ..." ; kill $MAINPID )
  146. ## project menu
  147. if [ "$2" == "INPUT2" ]; then
  148. ## back to mainmenu
  149. if [ "$1" == "z" ]; then
  150. bash $0
  151. elif [ `echo $1 | grep -E ^[[:digit:]]+$` ]; then
  152. return 0
  153. else
  154. ## happen when projectname is directly given
  155. [[ `grep "$project:$1" $locationOfScript/projects.txt` ]] && return 0 || return 1
  156. fi
  157. elif [ `echo "$1" | grep -E ^[[:digit:]]+$` ] && [ "$1" -le "$project_count" ]; then
  158. return 0
  159. ## check for project-names
  160. elif [ `cat $locationOfScript/projects.txt | cut -d ":" -f1 | egrep -v "^$|#" | sort -u | grep "$1"` ]; then
  161. return 0
  162. else
  163. return 1
  164. fi
  165. }
  166. #=== FUNCTION ================================================================
  167. # NAME: ssh_to_host
  168. # DESCRIPTION: connect to host
  169. # PARAMETERS: hostname (INPUT2)
  170. # RETURNS: true/false
  171. #===============================================================================
  172. function ssh_to_host() {
  173. ## check if number or hostname given
  174. if [ `echo $1 | grep -E ^[[:digit:]]+$` ]; then
  175. ## linenumber
  176. HOSTSTRING=$(grep "^$project" $locationOfScript/projects.txt | head -n $1 | tail -n 1 )
  177. else
  178. ## hostnme
  179. HOSTSTRING==$(grep "^$project:$1" $locationOfScript/projects.txt )
  180. fi
  181. SSHHOST=`echo $HOSTSTRING |cut -f 2 -d ':' `
  182. USERNAME=`echo $HOSTSTRING |cut -f 4 -d ':' `
  183. SSHPORT=`echo $HOSTSTRING |cut -f 6 -d ':' `
  184. [[ -z $USERNAME ]] || USERNAME="$USERNAME@"
  185. [[ -z $SSHPORT ]] && SSHPORT="22"
  186. echo "Executing: ssh -p $SSHPORT $USERNAME$SSHHOST"
  187. ssh -p $SSHPORT $USERNAME$SSHHOST
  188. }
  189. #=== Main Script =============================================================
  190. #
  191. #
  192. #===============================================================================
  193. while ! check_input $INPUT ; do
  194. if [ -z $1 ]; then
  195. clear
  196. echo " == Super SSH Sprungmenu =="
  197. echo
  198. cat $locationOfScript/projects.txt | grep -ve "^#" -e "^$" | cut -d ":" -f "1" | awk '!a[$0]++' | cat -n
  199. echo
  200. echo " x Abbruch"
  201. echo
  202. echo " ===================="
  203. echo -ne "Auswahl: "
  204. read INPUT
  205. else
  206. INPUT="$1"
  207. fi
  208. get_projects
  209. done
  210. ## check if group OR host given
  211. while ! check_input $INPUT2 INPUT2 ; do
  212. ## nothing was given
  213. if [ -z $INPUT2 ]; then
  214. print_projectmenu $INPUT
  215. read INPUT2
  216. ## group was given
  217. elif ( [[ `echo $2 | grep -E ^[[:lower:]]$` ]] || [ `echo $INPUT2 | grep -E ^[[:lower:]]$` ] ); then
  218. print_projectmenu $INPUT $INPUT2
  219. read INPUT2
  220. ## hostnumber or hostname was given
  221. else
  222. INPUT2="$2"
  223. fi
  224. done
  225. ssh_to_host $INPUT2