gittech. site

for different kinds of informations and explorations.

SSH_commander: Run sequential command(s) on a group of SSH servers

Published at
Feb 5, 2025

SSH Commander

SSH Commander is a powerful, colorful command-line tool for executing commands across multiple SSH servers simultaneously. It supports both password and key-based authentication, custom ports, and can execute both single commands and command files.

SSH Commander Demo

Features

  • πŸ”‘ Supports both password and key-based authentication
  • 🌈 Colorized output for better readability
  • πŸ“ Execute commands from files
  • πŸ”„ Interactive server management
  • πŸ”’ Secure password handling (never shown in terminal)
  • πŸš€ Single binary deployment
  • βš™οΈ YAML-based configuration
  • πŸ“Š Real-time output streaming
  • ⌨️ Graceful interrupt handling (Ctrl+C support)

System Requirements

Linux

Ubuntu/Debian:

  • Modern systems: libffi8 and libssl3
  • Older systems: libffi7 and libssl1.1

CentOS/RHEL:

  • Modern systems: libffi-8 and openssl-3
  • Older systems: libffi-7 and openssl-1.1

Windows

  • Microsoft Visual C++ Redistributable

macOS

  • No additional requirements

Installation

Option 1: Debian Package (Ubuntu/Debian)

  1. Download the latest .deb package from the releases page
  2. Install using dpkg:
sudo dpkg -i ssh-commander_*.deb

If you encounter any dependency errors during installation, run:

sudo apt -f install

This will automatically install any missing dependencies and complete the installation.

Option 2: Using Pre-built Binaries

  1. Download the latest release for your platform from the releases page
  2. Extract the archive
  3. Run the installation script:
./install.sh  # For macOS/Linux
.\install.ps1  # For Windows (Run as Administrator)

Option 3: Building from Source

  1. Clone the repository:
git clone https://github.com/AthenaNetworks/ssh_commander.git
cd ssh_commander
  1. Create a virtual environment and install dependencies:
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
  1. Build the executable:
./build_all.sh

Configuration

SSH Commander stores server configurations in ~/.config/ssh-commander/servers.yaml. This file is automatically created when you add your first server.

Configuration Format

The configuration file uses YAML format and supports both key-based and password authentication:

# Key-based authentication (recommended)
- hostname: web1.example.com
  username: admin
  key_file: ~/.ssh/id_rsa  # Path to your SSH key
  port: 22  # Optional, defaults to 22
  tags: [prod, web]  # Optional, defaults to ['default']

# Password authentication
- hostname: db1.example.com
  username: dbadmin
  password: your_secure_password  # Not recommended for production use
  port: 2222
  tags: [prod, db]  # Optional server tags

Security Notes

⚠️ Important Security Warning:

  • Passwords are stored in plaintext and are NOT secure
  • Anyone with access to your config file can see the passwords
  • This includes backup systems, cloud sync, etc.

βœ… Recommended Approach:

  • Use key-based authentication instead
    • Generate an SSH key: ssh-keygen -t ed25519
    • Copy to server: ssh-copy-id user@hostname
    • Use key_file: ~/.ssh/id_ed25519 in config

Configuration Security

  • Config file is searched for in the following order:
    1. Path specified by --config argument
    2. servers.yaml in the same directory as the executable
    3. ~/.config/ssh-commander/servers.yaml
  • File permissions are set to user-only read/write (600)
  • SSH key paths support ~ expansion to your home directory

Usage

Managing Servers

  1. Add a new server interactively:
ssh-commander add
  1. List configured servers:
ssh-commander list
  1. Remove a server:
ssh-commander remove web1.example.com

Executing Commands

  1. Run a single command on all servers:
ssh-commander exec -c "uptime"
  1. Run a command on servers with specific tags:
ssh-commander exec -c "uptime" -t "prod,web"
  1. Run multiple commands from a file:
ssh-commander exec -f commands.txt
  1. Run commands from file on specific tags:
ssh-commander exec -f commands.txt -t "staging"

Example commands.txt:

uptime
df -h
free -m
who
  1. Use a different config file:
ssh-commander --config prod-servers.yaml exec -c "docker ps"

Real-world Examples

  1. Check system status across all servers:
ssh-commander exec -c "systemctl status nginx"
  1. Deploy updates:
ssh-commander exec -c "sudo apt update && sudo apt upgrade -y"
  1. Monitor disk space:
ssh-commander exec -c "df -h / /var /home"
  1. Check running Docker containers:
ssh-commander exec -c "docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'"
  1. Execute a maintenance script:
# maintenance.txt
systemctl status nginx
df -h
free -m
find /var/log -type f -size +100M -exec ls -lh {} \;

# Run maintenance checks
ssh-commander exec -f maintenance.txt

Output Formatting

SSH Commander uses colors to make output more readable:

  • πŸ”΅ Server names are highlighted in blue
  • 🟒 Successful output is shown in green
  • πŸ”΄ Errors are displayed in red
  • 🟣 Command execution status in purple

Security Considerations

  1. Password Storage:

    • Passwords in the config file should be treated with care
    • Consider using key-based authentication when possible
    • Use appropriate file permissions for your config file: chmod 600 servers.yaml
  2. SSH Keys:

    • Key files should have proper permissions: chmod 600 ~/.ssh/id_rsa
    • Consider using different keys for different server groups
  3. Network Security:

    • Be mindful of firewalls and network policies
    • Use custom ports if needed
    • Consider using jump hosts for isolated networks

Contributing

  1. Fork the repository
  2. Create your feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add amazing feature'
  4. Push to the branch: git push origin feature/amazing-feature
  5. Open a Pull Request

License

This project is licensed under the GNU General Public License v3.0 (GPL-3.0) - see the LICENSE file for details.

Key points of the GPL-3.0 license:

  • βœ… You can use this software for commercial purposes
  • βœ… You can modify the source code
  • βœ… You can distribute your modifications
  • ⚠️ You must disclose the source code of your modifications
  • ⚠️ You must license your modifications under the GPL-3.0
  • ⚠️ You must state the significant changes you made

Support

If you encounter any issues or have questions:

  1. Check the Issues page
  2. Create a new issue with detailed information about your problem
  3. Include your OS version and Python version when reporting bugs