I'm trying to get a specific json field from a cURL GET command in a bash script from an Elasticsearch index. To to this I have set up a script to pass variables into the cURL command. Hard coding the values gives the proper response so I just need to figure out how to use variables so I can get different responses for different purposes.
This is the desired outcome and the current working command:
docs=`curl -k -u admin:hunter1 -HContent-Type:application/json -X GET 'https://10.10.15.15:9200/logstash-linux-pci-2019.03.15/_stats' | jq '._all.primaries.docs.count'`
echo "The document count is "$docs
The document count is 183,283,999
I am importing line-by-line indexes from a text file that might look something like this:
list.txt
logstash-linux-pci-2019.03.15
logstash-linux-pci-2019.03.16
logstash-linux-pci-2019.03.17
logstash-linux-pci-2019.03.18
I need to place an ip variable along with these^ into a curl command as such:
reindex.sh
user='admin'
pass='hunter1'
ip='https://10.10.15.15:9200'
indexList=(`cat "list.txt"`)
for index in "${indexList[@]}"
do
echo "Reindexing: "$index
docs=`curl -k -u $user:$pass -HContent-Type:application/json -X GET '$ip/$index/_stats' | jq '._all.primaries.docs.count'`
echo "index "$index" has a doc count of "$docs
done
echo "Complete!"
The only thing that doesn't work in the above example is ....GET '$ip/$index/_stats'....
I have tried hard coding the variables as plain text in the command like a normal curl GET and it works fine, I can get the doc count for the hard coded index.
I have tried:
....GET '$ip/$index/_stats'....
Throws error:
curl: (52) Empty reply from server
.
....GET "${ip}/${index}/_stats"....
Throws error:
curl: (52) Empty reply from server
.
....GET 'http://10.10.15.15/'"$index"'/_stats'....
Throws error:
curl: (52) Empty reply from server
.
....GET '$ip/$index/_stats'....
Throws error:
curl: (52) Empty reply from server
.
url="${ip}/${index}/_stats"
....GET $url ....
Throws error:
curl: (52) Empty reply from server
.
I have searched online for hours but anything I find is related to curl PUT and putting variables as json onto servers. How can I curl with a variable URL and index?