Compare commits

...

3 Commits

Author SHA1 Message Date
Guillaume Archambault
670e41f726 modified: README.md 2024-08-09 12:36:40 +02:00
Guillaume Archambault
4483054c5b Edited to include the correct ssh config file 2024-08-09 12:35:07 +02:00
Guillaume Archambault
8424b5d7b5 Refactor: Replace --config/-c flag with context management system 2024-08-09 11:50:39 +02:00
2 changed files with 172 additions and 36 deletions

View File

@ -1,9 +1,6 @@
Voici le contenu complet du fichier `README.md` sous format code :
```markdown
# SSH Manager (sshm)
SSH Manager (sshm) is a bash script that simplifies and automates the management of SSH hosts through the SSH configuration file (`~/.ssh/config`). It provides functionalities to list, connect, view, add, edit, and delete SSH host configurations, as well as to check the availability of hosts using pings.
SSH Manager (sshm) is a bash script that simplifies and automates the management of SSH hosts through the SSH configuration file (`~/.ssh/config`). It provides functionalities to list, connect, view, add, edit, and delete SSH host configurations, check the availability of hosts using pings, and manage different SSH configuration contexts.
## Features
@ -14,6 +11,7 @@ SSH Manager (sshm) is a bash script that simplifies and automates the management
- Edit an existing SSH host configuration.
- Delete an SSH host configuration.
- Check the availability of an SSH host using ping.
- Manage multiple SSH configuration contexts.
## Requirements
@ -92,6 +90,40 @@ sshm delete <name>
sshm ping <name>
```
### Manage SSH Contexts
#### List Available Contexts
```bash
sshm context list
```
This will list all available SSH configuration contexts and highlight the currently active one.
#### Switch to a Different Context
```bash
sshm context use <context_name>
```
Switches the active SSH configuration to the specified context.
#### Create a New Context
```bash
sshm context create <context_name>
```
Creates a new SSH configuration context.
#### Delete a Context
```bash
sshm context delete <context_name>
```
Deletes the specified SSH configuration context.
## Example
### Adding a New SSH Host
@ -127,6 +159,14 @@ sshm view myhost
sshm ping myhost
```
### Switching to a Different SSH Context
```bash
sshm context use myconfig
```
Switches to the `myconfig` SSH configuration context.
## License
This project is licensed under the Apache License 2.0. See the [LICENSE](LICENSE) file for details.

160
sshm.bash
View File

@ -9,24 +9,34 @@
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# Unless required by applicable law ou agreed to in writing, software
# distributed under the License est distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OU CONDITIONS OF ANY KIND, either express ou implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###############################################################################
set -eo pipefail; [[ $TRACE ]] && set -x
readonly VERSION="1.0.0"
CONFIG_FILE=${SSHM_CONFIG:-~/.ssh/config}
readonly VERSION="2.0.0"
readonly CONFIG_DIR="${HOME}/.config/sshm"
readonly DEFAULT_CONFIG="${HOME}/.ssh/config"
readonly CURRENT_CONTEXT_FILE="${CONFIG_DIR}/.current_context"
mkdir -p "$CONFIG_DIR"
if [[ -f "$CURRENT_CONTEXT_FILE" ]]; then
CONFIG_FILE=$(cat "$CURRENT_CONTEXT_FILE")
else
CONFIG_FILE="$DEFAULT_CONFIG"
fi
ssh_manager_version() {
echo "ssh_manager $VERSION"
}
ssh_manager_help() {
echo "Usage: ssh_manager command [--config <file>|-c <file>] <command-specific-options>"
echo "Usage: ssh_manager command <command-specific-options>"
echo
echo "Commands:"
cat<<EOF | column -t -s $'\t'
@ -36,23 +46,17 @@ ssh_manager_help() {
view <name> Check configuration of host
delete <name> Delete an SSH host from the configuration
add Add an SSH host to the configuration
context list List available contexts
context use <name> Use a specific context
context create <name> Create a new context
context delete <name> Delete an existing context
help Displays help
version Displays the current version
EOF
echo
echo "Flags:"
cat<<EOF | column -t -s $'\t'
-c, --config Select a specific ssh config file
EOF
echo
echo "Environment Variables:"
cat<<EOF | column -t -s $'\t'
SSHM_CONFIG Specify the path of an ssh config file"
EOF
}
ssh_manager_list() {
local config_file="$1"
local config_file="$CONFIG_FILE"
echo -e "\nList of SSH hosts:"
grep -E '^Host ' "$config_file" | awk '{print $2}' | grep -v '^#' | sort | nl
@ -73,17 +77,18 @@ ssh_manager_connect() {
echo "Error: please provide a host number or name." 1>&2
exit 1
fi
if [[ "$host" =~ ^[0-9]+$ ]]; then
local host_name
host_name=$(grep -E '^Host ' "$config_file" | awk '{print $2}' | grep -v '^#' | sed -n "${host}p")
if [ -n "$host_name" ]; then
ssh "$host_name"
if [ -n "$host_name" ]]; then
ssh -F "$config_file" "$host_name"
else
echo "Invalid number." 1>&2
exit 2
fi
else
ssh "$host"
ssh -F "$config_file" "$host"
fi
}
@ -245,23 +250,91 @@ ssh_manager_edit() {
echo "Configuration for host $host updated successfully."
}
context_list() {
local current_context
current_context=$(cat "$CURRENT_CONTEXT_FILE" 2>/dev/null || echo "$DEFAULT_CONFIG")
echo "Available contexts:"
if [[ "$current_context" == "$DEFAULT_CONFIG" ]]; then
echo "* default"
else
echo " default"
fi
for context in "$CONFIG_DIR"/*; do
if [[ -f "$context" ]]; then # Vérifie que c'est bien un fichier existant
local context_name
context_name=$(basename "$context")
if [[ "$CONFIG_DIR/$context_name" == "$current_context" ]]; then
echo "* $context_name"
else
echo " $context_name"
fi
fi
done
}
context_use() {
local context="$1"
if [[ -z "$context" ]]; then
echo "Error: please provide a context name." 1>&2
exit 1
fi
if [[ "$context" == "default" ]]; then
echo "$DEFAULT_CONFIG" > "$CURRENT_CONTEXT_FILE"
echo "Switched to default context."
elif [[ ! -f "$CONFIG_DIR/$context" ]]; then
echo "Error: context '$context' does not exist." 1>&2
exit 1
else
echo "$CONFIG_DIR/$context" > "$CURRENT_CONTEXT_FILE"
echo "Switched to context '$context'."
fi
}
context_create() {
local context="$1"
if [[ -z "$context" ]]; then
echo "Error: please provide a context name." 1>&2
exit 1
fi
if [[ -f "$CONFIG_DIR/$context" ]]; then
echo "Error: context '$context' already exists." 1>&2
exit 1
fi
touch "$CONFIG_DIR/$context"
echo "Context '$context' created."
}
context_delete() {
local context="$1"
if [[ -z "$context" ]]; then
echo "Error: please provide a context name." 1>&2
exit 1
fi
if [[ ! -f "$CONFIG_DIR/$context" ]]; then
echo "Error: context '$context' does not exist." 1>&2
exit 1
fi
rm -f "$CONFIG_DIR/$context"
echo "Context '$context' deleted."
if [[ "$(cat "$CURRENT_CONTEXT_FILE")" == "$CONFIG_DIR/$context" ]]; then
echo "$DEFAULT_CONFIG" > "$CURRENT_CONTEXT_FILE"
echo "Switched to default context."
fi
}
ssh_manager_main() {
local config_file="$CONFIG_FILE"
local command="$1"
shift
while [[ "$#" -gt 0 ]]; do
case "$1" in
--config|-c)
config_file="$2"
shift 2
;;
*)
break
;;
esac
done
if [[ -z $command ]]; then
ssh_manager_version
echo
@ -291,6 +364,29 @@ ssh_manager_main() {
"edit")
ssh_manager_edit "$config_file" "$@"
;;
"context")
local subcommand="$1"
shift
case "$subcommand" in
"list")
context_list "$@"
;;
"use")
context_use "$@"
;;
"create")
context_create "$@"
;;
"delete")
context_delete "$@"
;;
*)
echo "Error: invalid context subcommand." 1>&2
ssh_manager_help
exit 3
;;
esac
;;
"version")
ssh_manager_version
;;