Forum Discussion

cgwin12's avatar
cgwin12
Icon for Nimbostratus rankNimbostratus
Jan 31, 2024

exporting a cli script

I need to export a cli script from one F5 to another F5. They are separate appliances, not synched to each other. 

I thought I could run 'tmsh' then 'list cli script <scriptname>'. It shows the output, I copy it all to notepad. 

I went to the target box, ran tmsh, then 'cli script create <scriptname>'. I pasted between the { and the }. I get a syntax error upon saving. 

Has anyone else tried this? Would it be easier to winscp from one box to the other? What's the file directory for scripts on F5, that I could locate in WinSCP? Thanks 

4 Replies

  • 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"
  • Hi , 

    Do you mean a ".sh" file contains script ? 
    if so do that using winSCP  , or if there is a connectivity between the two devices you can use "scp" protocol, as below: 

    scp -p <local_filename> <username>@<server>:<remote_path>
    
    For Example : 
    
    scp -p myfile.bin root@10.90.101.50:/var/tmp/

     

    Have a look here