Come si esegue uno script dopo l'apertura di un'interfaccia di rete?

7

Esiste un modo per eseguire uno script ogni volta che un'interfaccia di rete termina? Oppure c'è una risposta equivoca a questa domanda Debian su OSX?

    
posta Matt 28.11.2011 - 02:09
fonte

4 risposte

1

Non ho ancora provato questa soluzione - ma ho trovato questo thread: link

You should consider using crankd, which precisely allows you to run scripts in response to many system events such as network changes, filesystem activity, application launching, etc.

As I couldn't find any sensible documentation, I also wrote a small blog post on getting started using crankd.

    
risposta data 01.12.2011 - 15:49
fonte
8

I seguenti lavori su Mac OS X 10.11.3 (El Capitan)

crea un file: networkchange.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>networkchange</string>
  <key>LowPriorityIO</key>
  <true/>
  <key>ProgramArguments</key>
  <array>
    <string>/Users/Shared/bin/networkchange.sh</string>
  </array>
  <key>WatchPaths</key>
  <array>
    <string>/etc/resolv.conf</string>
    <string>/var/run/resolv.conf</string>
    <string>/private/var/run/resolv.conf</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

E registralo con:

launchctl load networkchange.plist
launchctl start networkchange

tieni presente che lo script viene eseguito dall'utente che registra il plist.

    
risposta data 23.03.2016 - 18:39
fonte
6

Un agente launchd che guarda /etc/resolv.conf , e due file .plist relativi alla rete sotto /Library/Preferences/SystemConfiguration/ sembra funzionare per me (in Mac OS X 10.8.4):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
 "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>ifup.ddns</string>

  <key>LowPriorityIO</key>
  <true/>

  <key>ProgramArguments</key>
  <array>
    <string>/Users/Shared/bin/ddns-update.sh</string>
  </array>

  <key>WatchPaths</key>
  <array>
    <string>/etc/resolv.conf</string>
    <string>/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist</string>
    <string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
  </array>

  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

In precedenza avevo usato solo /etc/resolv.conf, ma c'erano casi in cui non era abbastanza.

    
risposta data 02.08.2013 - 00:18
fonte
1

Ho finito con questo script:

#!/bin/bash

COUNTER=0
while [ $COUNTER -lt 10 ]; do
    let COUNTER=COUNTER+1

    # assign to test at each iteration otherwise the output won't be changed
    test=$(ifconfig -a inet 2>/dev/null | sed -n -e '/127.0.0.1/d' -e '/0.0.0.0/d' -e '/inet/p' | wc -l)

    if [ "${test}" -gt 0 ]; then
        # do the job here

        # exit the loop
        let COUNTER=COUNTER+10
        break
    fi

    # give a little time for a network to get up
    sleep 2
done

Controlla una rete 10 volte e la rete ha 2 * 10 secondi per alzarsi.

    
risposta data 03.08.2017 - 12:23
fonte

Leggi altre domande sui tag