Advertisement
METAJIJI

sshmeny.sh

Mar 6th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.34 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Dialog programm.
  4. DIALOG=whiptail
  5. #DIALOG=dialog
  6. #DIALOG=gdialog
  7.  
  8. CONFIG=~/.ssh/config.cfg
  9.  
  10. # Create auto tmp files.
  11. tempfile1=/tmp/dialog_1_$$
  12. tempfile2=/tmp/dialog_2_$$
  13. trap "rm -f $tempfile1" 0 1 2 5 15
  14. trap "rm -f $tempfile2" 0 1 2 5 15
  15.  
  16.  
  17. main_menu() {
  18.     local TITLE='Список групп'
  19.  
  20.     eval "$DIALOG --cancel-button 'Exit' --menu \"$TITLE\" 20 60 10 "$(get_groups) 2>$tempfile1
  21.     if [ "$?" = "0" ]; then  # OK is pressed
  22.         local _selected="$(cat $tempfile1)"
  23.  
  24.         # Parse data for selected host from config and connect.
  25.         while true; do
  26.             group_menu "${_selected}"
  27.         done
  28.  
  29.     else  # Cancel is pressed
  30.         echo "INFO    : Cancel is pressed, exiting..."
  31.         break
  32.     fi
  33. }
  34.  
  35. group_menu() {
  36.     local group="$1"
  37.     local TITLE='Список хостов в группе '"$group"
  38.  
  39.     eval "$DIALOG --cancel-button 'Back' --menu \"$TITLE\" 20 60 10 "$(get_hosts_by_group "$group") 2>$tempfile2
  40.     if [ "$?" = "0" ]; then  # OK is pressed
  41.         local _selected=$(cat $tempfile2)
  42.  
  43.         # Parse data for selected host from config and connect.
  44.         $(get_host_by_group_and_host "$group" "${_selected}")
  45.  
  46.     else  # Cancel is pressed
  47.         echo "INFO    : Cancel is pressed, exiting from group_menu..."
  48.         break
  49.     fi
  50. }
  51.  
  52. get_hosts_by_group() {
  53. # get_hosts_by_group group
  54.     local group="$1"
  55.  
  56.     awk -F'|' '{
  57.         sub(/[;#].*/, "", $0)
  58.         if ($0 && $2 == "'"$group"'") printf("%s \"%s\" ", $1, $6)
  59.     }' "$CONFIG"
  60. }
  61.  
  62. get_host_by_group_and_host() {
  63. # get_host_by_group_and_host group host
  64.     local group="$1"
  65.     local host="$2"
  66.  
  67.     awk -F'|' 'BEGIN {
  68.         command="ssh -o StrictHostKeyChecking=no"
  69.     } /^'"$host"'\|/ {
  70.         sub(/[;#].*/, "", $0)
  71.         if ($0 && $2 == "'"$group"'") {
  72.             if($3 && $3 != "22") command=command" -p "$3
  73.             if($4) command=command" -i "$4
  74.             if($5) command="sshpass -p "$5" "command
  75.             command=command" "$1
  76.         }
  77.     } END {
  78.         print command
  79.     }' "$CONFIG"
  80. }
  81.  
  82. get_groups() {
  83.     awk -F'|' '{
  84.         sub(/[;#].*/, "", $0)
  85.         if ($0 && groups[$2] == 0) groups[$2]
  86.     } END {
  87.         for (k in groups) {
  88.             printf("\"%s\" \"\" ", k)
  89.         }
  90.     }' "$CONFIG"
  91. }
  92.  
  93.  
  94. while true; do
  95.     main_menu
  96. done
  97.  
  98. rm -f $tempfile1
  99. rm -f $tempfile2
  100.  
  101. #cat .ssh/config.cfg
  102. # Format of this file:
  103. # user@host|group|port|key|password|description
  104. # Examples:
  105. #  user@host|group1|22|key||description1
  106. #  user@host|group2||key||description
  107. #  user@host|group3|2222||password|descr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement