A Python script that automates SSH connections and executes predefined commands from a text file on remote servers.
- Establishes SSH connections to remote servers
- Executes commands from a configurable text file
- Supports both password and key-based authentication
- Supports parallel execution of commands for faster processing.
- Includes an option to enable legacy cryptographic algorithms for compatibility with older devices like Palo Alto firewalls.
- Provides detailed logging and error handling
- Command-line and configuration file interfaces
- Handles command failures gracefully
- Python 3.6+
- paramiko library
- Clone this repository:
git clone https://github.com/ronsnr/ssh-commands.git
cd ssh-commands- Install dependencies:
pip install -r requirements.txtThe script uses argparse for flexible command-line arguments.
python ssh_executor.py <hostname> <username> <commands_file> [OPTIONS]Options:
-p, --password <password>: SSH password (will prompt if not provided and key is not used).-k, --key_file <path>: Path to private key file.--port <port_number>: SSH port (default: 22).--parallel: Execute commands in parallel using a thread pool.--workers <number>: Number of parallel workers (threads) to use. Default is based on CPU count.--legacy-crypto: Enable legacy cryptographic algorithms for compatibility with older devices (e.g., Palo Alto firewalls).
Examples:
Using password authentication (password will be prompted):
python ssh_executor.py 192.168.1.100 myuser commands.txtUsing SSH key authentication:
python ssh_executor.py 192.168.1.100 myuser commands.txt -k ~/.ssh/id_rsaUsing SSH key authentication:
python ssh_executor.py 192.168.1.100 myuser commands.txt '' ~/.ssh/id_rsaUsing custom port:
python ssh_executor.py 192.168.1.100 myuser commands.txt '' ~/.ssh/id_rsa 2222- Create a configuration file by running:
python ssh_executor_config.py- Edit the generated
config.jsonfile with your connection details:
{
"hostname": "your-server-ip-or-hostname",
"username": "your-username",
"password": "your-password-or-leave-empty-for-key-auth",
"key_filename": "/path/to/your/private/key",
"port": 22,
"commands_file": "commands.txt"
}- Run the script:
python ssh_executor_config.pyThe commands.txt file contains the commands to execute on the remote server. Format rules:
- One command per line
- Lines starting with
#are treated as comments and ignored - Empty lines are ignored
- Commands are executed in the order they appear
Example commands.txt:
# System information
whoami
hostname
uname -a
# Directory listing
ls -la
pwd
# System resources
df -h
free -mThe script will prompt for the password during execution.
Set the key_filename field in config.json to point to your private key file, or pass it as a command line argument. Leave the password field empty when using key authentication.
The script includes comprehensive error handling for:
- SSH connection failures
- Authentication errors
- Command execution failures
- File not found errors
- Network connectivity issues
All operations are logged with timestamps and appropriate log levels.
- Store SSH keys securely with appropriate file permissions (600)
- Avoid storing passwords in configuration files in production
- Use key-based authentication when possible
- Consider using SSH agent for key management
- Validate commands before execution to prevent security issues
The script provides:
- Real-time command execution status
- STDOUT and STDERR output from each command
- Success/failure count summary
- Detailed logging information
2024-01-15 10:30:00,123 - INFO - Connecting to 192.168.1.100:22 as myuser
2024-01-15 10:30:01,456 - INFO - SSH connection established successfully
2024-01-15 10:30:01,457 - INFO - Loaded 5 commands from commands.txt
2024-01-15 10:30:01,458 - INFO - Executing command 1/5
2024-01-15 10:30:01,459 - INFO - Executing command: whoami
2024-01-15 10:30:01,678 - INFO - Command executed successfully (exit code: 0)
STDOUT:
myuser
2024-01-15 10:30:02,180 - INFO - Execution complete: 5/5 commands successful
All commands executed successfully
2024-01-15 10:30:02,181 - INFO - SSH connection closed
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
This project is open source. See the repository for license details.