Forum Discussion
exporting a cli script
chatGPT and I worked on a bash script that should help...you can modify as needed. Tested to same system with a static second name to make sure the script worked, but the script will carry forward the same name as the script you're moving. The error checking is bash-helpful but not REST-helpful. Even a 4xx error will move forward, so that part is a task for you to work toward completion. The gist is here on Github where I might update it, but also pasted below for review. You can probably remove the echo of the downloaded script, I had that there while tweaking chatGPT recommendations and making sure I only got the script instead of the entire JSON payload...
#!/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"- JRahmJan 31, 2024
Admin
and for a breakdown on my process here and want to engage on this with the larger LinkedIn community, I wrote about how I worked out this script.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
