Shell Script Automating Oracle Wallet for Autonomous Database
Managing Oracle Autonomous Database (ADB) connections often involves configuring wallet files to ensure secure access. This guide will walk you through creating a robust shell script to automate the process of configuring Oracle wallet files, setting environment variables, and dynamically updating configuration files.
Why Automate?
Setting up an Oracle wallet involves multiple repetitive tasks:
- Extracting wallet files to a specific directory.
- Updating
sqlnet.orawith the correct directory. - Reading
tnsnames.orato configureTWO_TASKdynamically.
Automating these steps not only saves time but also reduces errors.
The Shell Script
Here is the complete shell script to automate the Oracle wallet configuration:
#!/bin/bash
# Rohit Misri (Author)
# Prompt for wallet zip file name and path
read -p "Enter wallet zip file name with path (absolute or relative, e.g., /Users/rmisri/Downloads/temp_wallet/test/Wallet_USPQAK.zip): " WALLET_ZIP
read -p "Enter wallet directory path (e.g., /Users/rmisri/Downloads/temp_wallet/test): " WALLET_DIR
# Resolve relative paths to absolute paths
if [[ "$WALLET_ZIP" != /* ]]; then
WALLET_ZIP="$(pwd)/$WALLET_ZIP"
fi
if [[ "$WALLET_DIR" != /* ]]; then
WALLET_DIR="$(pwd)/$WALLET_DIR"
fi
# Debugging message: show where it's looking for the file
echo "Checking for wallet zip file at: $WALLET_ZIP"
# Check if wallet zip file exists
if [ ! -f "$WALLET_ZIP" ]; then
echo "Error: Wallet zip file not found at $WALLET_ZIP."
exit 1
fi
# Create the wallet directory if it doesn't exist
mkdir -p "$WALLET_DIR"
# Unzip the wallet file
echo "Unzipping wallet file: $WALLET_ZIP"
unzip -o "$WALLET_ZIP" -d "$WALLET_DIR"
if [ $? -eq 0 ]; then
echo "Wallet unzipped successfully to $WALLET_DIR"
else
echo "Error unzipping wallet file."
exit 1
fi
# Define TNS_ADMIN environment variable
export TNS_ADMIN="$WALLET_DIR"
echo "TNS_ADMIN set to $TNS_ADMIN"
# Update sqlnet.ora with the wallet directory
SQLNET_FILE="$WALLET_DIR/sqlnet.ora"
if [ -f "$SQLNET_FILE" ]; then
# Replace the entire WALLET_LOCATION line
sed -i.bak "s|WALLET_LOCATION = .*|WALLET_LOCATION = (SOURCE = (METHOD = file) (METHOD_DATA = (DIRECTORY=$WALLET_DIR)))|g" "$SQLNET_FILE"
echo "sqlnet.ora updated with wallet directory: $WALLET_DIR"
else
echo "Error: sqlnet.ora not found in $WALLET_DIR."
exit 1
fi
# Extract _high value from tnsnames.ora
TNSNAMES_FILE="$WALLET_DIR/tnsnames.ora"
if [ -f "$TNSNAMES_FILE" ]; then
HIGH_VALUE=$(grep -E "^[a-zA-Z0-9_]+_high" "$TNSNAMES_FILE" | awk '{print $1}' | head -n 1)
if [ -n "$HIGH_VALUE" ]; then
# Set TWO_TASK dynamically
export TWO_TASK="$HIGH_VALUE"
echo "TWO_TASK set to $TWO_TASK"
else
echo "Error: No '_high' entry found in tnsnames.ora."
exit 1
fi
else
echo "Error: tnsnames.ora not found in $WALLET_DIR."
exit 1
fi
# Confirmation message
echo "Environment setup complete."
Script Breakdown
1. User Input
The script prompts the user for:
- The path to the wallet zip file.
- The directory where the wallet should be extracted.
2. Path Resolution
The script resolves relative paths to absolute paths for better reliability:
if [[ "$WALLET_ZIP" != /* ]]; then
WALLET_ZIP="$(pwd)/$WALLET_ZIP"
fi
3. File Validation
Before proceeding, the script checks if the wallet zip file exists:
if [ ! -f "$WALLET_ZIP" ]; then
echo "Error: Wallet zip file not found at $WALLET_ZIP."
exit 1
fi
4. Unzipping Wallet
The script uses the unzip command to extract the wallet files into the specified directory:
unzip -o "$WALLET_ZIP" -d "$WALLET_DIR"
5. **Updating **sqlnet.ora
The sqlnet.ora file is updated to include the correct wallet directory:
sed -i.bak "s|WALLET_LOCATION = .*|WALLET_LOCATION = (SOURCE = (METHOD = file) (METHOD_DATA = (DIRECTORY=$WALLET_DIR)))|g" "$SQLNET_FILE"
6. **Extracting _high from **tnsnames.ora
The _high value is dynamically extracted and set as TWO_TASK:
HIGH_VALUE=$(grep -E "^[a-zA-Z0-9_]+_high" "$TNSNAMES_FILE" | awk '{print $1}' | head -n 1)
7. Environment Setup Complete
Finally, the script confirms the successful setup:
echo "Environment setup complete."
How to Use
- Save the script as
set_wallet.sh. - Make it executable:
chmod +x set_wallet.sh - Run the script:
./set_wallet.sh - Follow the prompts to provide the wallet zip file and extraction directory.
Example Output
Checking for wallet zip file at: /Users/rmisri/Downloads/temp_wallet/test/Wallet_USPQAK.zip
Unzipping wallet file: /Users/rmisri/Downloads/temp_wallet/test/Wallet_USPQAK.zip
Wallet unzipped successfully to /Users/rmisri/Downloads/temp_wallet/test
TNS_ADMIN set to /Users/rmisri/Downloads/temp_wallet/test
sqlnet.ora updated with wallet directory: /Users/rmisri/Downloads/temp_wallet/test
TWO_TASK set to uspqa_high
Environment setup complete.
Conclusion
This script simplifies Oracle wallet setup by automating the configuration process. By resolving paths, updating configuration files, and dynamically setting environment variables, it ensures a seamless and error-free experience. Save time and reduce manual effort with this automation solution!
Author: Rohit Mishri
