Created
January 31, 2024 20:36
-
-
Save f5-rahm/57d32f0691fa40479db375799f978c74 to your computer and use it in GitHub Desktop.
BIG-IP cli script transfer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check for the correct number of arguments | |
if [ "$#" -ne 3 ]; then | |
echo "Usage: $0 <script_name> <source_IP> <destination_IP>" | |
exit 1 | |
fi | |
script_name="$1" | |
source_ip="$2" | |
destination_ip="$3" | |
# Prompt for source BIG-IP credentials | |
read -p "Enter source BIG-IP username: " source_username | |
read -sp "Enter source BIG-IP password: " source_password | |
echo | |
# Prompt for destination BIG-IP credentials | |
read -p "Enter destination BIG-IP username: " destination_username | |
read -sp "Enter destination BIG-IP password: " destination_password | |
echo | |
# Define the source and destination URLs | |
source_url="https://$source_ip/mgmt/tm/cli/script/$script_name" | |
destination_url="https://$destination_ip/mgmt/tm/cli/script/" | |
# Download the script from the source BIG-IP and store it in a variable | |
echo "Downloading $script_name from $source_ip..." | |
downloaded_script=$(curl -sk -u "$source_username:$source_password" "$source_url" | jq -r '.apiAnonymous') | |
escaped_script=$(echo "$downloaded_script" | sed 's/"/\\"/g') | |
echo $downloaded_script | |
# Check if the download was successful | |
if [ $? -ne 0 ]; then | |
echo "Error: Failed to download $script_name from $source_ip" | |
exit 1 | |
fi | |
# Upload the script to the destination BIG-IP using the "apiAnonymous" sourcePath | |
echo "Uploading $script_name to $destination_ip..." | |
curl -sk -X POST -u "$destination_username:$destination_password" -H "Content-Type: application/json" -d "{\"name\": \"$script_name\",\"apiAnonymous\": \"$escaped_script\"}" "$destination_url" | |
# Check if the upload was successful | |
if [ $? -ne 0 ]; then | |
echo "Error: Failed to upload $script_name to $destination_ip" | |
exit 1 | |
fi | |
echo "Script $script_name successfully transferred from $source_ip to $destination_ip" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment