当tun0接口up / down事件时,如何使脚本自动运行?

我使用VPN客户端连接到我的公司服务器。 它在启动客户端后创建tun0接口。 我写了脚本,安装特定的路由指向tun0接口和rest使用普通的wifi连接。 因此,只有我办公室相关的流量通过VPN传输,其余的通过家庭互联网连接。 当tun0接口up / down事件时,如何使脚本自动运行?

我不确定tun0 ,但我认为/etc/network/if-up.d//etc/network/if-down.d/中的脚本分别在接口上升或下降时调用。

在脚本中,您可以根据变量IFACE的内容确定哪个界面感兴趣。

可以肯定的是,在/etc/network/if-up.d/添加一个简单的脚本

 #!/bin/sh # filename: tun-up if [ "$IFACE" = tun0 ]; then echo "tun0 up" >> /var/log/tun-up.log fi 

让它可执行

 sudo chmod +x /etc/network/if-up.d/tun-up 

然后查看up事件是否记录在/var/log/tun-up.log

 gksudo gedit /etc/network/interfaces 

加:

 auto tun0 iface tun0 inet manual up COMMAND 

COMMAND可以是一个命令,比如ip route add something...或者一个带有可执行权限的脚本路径( chmod +x ),最终存储在/etc/network/if-up.d/

而不是你可以使用post-updownpost-down

文件 :

IFACE选项

  The following "command" options are available for every family and method. Each of these options can be given multiple times in a single stanza, in which case the commands are executed in the order in which they appear in the stanza. (You can ensure a command never fails by suffixing them with "|| true".) pre-up command Run command before bringing the interface up. If this command fails then ifup aborts, refraining from marking the interface as configured, prints an error message, and exits with status 0. This behavior may change in the future. up command post-up command Run command after bringing the interface up. If this command fails then ifup aborts, refraining from marking the interface as configured (even though it has really been configured), prints an error message, and exits with status 0. This behavior may change in the future. down command pre-down command Run command before taking the interface down. If this command fails then ifdown aborts, marks the interface as deconfigured (even though it has not really been deconfigured), and exits with status 0. This behavior may change in the future. post-down command Run command after taking the interface down. If this command fails then ifdown aborts, marks the interface as deconfigured, and exits with status 0. This behavior may change in the future. There exists for each of the above mentioned options a directory /etc/network/if-