API for WOL with Curl

Started by HansJ, April 15, 2024, 08:55:25 PM

Previous topic - Next topic
April 15, 2024, 08:55:25 PM Last Edit: April 15, 2024, 08:57:13 PM by HansJ
Hello,

I am a total noob here, so my understanding of all this just comes from copy/paste from the internet (and reading info on those site's) I am in no way a programmer :(

I need to wake up PC's that are on other VLAN's with WOL and since I cant get it to work with the ARP IP to send it to , I figured ill just do it with the build in WOL API.

I will leave my PC's correct MAC and server IP in here, I dont care about its 'privacy' but the API key's user and secret are edited in my code's.

I made a user that only has access to the WOL plugin, and made a API key and secret for it.

I currently am able to wake up ALL computers with this CURL command ;

curl -XPOST -d '' -k -u "key":"secret" "https://192.168.15.1/api/wol/wol/wakeall"

This works, it boots up the PC's and in the command line I get there MAC adresses.

But I want to wake only ONE specific PC per command and I cant get it to work,
I found alot of info online and have been trying to make some copy/paste frankenstein code, but cant get it to work :(

stuff I tried ;

Most promising,
curl -XPOST -d '{"wake": {"interface": "opt2","mac": "E0:D5:5E:00:63:1C"}}' -k -u "key":"secret" "https://192.168.15.1/api/wol/wol/set"
this gives output in command line : curl: (3) unmatched brace in URL position 1:
{interface:
^

So I tryed playing with the { but like I say'd im not programmer :( :( :(

Not working stuff I tried;
curl -XPOST -d '{"wake":"interface":"opt2","mac":"E0:D5:5E:00:63:1C"}' -k -u "key":"secret" "https://192.168.15.1/api/wol/wol/set"
Just gives output "[]" on the command line ??

another way I found online
curl -s -k -d '{"wake":{"interface":"opt2", "mac":"E0:D5:5E:00:63:1C"}}' --user "key":"secret" -H 'Content-Type: application/json' https://192.168.15.1/api/wol/wol/set
this does not work, and gives no output on the command line (its a way I found that people sayed should work :( )

I  cant find a "real dummy's" guide on how to use Curl to make API calls on OPNsense (and I mean REAL dummy guide, one that assumes I know NOTHING about programming and ( and { :) :) )

Any help or pointing to some good guides is much appreciated :)

Anyone with CURL syntax and WOL api experience wana have a quick look at my code and tell me what im doing wrong ?

Would be highly appreciated :)


thank you,


Hans

Anyone with Curl syntax experience can have a quick look for me here ?


Any help is appreciated,


thank you,


Hans

Hi Hans,

i was playing with the API as well lately.
Your problem is because you are missing the "Content-Type" Header for Post Requests with payload.


curl -X POST \
     -d '{"wake":{"interface":"opt2","mac":"E0:D5:5E:00:63:1C"}}' \
     -k \
     -u "$API_KEY:$API_SECRET" \
     -H "Content-Type: application/json" \
     "https://192.168.15.1/api/wol/wol/set"


{"status":"OK"}

Thanks alot,

I will be trying this out and post here when it works :)


thank you,


Hans

Ok, forgive my ignorance, but I cant seem to get it to work,

So I put it in this format (since I want to send the command with a simple batch file)

curl -X POST -d '{"wake":{"interface":"opt2","mac":"E0:D5:5E:00:63:1C"}}' -k -u "$API_KEY:$API_SECRET" -H "Content-Type: application/json" "https://192.168.15.1/api/wol/wol/set"

But than I get this error message

{"errorMessage":"/usr/local/opnsense/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php:197: Syntax error","errorTrace":"#0 [internal function]: Phalcon\\Support\\Helper\\Json\\Decode->__invoke(''{wake:{interfa...', true)\n#1 /usr/local/opnsense/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php(197): Phalcon\\Http\\Request->getJsonRawBody(true)\n#2 /usr/local/opnsense/mvc/app/controllers/OPNsense/Base/ApiControllerBase.php(313): OPNsense\\Base\\ApiControllerBase->parseJsonBodyData()\n#3 [internal function]: OPNsense\\Base\\ApiControllerBase->beforeExecuteRoute(Object(Phalcon\\Mvc\\Dispatcher))\n#4 [internal function]: Phalcon\\Dispatcher\\AbstractDispatcher->dispatch()\n#5 /usr/local/opnsense/www/api.php(24): Phalcon\\Mvc\\Application->handle('/api/wol/wol/se...')\n#6 {main}","errorTitle":"An API exception occurred"}

So I get the error : An API excpetion occurred (not very helpfull)

Any more help is much appreciated,


Anyone know what this error means im doing wrong ??

Anyone with some API exprience can point me in the right direction ?

Hi HansJ
i was stuck at the same error message.

My solution was an python script:


import http.client
import json
import ssl

conn = http.client.HTTPSConnection('$OPNsenseIP:$Port', context=ssl._create_unverified_context())
headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Basic %Base64 Coded API and Secret Key%',
}
json_data = {
    'wake': {
        'interface': 'opt1',
        'mac': '00:00:00:00:00:00',
    },
}
conn.request(
    'POST',
    '/api/wol/wol/set',
    json.dumps(json_data),
    # '{"wake":{"interface":"opt1","mac":"00:00:00:00:00:00"}}',
    headers
)
response = conn.getresponse()


Save it as wol.py for example. Make sure you insert the right interface and MAC.

The python script ist auto generated from here: https://curlconverter.com/python-httpclient/
It will transform the OPNsense API key and secret automaticaly to Base64 for Basic Auth.

My original curl prompt was:


curl -X POST -H 'Content-Type:application/json' -d '{"wake":{"interface":"opt1","mac":"00:00:00:00:00:00"}}' -k -u "$API-Key:$API-Secret" https://$OPNsenseIP:$Port/api/wol/wol/set



In Windows you have to install python from https://www.python.org/downloads/windows/

When installing add python to system path global vars in Windows.

You can now start your wol.py via cmd or batch script


@echo off
python C:\pathtopythonscriptfile\wol.py
pause

Thanks alot, I will try this soon and let you know if it works,

After that I gues I will have to find some python way to make this also work from my android phone, but that will be the next problem :)

This works :)

I just had to remove the -s tag from my original Curl line,


Thank you so much,


You happen to know any quick and small android app that could run that Python command so I can start my server from my smartphone ? :)

Are you trying to wake an OPN machine or one running another OS, which one?

Quote from: cookiemonster on June 03, 2024, 10:10:15 PM
Are you trying to wake an OPN machine or one running another OS, which one?

I am waking up a OpenMediaVault server. (from one Vlan to another Vlan so the standard WOL magic packet wont work)
(so running linux) and its working now , so thanks all for helping :)


I had some success with using the host UUID configured. I was able to get the UUID from "https://<hostname>/api/wol/wol/searchhost" call.

curl -X POST -d '{"uuid":"<UUID>"}' -k -H "Content-Type: application/json" -H "Authorization:Basic <Base64 API key and secret>" https://<hostname>/api/wol/wol/set
When run successfully, I receive a response back of {"status":"OK"}