-1

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

Unixcited
  • 3
  • 2
  • This is the second question in a short period that is looking for `variable=$(command)`. Most tutorials that i've seen (e.g. https://guide.bash.academy/expansions/ or https://www.learnshell.org/en/Variables or many others) seem to cover this subject. – Ljm Dullaart Jun 28 '22 at 17:49

1 Answers1

0

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" ||
273K
  • 29,503
  • 10
  • 41
  • 64
  • 1
    Every POSIX compatible shell should support `$( ... )`, this is not specific to bash. – Bodo Jun 28 '22 at 17:55
  • thank you. when running this i get no output - ----------- #!/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" || ----------- adding echo "$var" dosent work – Unixcited Jun 28 '22 at 20:04
  • What output do you get if remove `| awk '{print "Avaliable",$9,$10,$11}'`? – 273K Jun 28 '22 at 20:18
  • thank you. i got it! :) the issue was with the structure not the awk. much appreciated now i run-----------#!/bin/bash var=`curl --stderr -i -H "Accept: application/json" -X POST -d "query=ip" -d "string=$1" -u 'user:password' https://device42.xxxxxxxxxxx/api/1.0/search/ --insecure` if [[ -z "$1" ]]; then echo "Please Enter IP" exit 1 else echo $var | awk '{print $8,$9,$10,$11}' | tr -d '{}()[]",' fi – Unixcited Jun 28 '22 at 20:24