ryanpitts1
ryanpitts1

Reputation: 894

Bash store output in variable, parse, and store a few values for later use

So, i'm trying to write a script to create a server on Rackspace Cloud. I've been able to get the script to successfully create a server. Although, after the server is created i need to get a couple of pieces of data out of the output and store them in a variable for later use in the same script (going to do a couple things on the new server after creation).

Here is the script i'm using to create the server:

#!/bin/bash

# Ask user to continue or not
read -p "You are about to setup a new Rackspace cloud server. Would you like to continue (y/n)? " cont
if [ "$cont" == "y" ]; then
    echo "Starting server setup script..."

    # Ask questions to get login creds
    read -p "Rackspace Username? " username
    read -p "Rackspace API Key? " apikey

    # Get Rackspace token
    echo "Getting token..."
    token=$(curl -s -X POST https://auth.api.rackspacecloud.com/v2.0/tokens \
        -d '{ "auth":{ "RAX-KSKEY:apiKeyCredentials":{ "username":"'"$username"'", "apiKey":"'"$apikey"'" } } }' \
        -H "Content-type: application/json" \
        | python -m json.tool \
        | python -c 'import sys, json; print json.load(sys.stdin)["access"]["token"]["id"]')
    echo "...done!"

    # Get Rackspace account id
    echo "Getting account id..."
    account=$(curl -s -X POST https://auth.api.rackspacecloud.com/v2.0/tokens \
        -d '{ "auth":{ "RAX-KSKEY:apiKeyCredentials":{ "username":"'"$username"'", "apiKey":"'"$apikey"'" } } }' \
        -H "Content-type: application/json" \
        | python -m json.tool \
        | python -c 'import sys, json; print json.load(sys.stdin)["access"]["token"]["tenant"]["id"]')
    echo "...done!"

    # Create a new Rackspace cloud server
    echo "Creating a new Rackspace cloud server..."
    serverpassword=$(curl -s -X POST https://dfw.servers.api.rackspacecloud.com/v2/$account/servers \
        -H "Content-Type: application/json" \
        -H "X-Auth-Token: $token" \
        -H "X-Auth-Project-Id: test-project" \
        -T server_build.json \
        | python -m json.tool \
        | python -c 'import sys, json; print json.load(sys.stdin)["server"]["adminPass"]')
    echo "...done!"

    echo "Your new server has been created!"

    read -p "Would you like to continue on to setting up your new server (y/n)? " cont2
    if [ "$cont2" == "y" ]; then
        exit
    else
        echo "Here is your root password for this server (you must write this down now): $serverpassword"
        exit
    fi
else
  echo "You have chosen not to setup a new Rackspace server."
  exit
fi

You can see that the token and account variables are being set and used in the creation curl command. The last curl command outputs something like this: (removed real values)

{
    "server": {
        "OS-DCF:diskConfig": "AUTO",
        "adminPass": "************",
        "id": "************",
        "links": [
            {
                "href": "https://dfw.servers.api.rackspacecloud.com/v2/...",
                "rel": "self"
            },
            {
                "href": "https://dfw.servers.api.rackspacecloud.com/...",
                "rel": "bookmark"
            }
        ]
    }
}

I'd like to be able to store both the adminPass and id in two different variables. How can i do this? I also know that by solving this issue i will be able to refactor the script when getting token and account values by using one curl command instead of two different ones.

Thanks for any help!

Upvotes: 3

Views: 4506

Answers (2)

ryanpitts1
ryanpitts1

Reputation: 894

I appreciate all the help. I was able to figure out what i needed to do. Maybe my original question wasn't worded in a way to get the best answers but hopefully this resolution will explain better what i was trying to accomplish.

I ended up using these commands for the 3rd curl command and to set the variables i needed:

serverinfo=$(curl -s https://dfw.servers.api.rackspacecloud.com/v2/731174/servers \
    -X POST \
    -H "Content-Type: application/json" \
    -H "X-Auth-Token: $token" \
    -H "X-Auth-Project-Id: test-project" \
    -T server_build.json | python -m json.tool)

serverpass=$(echo $serverinfo | python -c 'import sys, json; print json.load(sys.stdin)["server"]["adminPass"]')

This allowed me to run the curl command to setup the server and then grab the adminPass from the output of that command and set it to the serverpass variable for later use.

Using this concept i was also able to remove the 2nd curl command since it was a duplicate command just to set a variable. I can now use these commands to rub the curl once and set both variables from its output:

tokensinfo=$(curl -s -X POST https://auth.api.rackspacecloud.com/v2.0/tokens \
    -d '{ "auth":{ "RAX-KSKEY:apiKeyCredentials":{ "username":"'"$username"'", "apiKey":"'"$apikey"'" } } }' \
    -H "Content-type: application/json")

token=$(echo $tokensinfo | python -c 'import sys, json; print json.load(sys.stdin)["access"]["token"]["id"]')
account=$(echo $tokensinfo | python -c 'import sys, json; print json.load(sys.stdin)["access"]["token"]["tenant"]["id"]')

Hope that better explains what i was going for. Thanks for the help, it did help me arrive at this solution.

Upvotes: 2

Jayesh Bhoi
Jayesh Bhoi

Reputation: 25865

you can grab adminpass and id from the output and store in new variable like

var1=$(grep "adminPass" output.txt | cut -d: --complement -f1)
var2=$(grep "id" output.txt | cut -d: --complement -f1)

echo -e $var1
echo -e $var2

for here i assume the third command output you redirected to output.txt file.

Upvotes: 0

Related Questions