mkdir /usr/local/custom_scriptsvi /usr/local/custom_scripts/checknewip.sh#!/bin/shCURRENT_IP=`curl -s https://ipv4.icanhazip.com/`OLD_IP="${CURRENT_IP}"CURRENT_PREFIX=$(cat /tmp/igc0_prefixv6)OLD_PREFIX="${CURRENT_PREFIX}"WORKDIR="/usr/local/custom_scripts"cd "${WORKDIR}"SUBJECT="opnsense01 - new ip detected"CHANGED=0echo "current ip: ${CURRENT_IP}"if [ -f "${WORKDIR}/old_ip.txt" ]; then echo "old_ip.txt found" OLD_IP=$(cat "${WORKDIR}/old_ip.txt")else echo "no old_ip.txt found" echo "${CURRENT_IP}" > "${WORKDIR}/old_ip.txt"fiecho "old ip: ${OLD_IP}"if [ "${OLD_IP}" != "${CURRENT_IP}" ]; then CHANGED=1 echo "ip changed" echo "${CURRENT_IP}" > "${WORKDIR}/old_ip.txt"fiecho "current prefix: ${CURRENT_PREFIX}"if [ -f "${WORKDIR}/old_prefix.txt" ]; then echo "old_prefix.txt found" OLD_PREFIX=$(cat "${WORKDIR}/old_prefix.txt")else echo "no old_prefix.txt found" echo "${CURRENT_PREFIX}" > "${WORKDIR}/old_prefix.txt"fiecho "old prefix: ${OLD_PREFIX}"if [ "${OLD_PREFIX}" != "${CURRENT_PREFIX}" ]; then CHANGED=1 echo "prefix changed" echo "${CURRENT_PREFIX}" > "${WORKDIR}/old_prefix.txt"fiMSG="old ip: ${OLD_IP}new ip: ${CURRENT_IP}old prefix: ${OLD_PREFIX}new prefix: ${CURRENT_PREFIX}"if [ "${CHANGED}" == 1 ]; then "${WORKDIR}"/mailsender.py "${SUBJECT}" "${MSG}"fiexit 0
vi /usr/local/custom_scripts/mailsender.py#!/usr/local/bin/python3import smtplibfrom email.message import EmailMessagefrom email.utils import localtimeimport sysmsg = EmailMessage()msg.set_content(sys.argv[2])msg['Subject'] = sys.argv[1]msg['From'] = 'from@mailserver.xy'msg['To'] = 'to@mailserver.xy'msg['Date'] = localtime()server = smtplib.SMTP('mailserver.xy', 25)server.ehlo()server.starttls()server.ehlo()server.login('from@mailserver.xy', 'mailpassword')server.send_message(msg)server.quit()
vi /usr/local/opnsense/service/conf/actions.d/actions_own_checknewip.conf[checknewip]command:/usr/local/custom_scripts/checknewip.shparameters:type:scriptmessage:checking if ip4 or ip6 prefix has changeddescription:check if ip4 or ip6 prefix has changed