Search code examples
linuxbashcurlscriptingsystem-administration

how to use bash output as variable


I am creating a job that would first initiate an API checking what is the next available ip in a subnet with some parameters.

Then i want to run "ping" check on the output (that is an IP), then also telnet to ports 22 80 3389 on the same output,

how can i insert all the CURL output in to a variable so i can continue the script running ping and telnet checks before giving an indication that the ip is really "Available" - i have tried many failed syntaxes in the last 2 days :) thank you.

so:

#!/bin/bash
curl --stderr -i -H "Accept: application/json" -X POST -d "query=ip" -d "string=$1" -u 'username:password' https://device42.xxxxxxxxx/api/1.0/search/ --insecure | awk '{print "Avaliable",$9,$10,$11}'
[[ -z "$1" ]] && echo "Please Enter IP" ||

the api returns this atm:

available: Yes ip: 10.120.34.11


Solution

  • curl --stderr -i is invalid. You might want curl --stderr - -i.

    Try substitution ` with command output in any shell:

    #!/bin/bash
    var=`curl --stderr - -i -H "Accept: application/json" -X POST -d "query=ip" -d "string=$1" -u 'username:password' https://device42.xxxxxxxxx/api/1.0/search/ --insecure | awk '{print "Avaliable",$9,$10,$11}'`
    [[ -z "$var" ]] && echo "Please Enter IP" ||
    

    Or with another command output substitution syntax

    #!/bin/bash
    var=$(curl --stderr - -i -H "Accept: application/json" -X POST -d "query=ip" -d "string=$1" -u 'username:password' https://device42.xxxxxxxxx/api/1.0/search/ --insecure | awk '{print "Avaliable",$9,$10,$11}')
    [[ -z "$var" ]] && echo "Please Enter IP" ||