The oci-bastion-ssh-connect.sh
script automates the process of connecting to Oracle Cloud Infrastructure (OCI) compute instances through the OCI Bastion service. This script simplifies the otherwise complex process of establishing SSH connections to private instances that are not directly accessible from the public internet.
- OCI CLI: Must be installed and configured on your system.
- SSH Keys: A valid SSH key pair (public and private keys).
- OCI Bastion Service: A configured Bastion service in your OCI environment.
- Configuration File: A
.env
file with the required parameters.
The script uses a .env
file for configuration. Create this file in the same directory as the script with the following parameters:
# Configuration - Update these values with your actual information
BASTION_ID=ocid1.bastion.oc1.region.xxxx # Your Bastion service OCID
INSTANCE_ID=ocid1.instance.oc1.region.xxxx # Target instance OCID
PRIVATE_KEY=$HOME/.ssh/id_ed25519 # Path to your SSH private key
PUBLIC_KEY=$HOME/.ssh/id_ed25519.pub # Path to your SSH public key
TARGET_IP=10.0.1.xxx # Private IP of target instance
TARGET_PORT=22 # SSH port (usually 22)
USERNAME=ubuntu # Username on target instance
REGION=region-name # Your OCI region (e.g., us-ashburn-1)
SESSION_TTL=10800 # Session time-to-live in seconds (3 hours)
SESSION_MIN_REMAINING_SECONDS=600 # Min remaining time (10 minutes)
-
Session Management:
- Automatically checks for existing active Bastion sessions
- Calculates and displays remaining session time
- Offers to reuse existing sessions or create new ones
-
User Experience:
- Visual progress indicators (spinner)
- Informative emojis and formatted messages
- Clean error handling and exit management
-
Security:
- Automatic session cleanup on exit
- Secure handling of SSH credentials
- Session time limit enforcement
- Clone the repository containing the script.
- Create a
.env
file with your configuration. - Make the script executable:
chmod +x oci-bastion-connect.sh
- Run the script:
./oci-bastion-connect.sh
- The script checks for existing active Bastion sessions.
- If a valid session exists, it offers to reuse it.
- If no session exists or the user declines to reuse an existing one, it creates a new session.
- The script waits for the session to become active, displaying a spinner.
- Once active, it establishes an SSH connection to the target instance.
- When the user exits the SSH session (Ctrl+C), the script cleans up by deleting the Bastion session.
- Session creation failures: The script will display an error message if it cannot create a Bastion session.
- Session activation timeout: If a session takes too long to activate, the script will still attempt to connect.
- Interruption handling: Pressing Ctrl+C will properly clean up resources and exit.
- The script works with both Linux and macOS (handles date command differences).
- Session time-to-live (TTL) defaults to 3 hours (10800 seconds).
- Minimum session time remaining defaults to 10 minutes (600 seconds).