Search code examples
pythonfortigate

How can I change a fortigate hostname through fortimanager API


I'm working on a script that changes the hostname of a fortigate through fortimanager API.

I have found a field called hostname in the FMG gui, but whenever I change this field the change is not reflected on the device itself.

The device i'm looking at is a FG-100E to be specific.

These are the calls I've used to change the hostname under cli config->system->global ->hostname in the fmg gui, and the device name on the device manager dashboard.

The api calls work to change the device name and the hostname under the cli config, however the hostname change is not reflected on the dashboard or the device.

Thanks in advance if anyone knows how to get these changes reflected.

`

    # API call to change fmg hostname in gui at cli config level.
        fmg.fmgInstance.update(

            f"pm/config/device/{hostname}/global/system/global",
            hostname=new_hostname.upper()

        )
     # API call to change fmg device name
        fmg.fmgInstance.execute(

            "dvm/cmd/update/device", adom="root", 
            device=new_hostname.upper()

        )

`


Solution

  • You can apply 2 strategies:

    1. Modify the hostname through a proxy call to your FMG:
    {
      "id": 1,
      "jsonrpc": "2.0",
      "method": "exec",
      "params": [
        {
          "data": {
            "action": "put",
            "resource": "/api/v2/cmdb/system/global",
            "payload": {
                "hostname": "<new_hostname>"
            },
            "target": [
              "adom/{{adom}}/device/{{device1}}"
            ]
          },
          "url": "/sys/proxy/json"
        }
      ],
      "session": "{{session}}"
    }
    

    The modification will occurred only on FortiGate and the auto-retrieved feature will sync the configuration after a delay.

    Note: Fortimanager must be set to auto-retrieved enable (which is the default setting)

    1. Change the FMG DVMDB and install the modification on the device.

    Change config in Device DB:

    {
      "id": 1,
      "jsonrpc": "2.0",
      "method": "set",
      "params": [
        {
          "data": {
            "hostname": "<new_hostname>"
          },
          "url": "/pm/config/device/{{device}}/global/system/global"
        }
      ],
      "session": "{{session}}"
    }
    

    Install device:

    {
        "id": 1,
        "jsonrpc": "2.0",
        "method": "exec",
        "params": [
            {
                "url": "/securityconsole/install/device",
                "data": {
                    "adom": "{{adom}}",
                    "scope": [
                        {
                            "name": "{{device}}",
                            "vdom": "root"
                        }
                    ],
                    "flags": [
                        "none"
                    ]
                }
            }
        ],
        "session": "{{session}}",
        "verbose": 1
    }