Introducing SOC Insights for BloxOne Threat Defense: Boost your SOC efficiency with AI-driven insights to eliminate manual work and accelerate investigation and response times. Read the blog announcement here.

API & Integration, DevOps,NetOps,SecOps

Reply

curl giving different output when out is assigned to a var in a bash script

[ Edited ]
Techie
Posts: 7
5281     0

I am running the following cmd:

 

curl -k1 -u usernameSmiley Tongueassword -H "Content-Type: application/json" -X GET https://xxx.xxx.xxx.xxx/wapi/v2.5/extensibleattributedef? -d '{"name":"ea_name"}'

 

replacing the appropriate fields to obtain the object_ID for. This cmd outputs a json list with the object_ID and other information.

 

However, when I run this same cmd inside a bash script using the following syntax:

 

output=$(curl -k1 -u "$userID":"$passwd" -H "Content-Type: application/json" -X GET https://"$ip"/wapi/v2.5/extensibleattributedef? -d '{"name":"$ea_name"}')

 

I get the transfer stats from curl instead of the json list.

 

Does anyone know why the output is different when assigning the output to a variable? How can I get the json list stored in $output?

Re: curl giving different output when out is assigned to a var in a bash script

Moderator
Moderator
Posts: 287
5281     0

Use the curl "-s" or "--silent" option to supress the stats.

 

output=$(curl -s -k1 -u "$userID":"$passwd" -H "Content-Type: application/json" -X GET https://"$ip"/wapi/v2.5/extensibleattributedef? -d '{"name":"$ea_name"}')

Re: curl giving different output when out is assigned to a var in a bash script

Techie
Posts: 7
5281     0

The -s did suppress the stats, but I'm still not getting anything stored $output

Re: curl giving different output when out is assigned to a var in a bash script

Moderator
Moderator
Posts: 287
5281     0

This method is super finicky about single quotes, double quotes, and spaces.  I got it to work if the EA name doesn't have a space but could not get it to encode a space properly, for cases where the EA name has a space.

 

I'd reccomend going with Perl or Python instead if possible.  If you must use Bash + Curl, maybe work with the libcurl methods instead of redirected output from an external command.

 

Here's my working example.  I kept the extra "echo $ea_name" for troubleshooting.

 

#!/bin/bash

userID="admin"
passwd="infoblox"
ip="192.168.1.2"
ea_name="Role"

echo $ea_name

output=$(curl -s -k1 -u admin:infoblox -X GET https://$ip/wapi/v2.5/extensibleattributedef -H Content-Type:application/json -d {\"name\":\"$ea_name\"})

echo $output

And here's the output:

Role
[ { "_ref": "extensibleattributedef/b25lLmV4dGVuc2libGVfYXR0cmlidXRlc19kZWYkLlJvbGU:Role", "default_value": null, "name": "Role", "type": "STRING" } ]

Re: curl giving different output when out is assigned to a var in a bash script

Techie
Posts: 7
5281     0

Ok, I've done some debugging and have discovered the following:

 

I took the output of the curl cmd that was run from the cmdline, inserted it into a variable in another script and attempted to print that contents of that variable and got [].

 

There is something in the json list that is either preventing the json list from being stored or it is interferring with the echo cmd. I did try printf in place of echo and got the same [] output. I also tried writing to a file and got, you guessed it, [] in the file.

 

So, I'm changing my question to how do I properly store a multi-line json list in bash and how do I print it out.

 

 

Re: curl giving different output when out is assigned to a var in a bash script

[ Edited ]
Techie
Posts: 7
5281     0

Thanks @MRichard

 

I copied and pasted your script and I got the same result as earlier:

 

Role

[]

 

Can you tell me what distribution and version you ran this under?

 

I'm running RHEL-7.

Re: curl giving different output when out is assigned to a var in a bash script

Moderator
Moderator
Posts: 287
5281     0

You would need an EA called "Role" for it to work exactly.  That's not a default EA, so you should check on which
EAs are configured in the grid.

 

I ran it successfully on Mac OSX Mojave, 10.14.6 and on Centos 7.6.1810.

 

 

Re: curl giving different output when out is assigned to a var in a bash script

[ Edited ]
Techie
Posts: 7
5281     0

Understood.

 

I used one of our existing EA that does not contain a space. I just put Role in the post for simplicity.

 

If that worked in CentOS 7.6, then it should work in RHEL-7.6.

 

Weird.....ok, I'll give Python a go.

 

Thanks for your assistance.

Showing results for 
Search instead for 
Did you mean: 

Recommended for You