generated from m1cypher/Best-README-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashrc_menu.txt
100 lines (80 loc) · 2.74 KB
/
bashrc_menu.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#### SSH MENU for bottom of .bashrc ####
# Timestamp For bash history
export HISTTIMEFORMAT="%F %T "
# Variables
MENU_FILE="hosts.yaml"
PUBLIC_KEY_FILE="~/.ssh/bastion_key.pub"
function show_menu {
echo "SSH Menu Options:"
while IFS='' read -r line || [[ -n "$line" ]]; do
# Skip empty lines
[[ -z "$line" ]] && continue
# Skip lines starting with '#' (comments)
[[ "$line" =~ ^[[:space:]]*# ]] && continue
# Check if the line denotes a category
if [[ "$line" =~ ^\[([^]]+)\]$ ]]; then
category="${BASH_REMATCH[1]}"
echo -e "\n$category:"
else
# Display host under the current category
echo "$line"
fi
done < "$MENU_FILE"
}
function menu_prompt {
read -p "Enter the option number: " option
}
function is_valid_option {
# Check if the option is a number and not a commented line
[[ "$option" =~ ^[0-9]+ && -n "${menu_options[$option]}" ]]
}
# Function to stop the breaking of the script
function handle_interrupt {
echo "Script interrupted. Please use the menu to exit."
}
function execute_local_terminal {
echo "Breaking into local terminal."
# You can add any commands needed for local terminal access here
break # Exit the loop for local terminal access
}
trap handle_interrupt SIGINT
while true; do
# Display the menu
show_menu
# Prompt for user input
menu_prompt
# Read the menu options again to refresh the array
declare -A menu_options
while IFS='' read -r line || [[ -n "$line" ]]; do
# Skip empty lines
[[ -z "$line" ]] && continue
# Skip lines starting with '#' (comments)
[[ "$line" =~ ^[[:space:]]*# ]] && continue
# Check if the line denotes a category
if [[ "$line" =~ ^\[([^]]+)\]$ ]]; then
category="${BASH_REMATCH[1]}"
else
# Add host under the current category to the options
menu_options["$line"]="$category:$line"
fi
done < "$MENU_FILE"
# Execute the selected option if valid, otherwise display an error
if is_valid_option; then
# Extract the host information from the menu option
host_info=${menu_options[$option]}
category=${host_info%%:*}
host=${host_info#*:}
# Check if the option is the secret menu item
if [[ "$option" == "666" ]]; then
execute_local_terminal
# Execute the SSH command
else
echo "Connecting to $host in category $category"
# You can replace the following line with your SSH command
# ssh "$host"
break # Exit the loop if a valid option was selected
fi
else
echo "Invalid option. Please try again."
fi
done