Feature: Optional ping check

- Make ping optional with 'sshm list --ping' instead of default behavior
- Update documentation
This commit is contained in:
Guillaume Archambault 2025-08-28 14:48:18 +02:00
parent 411199595f
commit 7c6c77d63d
2 changed files with 47 additions and 10 deletions

View File

@ -4,8 +4,8 @@ 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.
- List all SSH hosts in the configuration file (with optional ping check).
- Connect to an SSH host by name or number from the list.
- View the configuration details of a specific SSH host.
- Add a new SSH host configuration.
- Edit an existing SSH host configuration.
@ -26,7 +26,7 @@ SSH Manager (sshm) is a bash script that simplifies and automates the management
1. Clone the repository:
```bash
git clone https://github.com/yourusername/sshm.git
git clone https://github.com/Gu1llaum-3/sshm.git
cd sshm
```
@ -50,12 +50,25 @@ SSH Manager (sshm) is a bash script that simplifies and automates the management
sshm list
```
To check host availability with ping (may be slower if hosts are unreachable):
```bash
sshm list --ping
```
### Connect to an SSH Host
```bash
sshm <host>
```
You can also connect by selecting a number from the `sshm list` output:
```bash
sshm list
# Select a number when prompted, e.g., type "1" to connect to the first host
```
### View SSH Host Configuration
```bash
@ -126,6 +139,16 @@ Deletes the specified SSH configuration context.
## Example
### Listing and Connecting to SSH Hosts
```bash
# Quick list without ping check (fast)
sshm list
# List with availability check (slower if hosts are down)
sshm list --ping
```
### Adding a New SSH Host
```bash

View File

@ -26,7 +26,7 @@ readonly BLUE='\033[0;34m'
readonly BOLD='\033[1m'
readonly NC='\033[0m' # No Color
readonly VERSION="2.1.0"
readonly VERSION="2.1.1"
readonly CONFIG_DIR="${HOME}/.config/sshm"
readonly DEFAULT_CONFIG="${HOME}/.ssh/config"
readonly CURRENT_CONTEXT_FILE="${CONFIG_DIR}/.current_context"
@ -73,7 +73,7 @@ sshm_help() {
echo -e "${BLUE}${BOLD}Commands:${NC}"
cat<<EOF | column -t -s $'\t'
<host> Connect directly to SSH host by name
list List SSH hosts and prompt for connection
list [--ping] List SSH hosts and prompt for connection (--ping to check availability)
ping <name> Ping an SSH host to check availability
view <name> Check configuration of host
delete <name> Delete an SSH host from the configuration
@ -89,6 +89,12 @@ EOF
sshm_list() {
local config_file="$CONFIG_FILE"
local do_ping=false
# Check for --ping option
if [[ "$1" == "--ping" ]]; then
do_ping=true
fi
# Check if the file exists and is not empty
if [[ ! -s "$config_file" ]]; then
@ -110,7 +116,11 @@ sshm_list() {
echo -e "\n${BLUE}${BOLD}Context: ${NC}${context_name}"
fi
echo -e "\n${BLUE}${BOLD}List of SSH hosts:${NC}"
if [[ "$do_ping" == true ]]; then
echo -e "\n${BLUE}${BOLD}List of SSH hosts (with ping):${NC}"
else
echo -e "\n${BLUE}${BOLD}List of SSH hosts:${NC}"
fi
# Create a temporary file to store results
local tmp_file
@ -126,10 +136,14 @@ sshm_list() {
continue
fi
if ping -c 1 -W 1 "$hostname" &> /dev/null; then
echo -e "${GREEN}${NC} $host ($hostname)" >> "$tmp_file"
if [[ "$do_ping" == true ]]; then
if ping -c 1 -W 1 "$hostname" &> /dev/null; then
echo -e "${GREEN}${NC} $host ($hostname)" >> "$tmp_file"
else
echo -e "${RED}${NC} $host ($hostname)" >> "$tmp_file"
fi
else
echo -e "${RED}${NC} $host ($hostname)" >> "$tmp_file"
echo -e "$host ($hostname)" >> "$tmp_file"
fi
done < <(grep -E '^Host ' "$config_file" | grep -v '^#' | sort)
@ -517,7 +531,7 @@ sshm_main() {
# Check if command is a known command, otherwise treat it as a host to connect to
case "$command" in
"list")
sshm_list
sshm_list "$@"
;;
"ping")
sshm_ping "$CONFIG_FILE" "$@"