惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
The Cloudflare Blog
G
Google Developers Blog
博客园_首页
Martin Fowler
Martin Fowler
Apple Machine Learning Research
Apple Machine Learning Research
L
LangChain Blog
D
Docker
C
Check Point Blog
T
Tailwind CSS Blog
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
V
V2EX
博客园 - 叶小钗
T
The Blog of Author Tim Ferriss
酷 壳 – CoolShell
酷 壳 – CoolShell
IT之家
IT之家
M
MIT News - Artificial intelligence
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 【当耐特】
GbyAI
GbyAI

博客园 - CalvinChu

vyos syslog配置 How to upgrade pip on Debian Wheezy 指数数据解密,懂的都懂 OpenWRT and VPN Passthrough - CalvinChu 如何将 find 命令与 exec 一起使用 CentOS 7 下搭建 OpenVPN - CalvinChu How to get the ip ranges of domain esxi 直通sata控制器 Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_P xRDP – Detected issues with Debian 11 – Oh No ! Something has gone wrong…. qemu-mips环境搭建跳坑指南 ImportError: libSM.so.6: cannot open shared object file: No such file or directory openwrt(lede)中,PPtP Client需要安装的软件包 MAC OSX VPN自动添加静态路由 - CalvinChu fbprophet安装和使用 CentOS7安装iptables防火墙 CENTOS7下安装REDIS mysql忘记root密码解决办法 iptables命令(备忘)
OpenWrt中的Hotplug脚本
CalvinChu · 2021-02-05 · via 博客园 - CalvinChu

Hotplug,直译就是热插拔。在OpenWrt中,无论何时一个设备从系统中增删,都产生一个“热插拔事件”。

每次网络接口启动(up)或者关闭(down)的时候,所有在/etc/hotplug.d/iface/目录中的脚本都会以字母顺序执行。根据一个不成文的规则,会在每个脚本的前面加上一个数字前缀来设置正确的运行顺序。这就是为什么脚本名称都像:/etc/hotplug.d/iface/<nn>-<scriptname>的原因。

在iface hotplug脚本中有三个主要的环境变量:

变量名称 描述
ACTION "ifup" 或者 "ifdown"
INTERFACE 网络接口的名称,如"wan"
DEVICE 物理设备的名称,如"br-lan"

利用hotplug脚本可以在网络接口启动或关闭时执行相应的动作,新建:/etc/hotplug.d/iface/99-ipreport:

vi /etc/hotplug.d/iface/99-ipreport

#!/bin/bash
[ "$ACTION" = ifup ] || exit 0
[ "$INTERFACE" = wan ] || exit 0
ip=$(ifconfig pppoe-wan | grep inet | awk '{print $2}' | cut -c 6-)
echo $ip > /tmp/ip.txt

会生成一个/tmp/ip.txt,里面有个外网ip地址

或者换一种if写法,将脚本外置方便修改

vi /etc/hotplug.d/iface/99-ipreport

#!/bin/bash
if [ $ACTION=ifup -a $INTERFACE=wan ]; then
  sh /root/pppoe-success.sh
fi

/root/pppoe-success.sh的内容为访问一个网址汇报ip地址

vi /root/pppoe-success.sh
#!/bin/bash
curl -s '网址' > /dev/null

上面用到了curl,openwrt默认没有,可以在luci后台搜索curl安装

然后设置执行权限,ipreport无需设置执行权限,只需要设置外部脚本pppoe-success.sh执行权限即可

chmod +x /root/pppoe-success.sh

在luci后台将wan断开,然后再连接,或者直接重启路由器,即可看到效果