OPNsense Forum

English Forums => General Discussion => Topic started by: bartjsmit on May 08, 2016, 11:15:01 PM

Title: [SOLVED] Remote shutdown
Post by: bartjsmit on May 08, 2016, 11:15:01 PM
I have OPNsense and a number of internal hosts powered from a UPS. One of the host has a USB connection to the UPS and runs a script on loss of mains power for a graceful shutdown. This script shuts down the other hosts powered by the UPS.

Is there a way to include a remote shutdown of OPNsense in this script? Either through SSH, an API call, SNMP or some other means?

Bart...
Title: Re: Remote shutdown
Post by: fabian on May 09, 2016, 05:55:25 PM
power off by the API is afaik not possible at the moment but if you look at this page, it should not be too hard to implement:
https://github.com/opnsense/core/blob/master/src/www/diag_halt.php

SSH works:
ssh root@opnsense /sbin/poweroff
Title: Re: Remote shutdown
Post by: franco on May 10, 2016, 08:21:37 AM
Do not use poweroff... use the script /usr/local/etc/rc.halt as you'll otherwise end up with no backups and/or improper shutdowns.

API for this is going to happen, likely not for 16.7, but should be a viable target for 17.1.
Title: Re: Remote shutdown [SOLVED]
Post by: bartjsmit on May 10, 2016, 06:27:22 PM
Thanks both, I will use the SSH script for now at least.

Bart...
Title: Re: [SOLVED] Remote shutdown
Post by: franco on May 18, 2016, 09:27:41 AM
I'll add the necessary firmware API bits for 16.1.15:

https://github.com/opnsense/core/issues/948
Title: Re: [SOLVED] Remote shutdown
Post by: franco on May 19, 2016, 08:11:02 AM
All done, using the API you can now do e.g.:

# curl -XPOST -d '{}' -H "Content-Type: application/json" -k -u "APIKEY":"APISECRET" https://10.0.0.1/api/core/firmware/poweroff

# curl -XPOST -d '{}' -H "Content-Type: application/json" -k -u "APIKEY":"APISECRET" https://10.0.0.1/api/core/firmware/reboot

Note that this requires the user to have firmware page privileges.

Have fun. :)
Title: Re: [SOLVED] Remote shutdown
Post by: bartjsmit on May 19, 2016, 08:27:26 AM
Thanks Franco,

I'll test that out once the school exams are finished.

Bart...
Title: Re: [SOLVED] Remote shutdown
Post by: RichardPo12 on September 19, 2025, 08:45:03 AM
Quote from: franco on May 10, 2016, 08:21:37 AMDo not use poweroff... use the script /usr/local/etc/rc.halt as you'll otherwise end up with no backups and/or improper shutdowns.

API for this is going to happen, likely not for 16.7, but should be a viable target for 17.1.

I tried to shutdown the router via:  ssh root@opnsense /.... Any command
But it does not accept ssh commands from the PC (the one connected to the ups), neither using the root user nor any other user.
From the same PC, I can access the OpnSense router's GUI with a browser //<ip>/
Title: Re: [SOLVED] Remote shutdown
Post by: meyergru on September 19, 2025, 09:39:56 AM
Did you enable SSH access (and for root, also)? You need SSH access (preferably with an SSH key to automate) and root privileges to do that.