OPNsense
  • Home
  • Help
  • Search
  • Login
  • Register

  • OPNsense Forum »
  • English Forums »
  • Tutorials and FAQs »
  • Script to send emails for updates
« previous next »
  • Print
Pages: 1 2 [3]

Author Topic: Script to send emails for updates  (Read 27673 times)

bartjsmit

  • Hero Member
  • *****
  • Posts: 1750
  • Karma: 176
    • View Profile
Re: Script to send emails for updates
« Reply #30 on: May 27, 2022, 07:53:07 am »
Thanks for your contribution!

Bart...
Logged

HomeUser28280

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
    • View Profile
Re: Script to send emails for updates
« Reply #31 on: July 26, 2023, 03:38:10 pm »
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: [Select]
$key="xxxxxxxxx"
$secret="xxxxxxxxxx"

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

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: [Select]
<#
  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
You could then acces the JSON endpoints like so:
Code: [Select]
$status = $apiResponse.status
$statusMsg = $apiResponse.status_msg
$newPackages = $apiResponse.new_packages
$upgradePackages = $apiResponse.upgrade_packages
$reinstallPackages = $apiResponse.reinstall_packages
$needsReboot = $apiResponse.needs_reboot


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).
« Last Edit: August 02, 2023, 01:39:50 pm by HomeUser28280 »
Logged

HomeUser28280

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
    • View Profile
Re: Script to send emails for updates
« Reply #32 on: August 24, 2023, 01:29:45 pm »
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: [Select]
//recent-post[1]/subject 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: [Select]
//recent-post[not(contains(subject, 'Re:'))]/subject
« Last Edit: September 22, 2023, 08:45:02 am by HomeUser28280 »
Logged

  • Print
Pages: 1 2 [3]
« previous next »
  • OPNsense Forum »
  • English Forums »
  • Tutorials and FAQs »
  • Script to send emails for updates
 

OPNsense is an OSS project © Deciso B.V. 2015 - 2023 All rights reserved
  • SMF 2.0.19 | SMF © 2021, Simple Machines
    Privacy Policy
    | XHTML | RSS | WAP2