Compare commits

..

No commits in common. "f161cf43e46f8b03ccf828d1503917945ba33c7d" and "281b541655adfe70bf857471f88e0199d9f5992f" have entirely different histories.

2 changed files with 45 additions and 78 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 name. - Connect to an SSH host by number or 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 <host> sshm connect <name>
``` ```
### View SSH Host Configuration ### View SSH Host Configuration
```bash ```bash
sshm view <host> sshm view <name>
``` ```
### 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 <host> sshm edit <name>
``` ```
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 <host> sshm delete <name>
``` ```
### Check SSH Host Availability ### Check SSH Host Availability
```bash ```bash
sshm ping <host> sshm ping <name>
``` ```
### Manage SSH Contexts ### Manage SSH Contexts

111
sshm.bash
View File

@ -18,11 +18,10 @@
set -eo pipefail; [[ $TRACE ]] && set -x set -eo pipefail; [[ $TRACE ]] && set -x
readonly VERSION="2.1.0" readonly VERSION="2.0.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"
@ -32,35 +31,17 @@ else
CONFIG_FILE="$DEFAULT_CONFIG" CONFIG_FILE="$DEFAULT_CONFIG"
fi fi
sshm_version() { ssh_manager_version() {
echo "sshm $VERSION" echo "ssh_manager $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
} }
sshm_help() { ssh_manager_help() {
echo "Usage: sshm [command] <command-specific-options>" echo "Usage: ssh_manager 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
@ -74,7 +55,7 @@ sshm_help() {
EOF EOF
} }
sshm_list() { ssh_manager_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
@ -86,10 +67,10 @@ sshm_list() {
exit 0 exit 0
fi fi
sshm_connect "$config_file" "$host" ssh_manager_connect "$config_file" "$host"
} }
sshm_connect() { ssh_manager_connect() {
local config_file="$1" local config_file="$1"
local host="$2" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
@ -103,22 +84,15 @@ sshm_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 "Error: Invalid host number." 1>&2 echo "Invalid 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
} }
sshm_ping() { ssh_manager_ping() {
local config_file="$1" local config_file="$1"
local host="$2" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
@ -140,7 +114,7 @@ sshm_ping() {
fi fi
} }
sshm_view() { ssh_manager_view() {
local config_file="$1" local config_file="$1"
local host="$2" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
@ -159,7 +133,7 @@ sshm_view() {
echo "$host_info" echo "$host_info"
} }
sshm_delete() { ssh_manager_delete() {
local config_file="$1" local config_file="$1"
local host="$2" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
@ -173,7 +147,7 @@ sshm_delete() {
echo "Host $host removed from SSH configuration." echo "Host $host removed from SSH configuration."
} }
sshm_add() { ssh_manager_add() {
local config_file="$1" local config_file="$1"
local host="$2" local host="$2"
local hostname local hostname
@ -181,9 +155,7 @@ sshm_add() {
local port local port
local identity_file local identity_file
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) # Request necessary information
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
@ -204,9 +176,10 @@ sshm_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: $default_identity_file): " identity_file read -p "Enter path to SSH key (default: ~/.ssh/id_rsa): " identity_file
identity_file=${identity_file:-$default_identity_file} identity_file=${identity_file:-~/.ssh/id_rsa}
# Add the new configuration to the file
{ {
echo "" echo ""
echo "Host $host" echo "Host $host"
@ -215,7 +188,7 @@ sshm_add() {
if [[ "$port" -ne 22 ]]; then if [[ "$port" -ne 22 ]]; then
echo " Port $port" echo " Port $port"
fi fi
if [[ "$identity_file" != "$default_identity_file" ]]; then if [[ "$identity_file" != ~/.ssh/id_rsa ]]; then
echo " IdentityFile $identity_file" echo " IdentityFile $identity_file"
fi fi
} >> "$config_file" } >> "$config_file"
@ -223,7 +196,7 @@ sshm_add() {
echo "Configuration for host $host added successfully." echo "Configuration for host $host added successfully."
} }
sshm_edit() { ssh_manager_edit() {
local config_file="$1" local config_file="$1"
local host="$2" local host="$2"
if [[ -z "$host" ]]; then if [[ -z "$host" ]]; then
@ -258,7 +231,7 @@ sshm_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
sshm_delete "$config_file" "$host" ssh_manager_delete "$config_file" "$host"
# Add the new configuration # Add the new configuration
{ {
@ -333,7 +306,6 @@ 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."
} }
@ -358,37 +330,39 @@ context_delete() {
fi fi
} }
sshm_main() { ssh_manager_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
sshm_version ssh_manager_version
echo echo
sshm_help ssh_manager_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")
sshm_list "$config_file" ssh_manager_list "$config_file"
;;
"connect")
ssh_manager_connect "$config_file" "$@"
;; ;;
"ping") "ping")
sshm_ping "$config_file" "$@" ssh_manager_ping "$config_file" "$@"
;; ;;
"view") "view")
sshm_view "$config_file" "$@" ssh_manager_view "$config_file" "$@"
;; ;;
"delete") "delete")
sshm_delete "$config_file" "$@" ssh_manager_delete "$config_file" "$@"
;; ;;
"add") "add")
sshm_add "$config_file" "$@" ssh_manager_add "$config_file" "$@"
;; ;;
"edit") "edit")
sshm_edit "$config_file" "$@" ssh_manager_edit "$config_file" "$@"
;; ;;
"context") "context")
local subcommand="$1" local subcommand="$1"
@ -408,30 +382,23 @@ sshm_main() {
;; ;;
*) *)
echo "Error: invalid context subcommand." 1>&2 echo "Error: invalid context subcommand." 1>&2
sshm_help ssh_manager_help
exit 3 exit 3
;; ;;
esac esac
;; ;;
"version") "version")
sshm_version ssh_manager_version
;; ;;
"help") "help")
sshm_help ssh_manager_help
;; ;;
*) *)
# If command is not recognized, treat it as a host name to connect to ssh_manager_help 1>&2
sshm_connect "$config_file" "$command" exit 3
esac esac
} }
if [[ "$0" == "$BASH_SOURCE" ]]; then if [[ "$0" == "$BASH_SOURCE" ]]; then
# If no arguments are provided, display help ssh_manager_main "$@"
if [[ $# -eq 0 ]]; then
sshm_version
echo
sshm_help
else
sshm_main "$@"
fi
fi fi