Compare commits

...

8 Commits

2 changed files with 78 additions and 45 deletions

View File

@ -5,7 +5,7 @@ SSH Manager (sshm) is a bash script that simplifies and automates the management
## Features ## Features
- List all SSH hosts in the configuration file. - List all SSH hosts in the configuration file.
- Connect to an SSH host by number or name. - Connect to an SSH host by name.
- View the configuration details of a specific SSH host. - View the configuration details of a specific SSH host.
- Add a new SSH host configuration. - Add a new SSH host configuration.
- Edit an existing SSH host configuration. - Edit an existing SSH host configuration.
@ -53,13 +53,13 @@ sshm list
### Connect to an SSH Host ### Connect to an SSH Host
```bash ```bash
sshm connect <name> sshm <host>
``` ```
### View SSH Host Configuration ### View SSH Host Configuration
```bash ```bash
sshm view <name> sshm view <host>
``` ```
### Add a New SSH Host Configuration ### Add a New SSH Host Configuration
@ -73,7 +73,7 @@ The script will prompt you to enter the host details.
### Edit an Existing SSH Host Configuration ### Edit an Existing SSH Host Configuration
```bash ```bash
sshm edit <name> sshm edit <host>
``` ```
The script will prompt you to enter the new details for the host. The script will prompt you to enter the new details for the host.
@ -81,13 +81,13 @@ The script will prompt you to enter the new details for the host.
### Delete an SSH Host Configuration ### Delete an SSH Host Configuration
```bash ```bash
sshm delete <name> sshm delete <host>
``` ```
### Check SSH Host Availability ### Check SSH Host Availability
```bash ```bash
sshm ping <name> sshm ping <host>
``` ```
### Manage SSH Contexts ### Manage SSH Contexts

111
sshm.bash
View File

@ -18,10 +18,11 @@
set -eo pipefail; [[ $TRACE ]] && set -x set -eo pipefail; [[ $TRACE ]] && set -x
readonly VERSION="2.0.0" readonly VERSION="2.1.0"
readonly CONFIG_DIR="${HOME}/.config/sshm" readonly CONFIG_DIR="${HOME}/.config/sshm"
readonly DEFAULT_CONFIG="${HOME}/.ssh/config" readonly DEFAULT_CONFIG="${HOME}/.ssh/config"
readonly CURRENT_CONTEXT_FILE="${CONFIG_DIR}/.current_context" readonly CURRENT_CONTEXT_FILE="${CONFIG_DIR}/.current_context"
readonly GITHUB_REPO="Gu1llaum-3/sshm"
mkdir -p "$CONFIG_DIR" mkdir -p "$CONFIG_DIR"
@ -31,17 +32,35 @@ else
CONFIG_FILE="$DEFAULT_CONFIG" CONFIG_FILE="$DEFAULT_CONFIG"
fi fi
ssh_manager_version() { sshm_version() {
echo "ssh_manager $VERSION" echo "sshm $VERSION"
# Fetch the latest release tag from GitHub
local latest_version
latest_version=$(curl -s "https://api.github.com/repos/$GITHUB_REPO/releases/latest" | jq -r .tag_name)
if [[ "$latest_version" == "null" ]]; then
echo "sshm $VERSION"
echo "Error: Unable to fetch the latest release from GitHub." 1>&2
exit 1
fi
# Compare with the current version
if [[ "$latest_version" != "$VERSION" ]]; then
echo "A new version of sshm is available: $latest_version (current: $VERSION)"
echo "You can update by running: git pull origin main"
else
echo "This is the latest version"
fi
} }
ssh_manager_help() { sshm_help() {
echo "Usage: ssh_manager command <command-specific-options>" echo "Usage: sshm [command] <command-specific-options>"
echo echo
echo "Commands:" echo "Commands:"
cat<<EOF | column -t -s $'\t' cat<<EOF | column -t -s $'\t'
<host> Connect directly to SSH host by name
list List SSH hosts and prompt for connection list List SSH hosts and prompt for connection
connect <number|name> Connect to SSH host by number or name
ping <name> Ping an SSH host to check availability ping <name> Ping an SSH host to check availability
view <name> Check configuration of host view <name> Check configuration of host
delete <name> Delete an SSH host from the configuration delete <name> Delete an SSH host from the configuration
@ -55,7 +74,7 @@ ssh_manager_help() {
EOF EOF
} }
ssh_manager_list() { sshm_list() {
local config_file="$CONFIG_FILE" local config_file="$CONFIG_FILE"
echo -e "\nList of SSH hosts:" echo -e "\nList of SSH hosts:"
grep -E '^Host ' "$config_file" | awk '{print $2}' | grep -v '^#' | sort | nl grep -E '^Host ' "$config_file" | awk '{print $2}' | grep -v '^#' | sort | nl
@ -67,10 +86,10 @@ ssh_manager_list() {
exit 0 exit 0
fi fi
ssh_manager_connect "$config_file" "$host" sshm_connect "$config_file" "$host"
} }
ssh_manager_connect() { sshm_connect() {
local config_file="$1" local config_file="$1"
local host="$2" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
@ -84,15 +103,22 @@ ssh_manager_connect() {
if [[ -n "$host_name" ]]; then if [[ -n "$host_name" ]]; then
ssh -F "$config_file" "$host_name" ssh -F "$config_file" "$host_name"
else else
echo "Invalid number." 1>&2 echo "Error: Invalid host number." 1>&2
exit 2 exit 2
fi fi
else else
# Check if the host exists in the SSH configuration
if ! grep -q "^Host $host$" "$config_file"; then
echo "Error: Host '$host' not found in SSH configuration." 1>&2
echo "Use 'sshm list' to see available hosts or 'sshm add $host' to add it." 1>&2
exit 1
fi
ssh -F "$config_file" "$host" ssh -F "$config_file" "$host"
fi fi
} }
ssh_manager_ping() { sshm_ping() {
local config_file="$1" local config_file="$1"
local host="$2" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
@ -114,7 +140,7 @@ ssh_manager_ping() {
fi fi
} }
ssh_manager_view() { sshm_view() {
local config_file="$1" local config_file="$1"
local host="$2" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
@ -133,7 +159,7 @@ ssh_manager_view() {
echo "$host_info" echo "$host_info"
} }
ssh_manager_delete() { sshm_delete() {
local config_file="$1" local config_file="$1"
local host="$2" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
@ -147,7 +173,7 @@ ssh_manager_delete() {
echo "Host $host removed from SSH configuration." echo "Host $host removed from SSH configuration."
} }
ssh_manager_add() { sshm_add() {
local config_file="$1" local config_file="$1"
local host="$2" local host="$2"
local hostname local hostname
@ -155,7 +181,9 @@ ssh_manager_add() {
local port local port
local identity_file local identity_file
# Request necessary information default_identity_file=$(find ~/.ssh -maxdepth 1 -type f \( -name "id_rsa" -o -name "id_ed25519" -o -name "id_ecdsa" -o -name "id_dsa" \) | head -n 1)
default_identity_file=${default_identity_file:-~/.ssh/id_rsa}
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
read -p "Enter host name: " host read -p "Enter host name: " host
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
@ -176,10 +204,9 @@ ssh_manager_add() {
read -p "Enter SSH port (default: 22): " port read -p "Enter SSH port (default: 22): " port
port=${port:-22} port=${port:-22}
read -p "Enter path to SSH key (default: ~/.ssh/id_rsa): " identity_file read -p "Enter path to SSH key (default: $default_identity_file): " identity_file
identity_file=${identity_file:-~/.ssh/id_rsa} identity_file=${identity_file:-$default_identity_file}
# Add the new configuration to the file
{ {
echo "" echo ""
echo "Host $host" echo "Host $host"
@ -188,7 +215,7 @@ ssh_manager_add() {
if [[ "$port" -ne 22 ]]; then if [[ "$port" -ne 22 ]]; then
echo " Port $port" echo " Port $port"
fi fi
if [[ "$identity_file" != ~/.ssh/id_rsa ]]; then if [[ "$identity_file" != "$default_identity_file" ]]; then
echo " IdentityFile $identity_file" echo " IdentityFile $identity_file"
fi fi
} >> "$config_file" } >> "$config_file"
@ -196,7 +223,7 @@ ssh_manager_add() {
echo "Configuration for host $host added successfully." echo "Configuration for host $host added successfully."
} }
ssh_manager_edit() { sshm_edit() {
local config_file="$1" local config_file="$1"
local host="$2" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
@ -231,7 +258,7 @@ ssh_manager_edit() {
new_identity_file=${new_identity_file:-${current_identity_file:-~/.ssh/id_rsa}} new_identity_file=${new_identity_file:-${current_identity_file:-~/.ssh/id_rsa}}
# Delete the old configuration # Delete the old configuration
ssh_manager_delete "$config_file" "$host" sshm_delete "$config_file" "$host"
# Add the new configuration # Add the new configuration
{ {
@ -306,6 +333,7 @@ context_create() {
fi fi
touch "$CONFIG_DIR/$context" touch "$CONFIG_DIR/$context"
chmod 600 "$CONFIG_DIR/$context"
echo "Context '$context' created." echo "Context '$context' created."
} }
@ -330,39 +358,37 @@ context_delete() {
fi fi
} }
ssh_manager_main() { sshm_main() {
local config_file="$CONFIG_FILE" local config_file="$CONFIG_FILE"
local command="$1" local command="$1"
shift shift
if [[ -z $command ]]; then if [[ -z $command ]]; then
ssh_manager_version sshm_version
echo echo
ssh_manager_help sshm_help
exit 0 exit 0
fi fi
# Check if command is a known command, otherwise treat it as a host to connect to
case "$command" in case "$command" in
"list") "list")
ssh_manager_list "$config_file" sshm_list "$config_file"
;;
"connect")
ssh_manager_connect "$config_file" "$@"
;; ;;
"ping") "ping")
ssh_manager_ping "$config_file" "$@" sshm_ping "$config_file" "$@"
;; ;;
"view") "view")
ssh_manager_view "$config_file" "$@" sshm_view "$config_file" "$@"
;; ;;
"delete") "delete")
ssh_manager_delete "$config_file" "$@" sshm_delete "$config_file" "$@"
;; ;;
"add") "add")
ssh_manager_add "$config_file" "$@" sshm_add "$config_file" "$@"
;; ;;
"edit") "edit")
ssh_manager_edit "$config_file" "$@" sshm_edit "$config_file" "$@"
;; ;;
"context") "context")
local subcommand="$1" local subcommand="$1"
@ -382,23 +408,30 @@ ssh_manager_main() {
;; ;;
*) *)
echo "Error: invalid context subcommand." 1>&2 echo "Error: invalid context subcommand." 1>&2
ssh_manager_help sshm_help
exit 3 exit 3
;; ;;
esac esac
;; ;;
"version") "version")
ssh_manager_version sshm_version
;; ;;
"help") "help")
ssh_manager_help sshm_help
;; ;;
*) *)
ssh_manager_help 1>&2 # If command is not recognized, treat it as a host name to connect to
exit 3 sshm_connect "$config_file" "$command"
esac esac
} }
if [[ "$0" == "$BASH_SOURCE" ]]; then if [[ "$0" == "$BASH_SOURCE" ]]; then
ssh_manager_main "$@" # If no arguments are provided, display help
if [[ $# -eq 0 ]]; then
sshm_version
echo
sshm_help
else
sshm_main "$@"
fi
fi fi