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
- 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.
- Add a new SSH host configuration.
- Edit an existing SSH host configuration.
@ -53,13 +53,13 @@ sshm list
### Connect to an SSH Host
```bash
sshm <host>
sshm connect <name>
```
### View SSH Host Configuration
```bash
sshm view <host>
sshm view <name>
```
### 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
```bash
sshm edit <host>
sshm edit <name>
```
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
```bash
sshm delete <host>
sshm delete <name>
```
### Check SSH Host Availability
```bash
sshm ping <host>
sshm ping <name>
```
### Manage SSH Contexts

111
sshm.bash
View File

@ -18,11 +18,10 @@
set -eo pipefail; [[ $TRACE ]] && set -x
readonly VERSION="2.1.0"
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"
readonly GITHUB_REPO="Gu1llaum-3/sshm"
mkdir -p "$CONFIG_DIR"
@ -32,35 +31,17 @@ else
CONFIG_FILE="$DEFAULT_CONFIG"
fi
sshm_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_version() {
echo "ssh_manager $VERSION"
}
sshm_help() {
echo "Usage: sshm [command] <command-specific-options>"
ssh_manager_help() {
echo "Usage: ssh_manager command <command-specific-options>"
echo
echo "Commands:"
cat<<EOF | column -t -s $'\t'
<host> Connect directly to SSH host by name
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
view <name> Check configuration of host
delete <name> Delete an SSH host from the configuration
@ -74,7 +55,7 @@ sshm_help() {
EOF
}
sshm_list() {
ssh_manager_list() {
local config_file="$CONFIG_FILE"
echo -e "\nList of SSH hosts:"
grep -E '^Host ' "$config_file" | awk '{print $2}' | grep -v '^#' | sort | nl
@ -86,10 +67,10 @@ sshm_list() {
exit 0
fi
sshm_connect "$config_file" "$host"
ssh_manager_connect "$config_file" "$host"
}
sshm_connect() {
ssh_manager_connect() {
local config_file="$1"
local host="$2"
if [[ -z "$host" ]]; then
@ -103,22 +84,15 @@ sshm_connect() {
if [[ -n "$host_name" ]]; then
ssh -F "$config_file" "$host_name"
else
echo "Error: Invalid host number." 1>&2
echo "Invalid number." 1>&2
exit 2
fi
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"
fi
}
sshm_ping() {
ssh_manager_ping() {
local config_file="$1"
local host="$2"
if [[ -z "$host" ]]; then
@ -140,7 +114,7 @@ sshm_ping() {
fi
}
sshm_view() {
ssh_manager_view() {
local config_file="$1"
local host="$2"
if [[ -z "$host" ]]; then
@ -159,7 +133,7 @@ sshm_view() {
echo "$host_info"
}
sshm_delete() {
ssh_manager_delete() {
local config_file="$1"
local host="$2"
if [[ -z "$host" ]]; then
@ -173,7 +147,7 @@ sshm_delete() {
echo "Host $host removed from SSH configuration."
}
sshm_add() {
ssh_manager_add() {
local config_file="$1"
local host="$2"
local hostname
@ -181,9 +155,7 @@ sshm_add() {
local port
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)
default_identity_file=${default_identity_file:-~/.ssh/id_rsa}
# Request necessary information
if [[ -z "$host" ]]; then
read -p "Enter host name: " host
if [[ -z "$host" ]]; then
@ -204,9 +176,10 @@ sshm_add() {
read -p "Enter SSH port (default: 22): " port
port=${port:-22}
read -p "Enter path to SSH key (default: $default_identity_file): " identity_file
identity_file=${identity_file:-$default_identity_file}
read -p "Enter path to SSH key (default: ~/.ssh/id_rsa): " identity_file
identity_file=${identity_file:-~/.ssh/id_rsa}
# Add the new configuration to the file
{
echo ""
echo "Host $host"
@ -215,7 +188,7 @@ sshm_add() {
if [[ "$port" -ne 22 ]]; then
echo " Port $port"
fi
if [[ "$identity_file" != "$default_identity_file" ]]; then
if [[ "$identity_file" != ~/.ssh/id_rsa ]]; then
echo " IdentityFile $identity_file"
fi
} >> "$config_file"
@ -223,7 +196,7 @@ sshm_add() {
echo "Configuration for host $host added successfully."
}
sshm_edit() {
ssh_manager_edit() {
local config_file="$1"
local host="$2"
if [[ -z "$host" ]]; then
@ -258,7 +231,7 @@ sshm_edit() {
new_identity_file=${new_identity_file:-${current_identity_file:-~/.ssh/id_rsa}}
# Delete the old configuration
sshm_delete "$config_file" "$host"
ssh_manager_delete "$config_file" "$host"
# Add the new configuration
{
@ -333,7 +306,6 @@ context_create() {
fi
touch "$CONFIG_DIR/$context"
chmod 600 "$CONFIG_DIR/$context"
echo "Context '$context' created."
}
@ -358,37 +330,39 @@ context_delete() {
fi
}
sshm_main() {
ssh_manager_main() {
local config_file="$CONFIG_FILE"
local command="$1"
shift
if [[ -z $command ]]; then
sshm_version
ssh_manager_version
echo
sshm_help
ssh_manager_help
exit 0
fi
# Check if command is a known command, otherwise treat it as a host to connect to
case "$command" in
"list")
sshm_list "$config_file"
ssh_manager_list "$config_file"
;;
"connect")
ssh_manager_connect "$config_file" "$@"
;;
"ping")
sshm_ping "$config_file" "$@"
ssh_manager_ping "$config_file" "$@"
;;
"view")
sshm_view "$config_file" "$@"
ssh_manager_view "$config_file" "$@"
;;
"delete")
sshm_delete "$config_file" "$@"
ssh_manager_delete "$config_file" "$@"
;;
"add")
sshm_add "$config_file" "$@"
ssh_manager_add "$config_file" "$@"
;;
"edit")
sshm_edit "$config_file" "$@"
ssh_manager_edit "$config_file" "$@"
;;
"context")
local subcommand="$1"
@ -408,30 +382,23 @@ sshm_main() {
;;
*)
echo "Error: invalid context subcommand." 1>&2
sshm_help
ssh_manager_help
exit 3
;;
esac
;;
"version")
sshm_version
ssh_manager_version
;;
"help")
sshm_help
ssh_manager_help
;;
*)
# If command is not recognized, treat it as a host name to connect to
sshm_connect "$config_file" "$command"
ssh_manager_help 1>&2
exit 3
esac
}
if [[ "$0" == "$BASH_SOURCE" ]]; then
# If no arguments are provided, display help
if [[ $# -eq 0 ]]; then
sshm_version
echo
sshm_help
else
sshm_main "$@"
fi
ssh_manager_main "$@"
fi