Many changes

This commit is contained in:
Gu1llaum-3 2025-03-15 15:24:57 +01:00
parent 96a83d119c
commit ea27b28f30

View File

@ -31,15 +31,16 @@ else
CONFIG_FILE="$DEFAULT_CONFIG" CONFIG_FILE="$DEFAULT_CONFIG"
fi fi
ssh_manager_version() { sshm_version() {
echo "ssh_manager $VERSION" echo "sshm $VERSION"
} }
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 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
@ -55,7 +56,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 +68,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 +85,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 +122,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 +141,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 +155,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
@ -196,7 +204,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 +239,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
{ {
@ -331,39 +339,40 @@ 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") "connect")
ssh_manager_connect "$config_file" "$@" sshm_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"
@ -383,23 +392,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