ISPConfig bash CLI script for REST API
In this blog post, I share a contribution to ISPConfig. We are happy users of ISPConfig and have extended its use to some of our bash scripts. Since there were no readily examples on shell and curl usage out there, we decided to create our own.
Why a shell script?
The use case at Molnix is for DNS automation that is part of our orchestration, which in the case concerned is written in bash. Previously the DNS automation was done with bind directly and that works very well. However, since most of our DNS needs are already managed through ISPConfig, we wanted to consolidate this part too. That said, addressing a REST API is not too common or too convenient in pure bash. Additionally, there is no CLI application for the ISPConfig API, so curl and some simple scripting are used to fill the gap.
The script follows the API example conventions in ISPConfig for easy readability. As such, it covers the basics and should help you get started with whichever methods you need. I am also creating a merge request at ISPConfig to include this example if it is found useful.
The script
#!/bin/bash set -e remote_user=test remote_password=apipassword remote_url='https://yourserver.com:8080/remote/json.php' # restCall method data restCall() { curl -sS -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d "${2}" "${remote_url}?${1}" } # Log in session_id=`restCall login "{\"username\": \"${remote_user}\",\"password\": \"${remote_password}\"}" | jq -r '.response'` if [[ $isession == "false" ]]; then echo "Login failed!" exit 1 #else #echo "Logged in. Session is: $session_id" fi restCall client_get "{\"session_id\": \"$session_id\",\"client_id\":{\"username\": \"abcde\"}}" # or by id restCall client_get "{\"session_id\": \"$session_id\",\"client_id\": \"2\"}" # or all restCall client_get "{\"session_id\": \"$session_id\",\"client_id\":{}}" # Log out if [[ `restCall logout "{\"session_id\": \"$session_id\"}" |jq -r .response` == "true" ]]; then #echo "Logout successful." exit 0 else echo "Logout failed!" exit 1 fi