Example Plesk API calls with BASH & Curl

This week I have been using blunt tools to make shiny things happen. Below are some examples of manipulating licenses with Odin/Plesk over their API using the exceptionally flexible, if a little blunt, tool that is BASH and Curl - with a side helping of JQ.

API calls are a new adventure to me, and is my second adventure into such things. So while these extracts may be a little crappy, ill thought out, ineloquent - the reality is they work... and the documentation (unlike that for the cPanel license API) ... SUCKS in terms of clarity and example. As such it seemed only right and fair to share the examples. Yes that's right Odin / Plesk in documentation shocker with cPanel in hands down win. Who would have thunk it?

I digress...

Examples for curl / BASH API calls for Plesk licensing below - hopefully with reasonably obvious $VARIABLENAMES for your ease of use.

MODIFY A KEY

curl -s -u $APIUSERNAME:$APIPASSWORD -H "Content-Type: application/json; charset=utf-8" -X PUT -d '{"ipAddressBinding":"'$IPADDRESS'","nickname":"'$NICKNAME'"}' https://api.central.plesk.com/30/keys/$KEY?return-key-state=yes | jq '. | {keyId: .keyIdentifiers.keyId, keyNumber: .keyIdentifiers.keyNumber, IP: .ipAddressBinding, nickname: .nickname, items: [.items[].item] }';

DELETE A KEY

curl -s -u $APIUSERNAME:$APIPASSWORD -H "Content-Type: application/json; charset=utf-8" -X DELETE https://api.central.plesk.com/30/keys/$KEY?return-key-state=yes | jq '.status';

CREATE A KEY

curl -s -u $APIUSERNAME:$APIPASSWORD -H "Content-Type: application/json; charset=utf-8" -X POST -d '{"items":[{"externalId":null,"item":"PLESK-12-WEB-PRO-1M"}]}' https://api.central.plesk.com/30/keys?return-key-state=yes | jq '.keyIdentifiers.keyId,.keyIdentifiers.activationCode'

DETAILS OF A KEY

curl -s https://$APIUSERNAME:$APIPASSWORD@api.central.plesk.com/30/keys/$KEY | jq '. | {keyId: .keyIdentifiers.keyId, keyNumber: .keyIdentifiers.keyNumber, IP: .ipAddressBinding, nickname: .nickname, items: [.items[].item] }';

LIST KEY OF A TYPE

for KEY in `curl -s -u $APIUSERNAME:$APIPASSWORD https://api.central.plesk.com/30/keys?status=$LICENSETYPE | jq '.[].keyId'`; do
echo "${KEY}";
done;

SPECIFIC KEY DETAILS

curl -s https://$APIUSERNAME:$APIPASSWORD@api.central.plesk.com/30/keys?ip-address-binding=$IPADDRESS | jq '.[0].keyId';

In other news - JQ - what is that all about?

Well I thought you would never ask!

Well the simple answer is I am a fan of AWK SED GREP and so on - I enjoy their power. However for JSON they kinda bungle through it. This is where JQ comes in.

Speaking in terms of Ubuntu - it is in the current stock repositories so you can apt-get install jq - and it would appear that CentOS 7.2 fails to see this as a useful thing - so you will need to copy the binary into the path, chmod +x /usr/bin/jq that badboy and probably rename it to jq.

More on this can be found at:

https://stedolan.github.io/jq/

With the delicious testing area here - which means you can stop pulling your hair out edit, save, run, curse, repeat.

https://jqplay.org/

Anyway - this should give a good idea of what is required if you fancy curling yourself licenses if you are trying to automate management in some way. It certainly gives you a step up from their docs, and in that I hope it helps as much forms an aide memoir for me.

Leave a Reply

Your email address will not be published. Required fields are marked *