Come faccio a far funzionare automaticamente lo script quando tun0 interfaccia gli eventi su / giù?


15

Uso il client VPN per connettermi ai miei server aziendali. Crea l'interfaccia tun0 dopo aver avviato il client. Ho scritto uno script che installa percorsi specifici che puntano all'interfaccia tun0 e resto per usare la normale connessione wifi. In tal modo, solo il traffico relativo al mio ufficio passa tramite VPN e il resto passa tramite la connessione Internet domestica. Come faccio a far funzionare automaticamente lo script quando tun0 interfaccia gli eventi su / giù?.

Risposte:


17

Non ne sono sicuro tun0, ma penso che la sceneggiatura sia in /etc/network/if-up.d/e/etc/network/if-down.d/ sia inserito invocato quando un'interfaccia va su o giù, rispettivamente.

All'interno dello script è possibile determinare quale interfaccia è interessata dal contenuto della variabile IFACE .

A dire il vero, aggiungi un semplice script al /etc/network/if-up.d/contenuto

#!/bin/sh
# filename: tun-up

if [ "$IFACE" = tun0 ]; then
  echo "tun0 up" >> /var/log/tun-up.log
fi

renderlo eseguibile

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

quindi vedere se gli eventi up sono stati registrati /var/log/tun-up.log


1
Grazie. Sto arrivando sotto il messaggio syslog e gli script non sono affatto invocati. / etc / network / interfaces non ha informazioni diverse dal loopback. 9 maggio 15:26:48 mypc NetworkManager [869]: SCPlugin-Ifupdown: dispositivo aggiunto (percorso: / sys / devices / virtual / net / tun0, iface: tun0): nessuna configurazione ifupdown trovata.
sudurais,

5
gksudo gedit /etc/network/interfaces

Inserisci:

auto tun0
iface tun0 inet manual
    up COMMAND

COMMANDpuò essere un comando, come ip route add something...o un percorso di script con autorizzazioni eseguibili ( chmod +x), eventualmente memorizzato in /etc/network/if-up.d/.

Invece di upche si può usare post-up, down, post-down.

Documentazione :

OPZIONI 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-<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

Ho usato systemdper eseguire uno script doponetwork-online.target . La mia sceneggiatura <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

3.) sudo systemctl start my-script.service

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.