如何让脚本在 tun0 interface up/down 事件时自动运行?

sud*_*ais 20 networking ifconfig internet interface

我使用 VPN 客户端连接到我的公司服务器。它在启动客户端后创建 tun0 接口。我已经编写了安装特定路由的脚本,指向 tun0 接口并休息以使用正常的 wifi 连接。因此,只有我的办公室相关流量通过 VPN 传输,其余流量通过家庭互联网连接传输。如何让脚本在 tun0 界面上/下事件时自动运行?。

enz*_*tib 24

我不确定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
Run Code Online (Sandbox Code Playgroud)

使其可执行

sudo chmod +x /etc/network/if-up.d/tun-up
Run Code Online (Sandbox Code Playgroud)

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


Kri*_*Dev 7

gksudo gedit /etc/network/interfaces
Run Code Online (Sandbox Code Playgroud)

添加:

auto tun0
iface tun0 inet manual
    up COMMAND
Run Code Online (Sandbox Code Playgroud)

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

up您可以使用post-up, down,代替post-down

文件

面选项

   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-<option>.d/  the  scripts  in  which  are  run (with no
   arguments)  using  run-parts(8)  after  the  option  itself  has   been
   processed.  Please  note  that  as post-up and pre-down are aliases, no
   files in the corresponding directories are processed.  Please  use  if-
   up.d and if-down.d directories instead.

   All  of  these  commands  have  access  to  the  following  environment
   variables.

   IFACE  physical name of the interface being processed

   LOGICAL
          logical name of the interface being processed

   ADDRFAM
          address family of the interface

   METHOD method of the interface (e.g., static)

   MODE   start if run from ifup, stop if run from ifdown

   PHASE  as per MODE, but with finer granularity, distinguishing the pre-
          up, post-up, pre-down and post-down phases.

   VERBOSITY
          indicates whether --verbose was used; set to 1 if so, 0 if not.

   PATH   the   command   search   path:  /usr/local/sbin:/usr/local/bin:???
          /usr/sbin:/usr/bin:/sbin:/bin

   Additionally, all options given in an interface definition  stanza  are
   exported to the environment in upper case with "IF_" prepended and with
   hyphens  converted  to  underscores  and  non-alphanumeric   characters
   discarded.

   When  ifupdown  is  being  called  with  the --all option, before doing
   anything to interfaces, if calls all the hook scripts (pre-up or  down)
   with  IFACE set to "--all", LOGICAL set to the current value of --allow
   parameter  (or  "auto"   if   it's   not   set),   ADDRFAM="meta"   and
   METHOD="none".   After all the interfaces have been brought up or taken
   down, the appropriate scripts (up or post-down) are executed.
Run Code Online (Sandbox Code Playgroud)