| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- #!/bin/bash
- # ##########################################################
- #
- # Super SSH Script
- #
- # Version 9.1 [11.2017]
- #
- # V9.2 Rewrite project menu output, change from echo to printf
- # V9.1 Project overview now accept project names as input [08.11.2017]
- # V9. [06.10.2017]
- # V8. Added group function again [08.2017]
- # V7. Rewrite script - now with functions [06.2016]
- # V6. Added function to pass parameters to server menu [05.2012]
- # V5. Added function to choose by hostname [05.2012]
- # V4. Added group function [11.2011]
- # V3. Added avaibility function
- # V2. color output
- # V1. Create script
- #### config ####
- ## Wenn true wird jeder Host 1x angepingt und der Status ausgegeben
- PINGHOST="true" # gueltige Werte [true,false]
- #### do not change ####
- ## used to kill from subshell
- MAINPID=$$
- ## locationOfScript
- locationOfScript=$(dirname "$(readlink -e "$0")")
- #=== FUNCTION ================================================================
- # NAME: get_projects
- # DESCRIPTION: find all projects
- # PARAMETERS:
- # RETURNS:
- #===============================================================================
- function get_projects() {
- for project in $(cat $locationOfScript/projects.txt | grep -ve "^#" -e "^$" | cut -d ":" -f "1" | awk '!a[$0]++'); do
- projects=$projects:$(echo $project)
- project_count=$[project_count+1]
- done
- ## remove first :
- projects=$(echo $projects | /bin/sed 's/://')
- }
- #=== FUNCTION ================================================================
- # NAME: get_groups
- # DESCRIPTION: find all groups of given project
- # PARAMETERS: project_name
- # RETURNS:
- #===============================================================================
- function get_groups() {
- ## reset groups/groupscount (neccessary for project reload)
- groups=""
- groups_count=0
- ## be carfully: $GROUPS is an BASH internal variable
- for group in $(cat $locationOfScript/projects.txt | grep -ve "^#" -e "^$" | grep "$1" | cut -d ":" -f "5" | awk '!a[$0]++'); do
- groups=$groups:$(echo $group)
- groups_count=$[groups_count+1]
- done
- ## remove first :
- groups=$(echo $groups | /bin/sed 's/://')
- # x y z sind mit sonderfunktionen belegt!
- 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 )
- for (( i=0;i<$groups_count;i++)); do
- # zur späteren Kontrolle schreiben wir alle BENUTZTEN Buchstaben in ein Array
- arr_character["$i"]="${arr_character[${i}]}"
- # Ausgabe der Gruppen in einer Zeile
- echo -en "[\033[1;33m"${arr_character[${i}]}"\033[0m]\033[1;32m"$( echo $groups | cut -d ":" -f "$(($i+1))" )"\033[0m "
- done
- }
- #=== FUNCTION ================================================================
- # NAME: print_projectmenu
- # DESCRIPTION: print out projectmenu for given project
- # PARAMETERS: project,group
- # RETURNS:
- #===============================================================================
- function print_projectmenu() {
- ## reset variables
- GROUP_FILTER=
- ## check if given projectname or projectnumber
- if [[ "$1" == ?(-)+([0-9]) ]]; then
- ## projectnumber, read projectname from "projects" string
- project=$( echo $projects | cut -d ":" -f "$1" )
- else
- ## projectname ...
- project=$1
- fi
- ## group filter given
- if [ -n "$2" ]; then
- for i in "${!arr_character[@]}"; do
- if [ "${arr_character[$i]}" == "$2" ]; then
- GROUP_FILTER=$( echo $groups | cut -d ":" -f $(($i+1)) )
- fi
- done
- fi
- clear
- echo -e " == SSH Sprungmenu ==\n\n Projekt: $project\n"
- ## just read out all groups
- get_groups $project
- # Ausgabe des Headers
- if [ "$PINGHOST" == "true" ]; then
- printf "\n\033[1;33m%3s %-11s %-20s %-12s %-13s %s\033[0m\n" "ID" "USER" "Host" "[Status]" "Gruppe" "Beschreibung"
- else
- echo -e "\033[1;33mID USER\t Host\tGruppe\t\t\tBeschreibung \033[0m "
- fi
- cat $locationOfScript/projects.txt | grep -ve "^#" -e "^$" | grep -E "^$project.*$GROUP_FILTER" | while read host ; do
- LINENUMBER=$[LINENUMBER+1]
- SSHHOST=`echo $host |cut -f 2 -d ':' `
- DESCRIBTION=`echo $host |cut -f 3 -d ':' `
- USERNAME=`echo $host |cut -f 4 -d ':' `
- GROUP=`echo $host |cut -f 5 -d ':' `
- SSHPORT=`echo $host |cut -f 6 -d ':' `
- ## check some variables
- [[ -z $USERNAME ]] && USERNAME="" || USERNAME="$USERNAME @"
- [[ -z $SSHPORT ]] && SSHPORT="22"
- [[ "$PINGHOST" == "true" ]] && ping_host $SSHHOST $SSHPORT || STATUS="\t"
- printf "%2s %12s %-20s $STATUSCOLOR%-12s\033[0m %-13s %s\n" "$LINENUMBER" "$USERNAME" "$SSHHOST" "$STATUS" "$GROUP" "$DESCRIBTION"
- done
- # Ausgabe der Standarteintraege
- echo -en "\n\n x Abbruch\n z Zurueck ins Basemenu\n ============================\nAuswahl: "
- }
- #=== FUNCTION ================================================================
- # NAME: ping_host
- # DESCRIPTION: check if given ssh port is open
- # PARAMETERS: host, portnumber
- # RETURNS:
- #===============================================================================
- function ping_host() {
- ## returncode doesn't work in subshell ...
- PING=$(netcat -w 1 -z $1 $2 >> /dev/null 2>&1 ; echo $? )
- if [ "$PING" -eq 0 ]; then
- STATUS="[ONLINE]"
- STATUSCOLOR="\033[1;32m"
- else
- STATUS="[OFFLINE]"
- STATUSCOLOR="\033[1;31m"
- fi
- }
- #=== FUNCTION ================================================================
- # NAME: check_input
- # DESCRIPTION: check if given input is correct and digit is in list range
- # PARAMETERS: input, INPUT2 (optional)
- # RETURNS: true/false
- #===============================================================================
- function check_input() {
- ## no project choose
- [ "$INPUT" == "" ] && return 1
- ## User cancel
- [ "$1" == "x" ] && ( echo "Du weißt auch nicht was du willst ..." ; kill $MAINPID )
- ## project menu
- if [ "$2" == "INPUT2" ]; then
- ## back to mainmenu
- if [ "$1" == "z" ]; then
- bash $0
- elif [ `echo $1 | grep -E ^[[:digit:]]+$` ]; then
- return 0
- else
- ## happen when projectname is directly given
- [[ `grep "$project:$1" $locationOfScript/projects.txt` ]] && return 0 || return 1
- fi
- elif [ `echo "$1" | grep -E ^[[:digit:]]+$` ] && [ "$1" -le "$project_count" ]; then
- return 0
- ## check for project-names
- elif [ `cat $locationOfScript/projects.txt | cut -d ":" -f1 | egrep -v "^$|#" | sort -u | grep "$1"` ]; then
- return 0
- else
- return 1
- fi
- }
- #=== FUNCTION ================================================================
- # NAME: ssh_to_host
- # DESCRIPTION: connect to host
- # PARAMETERS: hostname (INPUT2)
- # RETURNS: true/false
- #===============================================================================
- function ssh_to_host() {
- ## check if number or hostname given
- if [ `echo $1 | grep -E ^[[:digit:]]+$` ]; then
- ## linenumber
- HOSTSTRING=$(grep "^$project" $locationOfScript/projects.txt | head -n $1 | tail -n 1 )
- else
- ## hostnme
- HOSTSTRING==$(grep "^$project:$1" $locationOfScript/projects.txt )
- fi
- SSHHOST=`echo $HOSTSTRING |cut -f 2 -d ':' `
- USERNAME=`echo $HOSTSTRING |cut -f 4 -d ':' `
- SSHPORT=`echo $HOSTSTRING |cut -f 6 -d ':' `
- [[ -z $USERNAME ]] || USERNAME="$USERNAME@"
- [[ -z $SSHPORT ]] && SSHPORT="22"
- echo "Executing: ssh -p $SSHPORT $USERNAME$SSHHOST"
- ssh -p $SSHPORT $USERNAME$SSHHOST
- }
- #=== Main Script =============================================================
- #
- #
- #===============================================================================
- while ! check_input $INPUT ; do
- if [ -z $1 ]; then
- clear
- echo " == Super SSH Sprungmenu =="
- echo
- cat $locationOfScript/projects.txt | grep -ve "^#" -e "^$" | cut -d ":" -f "1" | awk '!a[$0]++' | cat -n
- echo
- echo " x Abbruch"
- echo
- echo " ===================="
- echo -ne "Auswahl: "
- read INPUT
- else
- INPUT="$1"
- fi
- get_projects
- done
- ## check if group OR host given
- while ! check_input $INPUT2 INPUT2 ; do
- ## nothing was given
- if [ -z $INPUT2 ]; then
- print_projectmenu $INPUT
- read INPUT2
- ## group was given
- elif ( [[ `echo $2 | grep -E ^[[:lower:]]$` ]] || [ `echo $INPUT2 | grep -E ^[[:lower:]]$` ] ); then
- print_projectmenu $INPUT $INPUT2
- read INPUT2
- ## hostnumber or hostname was given
- else
- INPUT2="$2"
- fi
- done
- ssh_to_host $INPUT2
|