tun0 인터페이스 업 / 다운 이벤트가 발생하면 스크립트가 자동으로 실행되도록하려면 어떻게해야합니까?


15

VPN 클라이언트를 사용하여 회사 서버에 연결합니다. 클라이언트를 시작한 후 tun0 인터페이스를 만듭니다. 특정 경로를 설치하고 tun0 인터페이스를 가리키는 스크립트를 작성했으며 일반적인 wifi 연결을 사용하도록 휴식했습니다. 따라서 내 사무실 관련 트래픽 만 VPN을 통해 이동하고 나머지는 홈 ​​인터넷 연결을 통해 이동합니다. tun0 인터페이스 가동 / 정지 이벤트시 스크립트가 자동으로 실행되도록하려면 어떻게해야합니까?

답변:


17

나는 확실히에 대한 아니에요 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


1
감사. syslog 메시지가 표시되지 않고 스크립트가 전혀 호출되지 않습니다. / etc / network / interfaces에는 루프백 이외의 정보가 없습니다. 5 월 9 일 15:26:48 mypc NetworkManager [869] : SCPlugin-Ifupdown : 장치 추가됨 (경로 : / sys / devices / virtual / net / tun0, iface : tun0) : ifupdown 구성이 없습니다.
sudurais

5
gksudo gedit /etc/network/interfaces

더하다:

auto tun0
iface tun0 inet manual
    up COMMAND

COMMANDip 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.

0

나는 systemd후에 스크립트를 실행하는 데 사용 했습니다 network-online.target. 내 스크립트 <path>/script.sh.

1.) sudo systemctl edit --force --full my-script.service:

[Unit]
Description=My script after network available
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=root
ExecStart=<path>/script.sh

[Install]
WantedBy=multi-user.target

2.) sudo systemctl enable my-script.service

삼.) sudo systemctl start my-script.service

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.