sshtunrun
sshtunrun is a powerful Go-based tool that creates an SSH tunnel and executes a command through it. It simplifies the process of running commands on remote hosts through a bastion server, making it ideal for secure remote execution in various environments.
Features
- Creates an SSH tunnel through a bastion host
- Executes a specified command after the local port is bound
- Automatic reconnection on tunnel failure (can be disabled)
- Graceful handling of termination signals
Installation
To install sshtunrun, ensure you have Go installed on your system, then run:
go install github.com/deorus/sshtunrun@latest
Usage
sshtunrun can be configured using command-line flags or environment variables. Here's the basic syntax:
sshtunrun [flags] -- [command to execute]
Flags
-u: SSH user for bastion host (required)
-b: SSH bastion hostname (required)
-p: SSH bastion port (default: 22)
-i: SSH private key path
--target-host: Target host to connect to through tunnel (required)
--target-port: Target port to connect to (required)
--local-port: Local port to bind to (required)
--no-reconnect: Disable automatic reconnection
Environment Variables
You can also use environment variables to configure sshtunrun:
SSHTUN_RUN_USER: SSH user for bastion host
SSHTUN_RUN_BASTION_HOST: SSH bastion hostname
SSHTUN_RUN_BASTION_PORT: SSH bastion port
SSHTUN_RUN_PRIVATE_KEY_PATH: SSH private key path
SSHTUN_RUN_TARGET_HOST: Target host to connect to through tunnel
SSHTUN_RUN_TARGET_PORT: Target port to connect to
SSHTUN_RUN_LOCAL_BIND_PORT: Local port to bind to
SSHTUN_RUN_PRIVATE_KEY: Private key data, for in-memory keys (they will be stored temporarily as a file, not recommended)
Example
Here's an example of how to use sshtunrun:
sshtunrun -u jumpuser -b bastion.example.com -i ~/.ssh/id_rsa --target-host db.internal --target-port 5432 --local-port 15432 -- psql -h localhost -p 15432 -U dbuser -d mydb
This command will:
- Create an SSH tunnel through
bastion.example.com using the user jumpuser
- Connect to
db.internal:5432 through the tunnel
- Bind the tunnel to local port 15432
- Execute the psql command to connect to the database
Security Considerations
- Always use strong, unique SSH keys for authentication.
- Be cautious when using the
SSHTUN_RUN_PRIVATE_KEY environment variable, as it may expose the key to other processes.
- Ensure that the bastion host and target systems are properly secured.
Contributing
Contributions to sshtunrun are welcome! Please feel free to submit pull requests, create issues, or suggest improvements.
License
sshtunrun is released under the MIT License. See the LICENSE.txt file for details.