Is there a way to extract IP address from Linode API?
Hello I am trying to extract just the IP address from a Linode API domain query.
I read through the API documentation and assumed something like below would return only the "target" which contains the IP address I am looking for, but it does not work as expected.
curl -H "Authorization: Bearer OAUTHKEY" -H 'X-Filter: { "target" }' https://api.linode.com/v4/domains/ID/records/DOMAINID/
I could pipe the data from the query to sed to extract the IP, but I am hoping there is a cleaner way.
Thanks
2 Replies
✓ Best Answer
Thank you for the response @tlambert
I noticed the same thing you did and was unable to only return the "target".
I was able to filter the contents using sed, in case anyone else is wanting to do this here is one of your options:
curl -H "Authorization: Bearer $token" https://api.linode.com/v4/domains/ID/records/NUMBER/ | sed 's/^."target"\:(.)"priority".*$/\1/'| tr -d '", '
I also realized I could use dig to get this information a bit easier. To anyone else who uses this method, be aware dig may not show you the most up to date info if you are making changes frequently.
dig +short DOMAIN.COM | head -1
To give a bit of background on what I am trying to accomplish here. I have multiple ISPs and when one goes down I want the A record for a website to update to my secondary ISP. I know there are other options for doing this, but this fit my budget and requirements.
Since the IP information is stored at the https://api.linode.com/v4/domains/ID/records and https://api.linode.com/v4/domains/ID/records/DOMAINID/ endpoints, these are the only places to retrieve that info from the API. As you can see from just listing the domains the associated IP addresses are not listed, and therefore you need to be a bit more specific when creating your API call.
This definitely creates a bit more work for the user. Using the Filtering and Sorting Documentation I used the following API call to return a result, however it did not return only the "target"
:
$ curl -H "Authorization: Bearer $TOKEN" \
https://api.linode.com/v4/domains/ID/records/ \
-H 'X-Filter: { "target": "my.domain's.ip.address" }' | json_pp
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 288 100 288 0 0 900 0 --:--:-- --:--:-- --:--:-- 900
{
"data" : [
{
"created" : "2023-04-28T16:28:13",
"id" : ID,
"name" : "",
"port" : 0,
"priority" : 0,
"protocol" : null,
"service" : null,
"tag" : null,
"target" : "my.domain's.ip.address",
"ttl_sec" : 300,
"type" : "A",
"updated" : "2023-04-28T16:28:13",
"weight" : 0
}
],
"page" : 1,
"pages" : 1,
"results" : 1
}
With this in mind, your best bet is probably to use sed
to extract the IP as you mentioned in your question.