Am trying to run multiline curl command in shell but not able to run it
curl -XPUT 'elk.***.com:9200/_snapshot/my_backup' \
{
"type": "fs",
"settings": {
"location": "/elkprod_bak/elkback",
"compress": true
}
}
You need to use the -d switch on the command line like this:
curl -XPUT 'elk.***.com:9200/_snapshot/my_backup' -d '{
"type": "fs",
"settings": {
"location": "/elkprod_bak/elkback",
"compress": true
}
}'
Related
I would like to add the Databricks init script API in my exsiting bash script. How can I do this? Here is the API provided by Databricks:
curl -n -X POST -H 'Content-Type: application/json' -d '{
"cluster_id": "",
"num_workers": 1,
"spark_version": "8.4.x-scala2.12",
"node_type_id": "$node_type",
"cluster_log_conf": {
"dbfs" : {
"destination": "dbfs:/cluster-logs"
}
},
"init_scripts": [ {
"dbfs": {
"destination": "dbfs:/FileStore/shared_uploads/kafka_keytabs/CopyKrbFiles.sh"
}
} ]
}' https://<databricks-instance>/api/2.0/clusters/edit
I am writing a bash script to be run on a linux appliance so I do not have access to install jq, I need to be able to use native Linux tools
curl --location --request POST "https://api-mp.meraki.com/api/v1/networks/12345/switch/stacks/12345/routing/interfaces" \
--header "X-Cisco-Meraki-API-Key: 123key" \
--header "Content-Type: application/json" \
--data '{
"name": "L3 int",
"subnet": "192.168.249.0/24",
"interfaceIp": "192.168.249.2",
"multicastRouting": "disabled",
"vlanId": 140,
"ospfSettings": {
"area": "0",
"cost": 1,
"isPassiveEnabled": true
}
}' | sed -n 's|.*"interfaceID":"\([^"]*\)".*|\1|p' > $newVar
the response from the server returns this:
{
"interfaceId": "12345",
"name": "PA L3 DR Int",
"subnet": "192.168.249.0/24",
"interfaceIp": "192.168.249.2",
"multicastRouting": "disabled",
"vlanId": 140,
"ospfSettings": {
"area": "0",
"cost": 1,
"isPassiveEnabled": true
}
}
I then need to be able to access the interfaceID new variable in a query string paramter
You just got the case wrong (ID vs Id in interfaceId) and forgot about the space after :, but you were close:
$ sed -n 's|.*"interfaceId": *"\([^"]*\)".*|\1|p' file
12345
Using cat file in place of your curl command to show how to save that to a variable in case you don't know:
$ var=$(cat file | sed -n 's|.*"interfaceId": *"\([^"]*\)".*|\1|p')
$ echo "$var"
12345
Could you please try following. Tested successfully in link
https://ideone.com/EsdCQp
your_command |
awk '
/"interfaceId": "/{
gsub(/[^0-9]+|"\,/,"")
print
}
'
I have an issue with the web interface. I'm using powerdns v3.4.5 with mysql as the back-end.
I have followed instructions from here:
https://www.unixmen.com/how-to-install-powerdns-on-ubuntu-14-04/
I have successfully installed powerdns with mysql and got the web-api to work.
However I have trouble inserting A records using the REST api.
I have followed command from here:
https://doc.powerdns.com/md/httpapi/README/
This creates a new zone:
curl -X POST --data '{"name":"example.org.", "kind": "Native", "masters": [], "nameservers": ["ns1.example.org.", "ns2.example.org."]}' -v -H 'X-API-Key: changeme' http://127.0.0.1:8081/servers/localhost/zones | jq .
(note that I changed the url and removed /api/v1/)
However when I run the following command to add a new A record:
curl -X PATCH --data '{"rrsets": [ {"name": "test.example.org.", "type": "A", "ttl": 86400, "changetype": "REPLACE", "records": [ {"content": "192.0.5.4", "disabled": false } ] } ] }' -H 'X-API-Key: changeme' http://127.0.0.1:8081/servers/localhost/zones/example.org. | jq .
I get the following error:
"error": "RRset test.example.org. IN A: Name is out of zone"
is there anything that I'm missing?
It should be as follows:
curl -X POST --data '{"name":"example.org", "kind": "Master","dnssec":false,"soa-edit":"INCEPTION-INCREMENT","masters": [], "nameservers": ["ns1.example.org"]}' -v -H 'X-API-Key: changeme' http://127.0.0.1:8081/servers/localhost/zones | jq .
and then:
curl -X PATCH --data '{"rrsets": [ {"name": "test.example.org", "type": "A", "changetype": "REPLACE", "records": [ {"content": "192.168.9.9", "disabled": false, "name": "test.example.org", "ttl": 86400, "type": "A", "priority": 0 } ] } ] }' -H 'X-API-Key: changeme' http://127.0.0.1:8081/servers/localhost/zones/example.org | jq .
As time passes by, the API changed. The current json structure is a little bit different, also power-dns insists of getting canonical names ending with a dot.
# cat example_zone.json
{
"kind": "Native",
"masters": [],
"name": "example.com.",
"nameservers": [
"ns1.example.com.",
"ns2.example.com."
]
}
# curl -s -H 'X-API-Key: changeme' --data #example_zone.json http://127.0.0.1:8081/api/v1/servers/localhost/zones
# cat example_rrset.json
{
"rrsets":
[
{
"name": "test.example.com.",
"type": "A",
"changetype": "REPLACE",
"ttl": 86400,
"records":
[
{
"content": "192.168.9.9",
"disabled": false,
"name": "test.example.com.",
"type": "A",
"priority": 0
}
]
}
]
}
# curl -v -X PATCH -H 'X-API-Key: changeme' --data #example_rrset.json http://127.0.0.1:8081/api/v1/servers/localhost/zones/example.com.
I'm new but trying to get a new script running but I need it to call on todays date as a variable within the configuration file so the program can be run.
I'm sure sure the best way to implement it so far this line will replace the correct part of the configuration file I need but I can't figure out how to get it to use the "todays date" e.g. date +%F command.
sed -i 's/"to_date":.*/"to_date":"date +%F"/' /config/settings
config following:
{
"username":"admin",
"password":"redhat",
"assumeyes":true,
"to_date": "2011-10-01",
"skip_depsolve":false,
"skip_errata_depsolve":false,
"security_only":false,
"use_update_date":false,
"no_errata_sync":false,
"dry_run":false,
"errata": ["RHSA-2014:0043", "RHBA-2014:0085"],
"blacklist": {
},
"removelist": {
},
"channels":[
{
"rhel-x86_64-server-5": {
"label": "my-rhel5-x86_64-clone",
"existing-parent-do-not-modify": true
},
"rhn-tools-rhel-x86_64-server-5": {
"label": "my-tools-5-x86_64-clone",
"name": "My Clone's Name",
"summary": "This is my channel's summary",
"description": "This is my channel's description"
}
},
{
"rhel-i386-server-5": "my-rhel5-i386-clone"
}
]
}
Using a proper JSON parser jq with the --arg field to pass the current date,
jq --arg inputDate $(date +%F) '.to_date = $inputDate' /config/settings
{
"username": "admin",
"password": "redhat",
"assumeyes": true,
"to_date": "2017-01-27",
"skip_depsolve": false,
"skip_errata_depsolve": false,
"security_only": false,
"use_update_date": false,
"no_errata_sync": false,
"dry_run": false,
"errata": [
"RHSA-2014:0043",
"RHBA-2014:0085"
],
"blacklist": {},
"removelist": {},
"channels": [
{
"rhel-x86_64-server-5": {
"label": "my-rhel5-x86_64-clone",
"existing-parent-do-not-modify": true
},
"rhn-tools-rhel-x86_64-server-5": {
"label": "my-tools-5-x86_64-clone",
"name": "My Clone's Name",
"summary": "This is my channel's summary",
"description": "This is my channel's description"
}
},
{
"rhel-i386-server-5": "my-rhel5-i386-clone"
}
]
}
The jq download and usage instructions are pretty straight forward. Recommend using it for manipulating JSON, instead of depending upon regex.
jq does not edit the file in-place, save it to a temporary file and rename it back, using GNU mktemp
jsonTemp=$(mktemp)
jq --arg inputDate $(date +%F) '.to_date = $inputDate' /config/settings > "$jsonTemp"
mv "$jsonTemp" /config/settings
To include the output of a command inside some quoted text, you have to use a subshell and use double-quotes so the text will get expanded :
sed -i "s/\"to_date\":.*/\"to_date\":\"$(date +%F)\"/" /config/settings
Also I second Inian's comment : you should be using jq to manipulate JSON data.
For example, the following command should do the modification you need :
jq ".toDate = $(date +%F)" /config/settings
I found this.
And I wrote this variant:
#!/bin/bash
while read line ; do
headers="$headers -H '$line'"
done < public/headers.txt
echo $headers
curl -X PUT \
$headers \
-d #'public/example.json' \
echo.httpkit.com
In headers.txt I have:
X-PAYPAL-SECURITY-USERID:123
X-PAYPAL-SECURITY-PASSWORD:123
But when I run ./public/curl.sh I am not getting the headers I am sending.
I isolated the issue with an env var:
$ x='-H some:asd'
$ curl $x echo.httpkit.com
=> header was NOT present
$ curl -H 'some:asd' echo.httpkit.com
=> header was present
$ curl -H some:asd echo.httpkit.com
=> header was present
How can I correctly insert a variable in the header section?
Let's ask shellcheck:
In yourscript line 3:
headers="$headers -H '$line'"
^-- SC2089: Quotes/backslashes will be treated literally.
Use an array.
Ok, then let's do that:
#!/bin/bash
while read line ; do
headers=("${headers[#]}" -H "$line")
done < public/headers.txt
echo "${headers[#]}"
curl -X PUT \
"${headers[#]}" \
-d #'public/example.json' \
echo.httpkit.com
Result:
{
"method": "PUT",
"uri": "/",
"path": {
"name": "/",
"query": "",
"params": {}
},
"headers": {
"host": "echo.httpkit.com",
"user-agent": "curl/7.35.0",
"accept": "*/*",
"x-paypal-security-userid": "123", // <----- Yay!!
"x-paypal-security-password": "123",
"content-length": "32",
"content-type": "application/x-www-form-urlencoded"
},
"body": "\"This is text from example.json\"",
"ip": "127.0.0.1",
"powered-by": "http://httpkit.com",
"docs": "http://httpkit.com/echo"
}
If you don't want to put the HTTP headers on a command line (perhaps for security reasons), you can still have curl read them directly from a file.
curl -H #headerfile.txt https://www.google.com/ # requires curl >=7.55.0
If your curl is older than 7.55.0:
Use the option -K/--config <config file>, and put several -H/--header <header> lines in the text file.
For more details, please see my answer in the original article:
https://stackoverflow.com/a/48762561/5201675