首页 > 其他分享 >Udhcpc.user script documentation and how to hotplug for DHCP events

Udhcpc.user script documentation and how to hotplug for DHCP events

时间:2023-04-16 12:22:53浏览次数:42  
标签:script documentation hotplug events DHCP udhcpc openwrt

Udhcpc.user script documentation and how to hotplug for DHCP events

https://forum.openwrt.org/t/udhcpc-user-script-documentation-and-how-to-hotplug-for-dhcp-events/47952/10

 

Hi, guys! I've been looking for ways to execute programmes/scripts on DHCP events. To be more specific, I want to update my dynamic DNS (FreeDNS 1) when, and only when there's a potential change on the WAN interface(s) IP address(es) (yeah, I had a cron job doing it every ten minutes, but I always found that rather ugly, IMNSHO).
It took me quite a bit searching in order to find this trac entry 20 mentioning an elusive /etc/udhcpc.user script (for which there isn't even a stub in the base-files package). So, I created this one…

#!/bin/sh
env | logger
exit 0

… sent USR1 to the udhcpc PID and got quite a number of juicy variables in the log, namely $ip (the IP address), $INTERFACE (the interface name) and $interface (the physical device). With this information, I changed my script to…

#!/bin/sh
if [ "wan" = $INTERFACE ]; then
	/usr/bin/wget -q -O - 'https://sync.afraid.org/u/[token redacted]/?myip='${ip}''
fi
exit 0

… which works like a charm!
So, my question is simple: am I doing this right? Can I rely on these variables to always exist and contain the information I need, or are there any changes planned? Thanks in advance! 

 

tl;dr yes, see this doc for what variables are created https://udhcp.busybox.net/README.udhcpc 77

full info post:

Next time you are doing some detective work I suggest you do a github (or openwrt source text content) search for udhcpc.user to look around on what interacts with it in the source.
You find that such file is called at the end of /usr/share/udhcpc/default.script, and also the /lib/netifd/dhcp.script as added in that ticket you saw.

https://github.com/openwrt/openwrt/blob/master/package/network/config/netifd/files/usr/share/udhcpc/default.script 48

https://github.com/openwrt/openwrt/blob/master/package/network/config/netifd/files/lib/netifd/dhcp.script 23

If you look at the top of both scripts, the very first line is
[ -z "$1" ] && echo "Error: should be run by udhcpc" && exit 1

So yeah they are run by "udhcpc", and that is a busybox tool (embedded device light version of core commandline tools), the dhcp client used by OpenWrt.

The documentation is in its old project page https://udhcp.busybox.net/ 4 that has now been moved to busybox project infrastructure since the application was merged into busybox, and you can find the full client documentation about what variables it generates for use in scripts if you scroll down and click on Client README https://udhcp.busybox.net/README.udhcpc 77

It says:
When an event occurs, udhcpc calls the action script. The script by default is /usr/share/udhcpc/default.script
(for "events" it means something related to DHCP changes)

And then it says

 
The paramaters for enviromental variables are as follows:

$HOME		- The set $HOME env or "/"
$PATH		- the set $PATH env or "/bin:/usr/bin:/sbin:/usr/sbin"
$1		- What action the script should perform
interface	- The interface this was obtained on
ip		- The obtained IP
siaddr		- The bootp next server option
sname		- The bootp server name option
boot_file	- The bootp boot file option
subnet		- The assigend subnet mask
timezone	- Offset in seconds from UTC
router		- A list of routers
timesvr		- A list of time servers
namesvr		- A list of IEN 116 name servers
dns		- A list of DNS server
logsvr		- A list of MIT-LCS UDP log servers
cookiesvr	- A list of RFC 865 cookie servers
lprsvr		- A list of LPR servers
hostname	- The assigned hostname
bootsize	- The length in 512 octect blocks of the bootfile
domain		- The domain name of the network
swapsvr		- The IP address of the client's swap server
rootpath	- The path name of the client's root disk
ipttl		- The TTL to use for this network
mtu		- The MTU to use for this network
broadcast	- The broadcast address for this network
ntpsrv		- A list of NTP servers
wins		- A list of WINS servers
lease		- The lease time, in seconds
dhcptype	- DHCP message type (safely ignored)
serverid	- The IP of the server
message		- Reason for a DHCPNAK
tftp		- The TFTP server name
bootfile	- The bootfile name

I think this interface is not going to change anytime soon, that's a core tool, the dhcp client application used in OpenWrt.

I also think that it should be fairly trivial to add a line that calls network hotplug scripts when DHCP events happen by adding a line in the two linked scripts above, so packages can rely on DHCP events too without having to make a custom user config script like you did.

 

Hmm, more rummaging in OpenWrt sources and it seems there is a dhcp hotplug for scripts found in /etc/hotplug.d/dhcp, but only for events triggered by dnsmasq (the DHCP server used by OpenWrt).
They are not triggered for DHCP client events like what you need.

It does show how you can add hotplugging for DHCP client events, just add the same in the scripts I linked above, and it should then call stuff in /etc/hotplug.d/dhcp for DHCP client events too.

https://github.com/openwrt/openwrt/blob/master/package/network/services/dnsmasq/files/dhcp-script.sh 67

Would be cool if after testing this, someone could open a PR to merge this in OpenWrt. I'm currently away from home and I can't really test much.

 

It seems odhcpd (DHCP server for IPv6) and odhcp6c (DHCP client for IPv6) aren't triggering hotplug scripts either.

Both have a script hook they call every time there is a DHCP update so it's the same thing as above to add the hotplug functionality.
https://github.com/openwrt/openwrt/blob/master/package/network/ipv6/odhcp6c/files/dhcpv6.script 22 for odhcp6c and
https://github.com/openwrt/openwrt/blob/master/package/network/services/odhcpd/files/odhcpd-update 23 for odhcpd

 

an update: I asked on mailing list and Hans Dedecker (a core OpenWrt developer and more or less maintainer of these daemons) https://www.mail-archive.com/[email protected]/msg49345.html 14 said that the odhcpd-update script is not the same thing, and that at the moment odhcpd does not call any script on DHCP lease changes, so it cannot do any hotplug.

 

======== End

 

标签:script,documentation,hotplug,events,DHCP,udhcpc,openwrt
From: https://www.cnblogs.com/lsgxeva/p/17323040.html

相关文章

  • javascript常用的循环对比及性能分析
    结论:js中的for循环只有在处理百万条数据以上才会展示出他的强大性能,和看出明显优势,但是在百万条数据往下甚至到个位数的数据量通常都是for和while还有do...while不相上下,反而后两者更加优势明显下面是测试耗时截图(在不同浏览器也会有所不同,我这是Chrome版本111.0.5563.149)......
  • JavaScript运算符与表达式
    目录一、===二、||三、??与?.???.四、...五、[]{}[]{}一、===严格相等运算符,用作逻辑判断1==1 //返回true1=='1' //返回true,会先将右侧的字符串转为数字,再做比较1==='1' //返回false,类型不等,直接返回falsetypeof查看某个值的类型typeof1 //返回'number'ty......
  • JavaScript中 处理异步的几种方法
    1.回调函数回调(callback)是一个函数被作为一个参数传递到另一个函数里,在那个函数执行完后再执行。假定有两个函数f1和f2,f2等待f1的执行结果,f1()–>f2();如果f1很耗时,可以改写f1,把f2(箭头函数)写成f1的回调函数:functionf1(callback){setTimeout(()=>{letname='小明'......
  • javascript基础练习
     本练习根据w3cschool:https://www.w3cschool.cn/javascript/javascript-conventions.html 1.javascript简介1.1JavaScript是脚本语言JavaScript是一种轻量级的编程语言。JavaScript是可插入HTML页面的编程代码。JavaScript插入HTML页面后,可由......
  • TypeScript type 关键字和 interface 关键字
    前言type和interface都可以声明TS类型。typePoint1={x:number;y:number;};interfacePoint2{x:number;y:number;};它们两个声明这个对象类型有任何区别吗?很明显没有,我认为最能区分它们两个的标志是,type有一个=赋值等号。typetype可以做类......
  • JavaScript 邮箱 验证正则表达式 ,包看懂
    \w就是[0-9a-zA-Z_]\s是[\t\v\n\r\f]\S是[^\t\v\n\r\f]\W是[^0-9a-zA-Z_]\D就是[^0-9]\d就是[0-9].就是[^\n\r\u2028\u2029]。表示几乎任意字符。varreg=/\w{1,30}(\.\w{1,10}){0,2}@\w{1,10}\.\w{1,10}/g\w{1,30}理解为至少有一个字符,最多30个.\w{1,30}理......
  • 前端基础之JavaScript
    目录JS简介JS基础变量与常量基本数据类型数值(number)字符串的常用方法对象的常用方法forEach()splice()map()运算符流程控制函数内置对象JS简介全称JavaScript但是与Java一毛钱关系都没有之所以这么叫是为了蹭Java的热度它是一门前端工程师的编程语言但是它本身有很多逻辑错误(......
  • TypeScript 报错:Type '({ filename: string; createTime: string; filePath: string;
    问题:因为TypeScript不支持直接给一个接口类型的变量赋一个未知的值。如consta:A={ name:'s'};你需要给这样的对象或数组值使用as指定一个类型。正确写法:consta:A={ name:'s'}asA;数组写法一样:consta:A[]=[ { name:'s' }]asA[];使用as将一......
  • typescript vue3 VueDraggable 报错 Uncaught TypeError: Cannot read properties of
    UncaughtTypeError:Cannotreadpropertiesofnull(reading'element')nnotreadpropertiesofnull(reading'index')错误写法就是说子组件需要用div包着,你用其他东西,他无法添加key,然后就会报错。<template#item="{element}"><Todo:detail=......
  • JavaScript 中 new Date().getTime() 方法在 iOS 中的兼容性问题
    JavaScript中newDate(time).getTime()获取时间戳方法在iOS中的兼容性问题在iOS系统的H5页面中获取时间戳方法newDate(time).getTime()存在返回NaN或结果不准确的情况在iPhone8中iOS11.03系统下的H5页面测试newDate(time).getTime()方法测试代码:测试结......