Created
July 15, 2022 04:36
-
-
Save moesoha/5333d97fbfb2eb7c6cf426ab71e4e10b to your computer and use it in GitHub Desktop.
Juniper DDNS script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version 1.0; | |
ns junos = "http://xml.juniper.net/junos/*/junos"; | |
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0"; | |
ns curl extension = "http://xml.libslax.org/curl"; | |
import "../import/junos.xsl"; | |
/* | |
put this file as /var/db/scripts/op/update-ddns.slax | |
and then configure: | |
system { | |
scripts { | |
op { | |
file update-ddns.slax { | |
command update-ddns; | |
routing-instance somewhere; // optional, add this if you want to send update with non-default instance | |
} | |
} | |
} | |
services { | |
apply-macro ddns:he-example { | |
type HE; | |
hostname example.com; | |
password 114514; | |
interface pp0.0; // IP source, optional | |
} | |
} | |
} | |
event-options { | |
generate-event { | |
every-10min time-interval 600; | |
} | |
policy ddns-update { | |
events [ snmp_trap_link_up every-10min ]; | |
attributes-match { | |
snmp_trap_link_up.interface-name matches pp0.0; | |
} | |
then { | |
execute-commands { | |
commands { | |
"ping 1 count 5 interval 1 wait 1"; // wait 5s for pp0.0 to be online | |
"op update-ddns"; | |
} | |
} | |
} | |
} | |
} | |
*/ | |
match / { | |
var $configs = jcs:invoke(<get-configuration> { | |
<configuration> { | |
<system> { | |
<services> { | |
<apply-macro>; | |
} | |
} | |
} | |
}, "no-login-logout"); | |
for-each ($configs//apply-macro[starts-with(./name, 'ddns:')]) { | |
expr jcs:output('Processing config ' _ ./name); | |
call update-ddns($config=.); | |
} | |
} | |
template update-ddns($config) { | |
var $address = { | |
if ($config/data[./name="interface"]) { | |
var $iface = jcs:invoke(<get-interface-information> { | |
<interface-name> $config/data[./name="interface"]/value; | |
}); | |
expr $iface//address-family[./address-family-name="inet"]/interface-address/ifa-local[1]; | |
} | |
}; | |
var $hostname = $config/data[./name="hostname"]/value; | |
if ($config/data[./name="type"][value="HE"]) { | |
var $curlParam := { | |
<insecure>; /* TOO LAZY TO IMPORT CA CERTIFICATE BUNDLE */ | |
<url> 'https://dyn.dns.he.net/nic/update'; | |
<method> 'POST'; | |
<param name="hostname"> $hostname; | |
<param name="password"> $config/data[./name="password"]/value; | |
if ($address) { | |
<param name="myip"> $address; | |
} | |
}; | |
if ($address) { | |
expr jcs:output('[HE:' _ $hostname _ '] Updating to address ' _ $address); | |
} else { | |
expr jcs:output('[HE:' _ $hostname _ '] Updating'); | |
} | |
var $curlResult = curl:single($curlParam); | |
if (not($curlResult/curl-success)) { | |
expr jcs:output('[HE:' _ $hostname _ '] cURL error: ' _ $curlResult//error); | |
} else if ($curlResult//headers/code = 200) { | |
expr jcs:output('[HE:' _ $hostname _ '] Success with content: ' _ $curlResult//raw-data); | |
} else { | |
expr jcs:output('[HE:' _ $hostname _ '] Failed with content: ' _ $curlResult//raw-data); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment