English Forums > Tutorials and FAQs

Script to send emails for updates

<< < (7/7)

bartjsmit:
Thanks for your contribution!

Bart...

HomeUser28280:
I used the ChangeDetection.io Docker container i am running to monitor if my OPNsense has updates available. Configure a watch like this:

General > URL: https://hostnameofrouter/api/core/firmware/status
Request > Request method: POST
Request > Request headers: Authorization: Basic PUTBASE64HERE
Filters & Triggers > CSS/JSONPath/JQ/XPath Filters > json:$.status

You can create the PUTBASE64HERE with some PowerShell:

--- Code: ---$key="xxxxxxxxx"
$secret="xxxxxxxxxx"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("$($key):$($secret)"))
Write-Host $base64AuthInfo

--- End code ---

As my OPNsense is currently updated, i haven't been able to check if it works completely right. But the JSON is displayed in ChangeDetection, so changes will be picked up if the 'status' field changes.

Or if you want to do it completely in PowerShell you could create a script yourself and use this as a start:

--- Code: ---<#
  This script retrieves the update status of OPNsense, to monitor if the system needs an update.
#>


$key="xxxxxx"
$secret="xxxxxx"

$hostname = 'puthostnamehere'

<#
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

#>
$user = $key
$pass = $secret
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
    Authorization = $basicAuthValue
}

$url = "https://$hostname/api/core/firmware/status"
Invoke-RestMethod -Uri $url -Method Post -Headers $Headers

--- End code ---
You could then acces the JSON endpoints like so:

--- Code: ---$status = $apiResponse.status
$statusMsg = $apiResponse.status_msg
$newPackages = $apiResponse.new_packages
$upgradePackages = $apiResponse.upgrade_packages
$reinstallPackages = $apiResponse.reinstall_packages
$needsReboot = $apiResponse.needs_reboot
--- End code ---


I commented out the TrustAllCertsPolicy stuff as it wasn't needed (i use a self-signed certificate on OPNsense which is also present on my computer).

HomeUser28280:
Unfortunately the above didn't seem to work, i wasn't notified of the most recent update. I think the POST-request and such works, but ChangeDetection.io is not playing nice with it.

I now configured changedetection to look at the URL https://forum.opnsense.org/index.php?board=11.0&action=.xml with the filter
--- Code: ---//recent-post[1]/subject
--- End code ---
to only return the first topic in the list. This seems to work better.

Edit: The above XPath also returns the 'Re:" topics. I changed my XPath to this (thanks ChatGPT :) so that i only get notified about new releases ):

--- Code: ---//recent-post[not(contains(subject, 'Re:'))]/subject
--- End code ---

Navigation

[0] Message Index

[*] Previous page

Go to full version