Hello,
Does PMG provide API to automatically add domains to "Relay domains, Transport and DKIM Sign?
Thank you
Does PMG provide API to automatically add domains to "Relay domains, Transport and DKIM Sign?
Thank you
pmgsh help /config/domains --verbose
pmgsh help /config/transport --verbose
Thanks a lot!Hi there, you can do this with the pmgsh functionality at command-line.
You could go via the API as well if that's preferred. Have you read docs/manuals?
For example:
Code:pmgsh help /config/domains --verbose pmgsh help /config/transport --verbose
It looks like DKIM would be under pmgconfig, though I haven't looked into it extensively.
I want to create a bash script to automatically add new domains from WHM/cPanel to PMG,If you just want to run the API URLs in your browser, just login to the node and then run the API url in another browser tab. The API will use your current session cookie for the API calls -- very handy for testing out calls during dev.
Otherwise, if connecting to API from a script, like PHP, you need to create an access ticket and supply that when you are doing calls. https://pmg.proxmox.com/pmg-docs/api-viewer/index.html#/access/ticket - provide username, password, and realm (pmg) in a POST. It will return an access ticket and a CSRF token and you provide that ticket in a PMGAuthCookie cookie with along with CSRFPreventionToken http header that has the CSRP token.
PMG API uses the same, as far as I am aware, API authentication as proxmox ve. So if you find a script for proxmox ve that has the login, you can adapt it for PMG.
@Abdelrahman what is your goal here for the API? I mean do you want to intergrate in to a deployment system, control panel, or ?
curl --request POST \
-d "domain=$currentDomain" \
-d "comment=hosted-`hostname`" \
--cookie "$PMGcookie" \
--header "CSRFPreventionToken: $CSRFtoken" \
https://pmg-server.example.com:8006/api2/json/config/domains
Got it .. I just did but I got an empty response, I checked on UI and I didn't see the domain addedYou need to first get the access ticket via curl, wget, whatever and get the PMGAuthCookie and CSFR token into bash script variables. How you get those there is up to you. It returns a json, so you'll need to parse it out. Using the jq tool seems to be a commonly recommended method for json in bash.
Once you have the PMGAuthCookie and CSRF token in bash variables, you do curl, wget, whatever that can pass a cookie and header and do a POST. So for example, curl would be:
Bash:curl --request POST \ -d "domain=$currentDomain" \ -d "comment=hosted-`hostname`" \ --cookie "$PMGcookie" \ --header "CSRFPreventionToken: $CSRFtoken" \ https://pmg-server.example.com:8006/api2/json/config/domains
Note that we're doing POST here and not PUT and we don't give the domain in the URL. PUT is only for updating an existing domain and using PUT while trying to add will lead to an error .
root@pmg:~# curl --request POST \
> -d "domain=xxx.com" \
> -d "comment=hosted-`test`" \
> --cookie "PMG:apis@pmg:60E9F16F::xxxxxxxx" \
> --header "CSRFPreventionToken: xxxxx" \
> https://pmg.domain.com:8006/api2/json/config/domains
root@pmg:~#
Note: Unnecessary use of -X or --request, POST is already inferred.You replaced "hostname" with "test" within the tildes and that is likely where the issue is as test is not a valid command. Putting something between tildes ("`") instructs bash to run that as as command and to use that output. You could remove that part if you want and just have the comment be: -d "comment=blah" .
Adding on --verbose to the curl will also help to determine errors from pmg.