Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # Dialog programm.
- DIALOG=whiptail
- #DIALOG=dialog
- #DIALOG=gdialog
- CONFIG=~/.ssh/config.cfg
- # Create auto tmp files.
- tempfile1=/tmp/dialog_1_$$
- tempfile2=/tmp/dialog_2_$$
- trap "rm -f $tempfile1" 0 1 2 5 15
- trap "rm -f $tempfile2" 0 1 2 5 15
- main_menu() {
- local TITLE='Список групп'
- eval "$DIALOG --cancel-button 'Exit' --menu \"$TITLE\" 20 60 10 "$(get_groups) 2>$tempfile1
- if [ "$?" = "0" ]; then # OK is pressed
- local _selected="$(cat $tempfile1)"
- # Parse data for selected host from config and connect.
- while true; do
- group_menu "${_selected}"
- done
- else # Cancel is pressed
- echo "INFO : Cancel is pressed, exiting..."
- break
- fi
- }
- group_menu() {
- local group="$1"
- local TITLE='Список хостов в группе '"$group"
- eval "$DIALOG --cancel-button 'Back' --menu \"$TITLE\" 20 60 10 "$(get_hosts_by_group "$group") 2>$tempfile2
- if [ "$?" = "0" ]; then # OK is pressed
- local _selected=$(cat $tempfile2)
- # Parse data for selected host from config and connect.
- $(get_host_by_group_and_host "$group" "${_selected}")
- else # Cancel is pressed
- echo "INFO : Cancel is pressed, exiting from group_menu..."
- break
- fi
- }
- get_hosts_by_group() {
- # get_hosts_by_group group
- local group="$1"
- awk -F'|' '{
- sub(/[;#].*/, "", $0)
- if ($0 && $2 == "'"$group"'") printf("%s \"%s\" ", $1, $6)
- }' "$CONFIG"
- }
- get_host_by_group_and_host() {
- # get_host_by_group_and_host group host
- local group="$1"
- local host="$2"
- awk -F'|' 'BEGIN {
- command="ssh -o StrictHostKeyChecking=no"
- } /^'"$host"'\|/ {
- sub(/[;#].*/, "", $0)
- if ($0 && $2 == "'"$group"'") {
- if($3 && $3 != "22") command=command" -p "$3
- if($4) command=command" -i "$4
- if($5) command="sshpass -p "$5" "command
- command=command" "$1
- }
- } END {
- print command
- }' "$CONFIG"
- }
- get_groups() {
- awk -F'|' '{
- sub(/[;#].*/, "", $0)
- if ($0 && groups[$2] == 0) groups[$2]
- } END {
- for (k in groups) {
- printf("\"%s\" \"\" ", k)
- }
- }' "$CONFIG"
- }
- while true; do
- main_menu
- done
- rm -f $tempfile1
- rm -f $tempfile2
- #cat .ssh/config.cfg
- # Format of this file:
- # user@host|group|port|key|password|description
- # Examples:
- # user@host|group1|22|key||description1
- # user@host|group2||key||description
- # user@host|group3|2222||password|descr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement