mirror of
https://github.com/Gu1llaum-3/sshm.git
synced 2025-09-06 21:00:45 +02:00
Compare commits
8 Commits
281b541655
...
f161cf43e4
Author | SHA1 | Date | |
---|---|---|---|
f161cf43e4 | |||
3527ead993 | |||
65bbbacaed | |||
a51c78efdf | |||
20990ef970 | |||
7cee21611d | |||
ea27b28f30 | |||
96a83d119c |
12
README.md
12
README.md
@ -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 number or name.
|
||||
- Connect to an SSH host by 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 connect <name>
|
||||
sshm <host>
|
||||
```
|
||||
|
||||
### View SSH Host Configuration
|
||||
|
||||
```bash
|
||||
sshm view <name>
|
||||
sshm view <host>
|
||||
```
|
||||
|
||||
### 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 <name>
|
||||
sshm edit <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
|
||||
|
||||
```bash
|
||||
sshm delete <name>
|
||||
sshm delete <host>
|
||||
```
|
||||
|
||||
### Check SSH Host Availability
|
||||
|
||||
```bash
|
||||
sshm ping <name>
|
||||
sshm ping <host>
|
||||
```
|
||||
|
||||
### Manage SSH Contexts
|
||||
|
111
sshm.bash
111
sshm.bash
@ -18,10 +18,11 @@
|
||||
|
||||
set -eo pipefail; [[ $TRACE ]] && set -x
|
||||
|
||||
readonly VERSION="2.0.0"
|
||||
readonly VERSION="2.1.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"
|
||||
|
||||
@ -31,17 +32,35 @@ else
|
||||
CONFIG_FILE="$DEFAULT_CONFIG"
|
||||
fi
|
||||
|
||||
ssh_manager_version() {
|
||||
echo "ssh_manager $VERSION"
|
||||
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_help() {
|
||||
echo "Usage: ssh_manager command <command-specific-options>"
|
||||
sshm_help() {
|
||||
echo "Usage: sshm [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
|
||||
@ -55,7 +74,7 @@ ssh_manager_help() {
|
||||
EOF
|
||||
}
|
||||
|
||||
ssh_manager_list() {
|
||||
sshm_list() {
|
||||
local config_file="$CONFIG_FILE"
|
||||
echo -e "\nList of SSH hosts:"
|
||||
grep -E '^Host ' "$config_file" | awk '{print $2}' | grep -v '^#' | sort | nl
|
||||
@ -67,10 +86,10 @@ ssh_manager_list() {
|
||||
exit 0
|
||||
fi
|
||||
|
||||
ssh_manager_connect "$config_file" "$host"
|
||||
sshm_connect "$config_file" "$host"
|
||||
}
|
||||
|
||||
ssh_manager_connect() {
|
||||
sshm_connect() {
|
||||
local config_file="$1"
|
||||
local host="$2"
|
||||
if [[ -z "$host" ]]; then
|
||||
@ -84,15 +103,22 @@ ssh_manager_connect() {
|
||||
if [[ -n "$host_name" ]]; then
|
||||
ssh -F "$config_file" "$host_name"
|
||||
else
|
||||
echo "Invalid number." 1>&2
|
||||
echo "Error: Invalid host 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
|
||||
}
|
||||
|
||||
ssh_manager_ping() {
|
||||
sshm_ping() {
|
||||
local config_file="$1"
|
||||
local host="$2"
|
||||
if [[ -z "$host" ]]; then
|
||||
@ -114,7 +140,7 @@ ssh_manager_ping() {
|
||||
fi
|
||||
}
|
||||
|
||||
ssh_manager_view() {
|
||||
sshm_view() {
|
||||
local config_file="$1"
|
||||
local host="$2"
|
||||
if [[ -z "$host" ]]; then
|
||||
@ -133,7 +159,7 @@ ssh_manager_view() {
|
||||
echo "$host_info"
|
||||
}
|
||||
|
||||
ssh_manager_delete() {
|
||||
sshm_delete() {
|
||||
local config_file="$1"
|
||||
local host="$2"
|
||||
if [[ -z "$host" ]]; then
|
||||
@ -147,7 +173,7 @@ ssh_manager_delete() {
|
||||
echo "Host $host removed from SSH configuration."
|
||||
}
|
||||
|
||||
ssh_manager_add() {
|
||||
sshm_add() {
|
||||
local config_file="$1"
|
||||
local host="$2"
|
||||
local hostname
|
||||
@ -155,7 +181,9 @@ ssh_manager_add() {
|
||||
local port
|
||||
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
|
||||
read -p "Enter host name: " host
|
||||
if [[ -z "$host" ]]; then
|
||||
@ -176,10 +204,9 @@ ssh_manager_add() {
|
||||
read -p "Enter SSH port (default: 22): " port
|
||||
port=${port:-22}
|
||||
|
||||
read -p "Enter path to SSH key (default: ~/.ssh/id_rsa): " identity_file
|
||||
identity_file=${identity_file:-~/.ssh/id_rsa}
|
||||
read -p "Enter path to SSH key (default: $default_identity_file): " identity_file
|
||||
identity_file=${identity_file:-$default_identity_file}
|
||||
|
||||
# Add the new configuration to the file
|
||||
{
|
||||
echo ""
|
||||
echo "Host $host"
|
||||
@ -188,7 +215,7 @@ ssh_manager_add() {
|
||||
if [[ "$port" -ne 22 ]]; then
|
||||
echo " Port $port"
|
||||
fi
|
||||
if [[ "$identity_file" != ~/.ssh/id_rsa ]]; then
|
||||
if [[ "$identity_file" != "$default_identity_file" ]]; then
|
||||
echo " IdentityFile $identity_file"
|
||||
fi
|
||||
} >> "$config_file"
|
||||
@ -196,7 +223,7 @@ ssh_manager_add() {
|
||||
echo "Configuration for host $host added successfully."
|
||||
}
|
||||
|
||||
ssh_manager_edit() {
|
||||
sshm_edit() {
|
||||
local config_file="$1"
|
||||
local host="$2"
|
||||
if [[ -z "$host" ]]; then
|
||||
@ -231,7 +258,7 @@ ssh_manager_edit() {
|
||||
new_identity_file=${new_identity_file:-${current_identity_file:-~/.ssh/id_rsa}}
|
||||
|
||||
# Delete the old configuration
|
||||
ssh_manager_delete "$config_file" "$host"
|
||||
sshm_delete "$config_file" "$host"
|
||||
|
||||
# Add the new configuration
|
||||
{
|
||||
@ -306,6 +333,7 @@ context_create() {
|
||||
fi
|
||||
|
||||
touch "$CONFIG_DIR/$context"
|
||||
chmod 600 "$CONFIG_DIR/$context"
|
||||
echo "Context '$context' created."
|
||||
}
|
||||
|
||||
@ -330,39 +358,37 @@ context_delete() {
|
||||
fi
|
||||
}
|
||||
|
||||
ssh_manager_main() {
|
||||
sshm_main() {
|
||||
local config_file="$CONFIG_FILE"
|
||||
local command="$1"
|
||||
shift
|
||||
|
||||
if [[ -z $command ]]; then
|
||||
ssh_manager_version
|
||||
sshm_version
|
||||
echo
|
||||
ssh_manager_help
|
||||
sshm_help
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Check if command is a known command, otherwise treat it as a host to connect to
|
||||
case "$command" in
|
||||
"list")
|
||||
ssh_manager_list "$config_file"
|
||||
;;
|
||||
"connect")
|
||||
ssh_manager_connect "$config_file" "$@"
|
||||
sshm_list "$config_file"
|
||||
;;
|
||||
"ping")
|
||||
ssh_manager_ping "$config_file" "$@"
|
||||
sshm_ping "$config_file" "$@"
|
||||
;;
|
||||
"view")
|
||||
ssh_manager_view "$config_file" "$@"
|
||||
sshm_view "$config_file" "$@"
|
||||
;;
|
||||
"delete")
|
||||
ssh_manager_delete "$config_file" "$@"
|
||||
sshm_delete "$config_file" "$@"
|
||||
;;
|
||||
"add")
|
||||
ssh_manager_add "$config_file" "$@"
|
||||
sshm_add "$config_file" "$@"
|
||||
;;
|
||||
"edit")
|
||||
ssh_manager_edit "$config_file" "$@"
|
||||
sshm_edit "$config_file" "$@"
|
||||
;;
|
||||
"context")
|
||||
local subcommand="$1"
|
||||
@ -382,23 +408,30 @@ ssh_manager_main() {
|
||||
;;
|
||||
*)
|
||||
echo "Error: invalid context subcommand." 1>&2
|
||||
ssh_manager_help
|
||||
sshm_help
|
||||
exit 3
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
"version")
|
||||
ssh_manager_version
|
||||
sshm_version
|
||||
;;
|
||||
"help")
|
||||
ssh_manager_help
|
||||
sshm_help
|
||||
;;
|
||||
*)
|
||||
ssh_manager_help 1>&2
|
||||
exit 3
|
||||
# If command is not recognized, treat it as a host name to connect to
|
||||
sshm_connect "$config_file" "$command"
|
||||
esac
|
||||
}
|
||||
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user