File: start_concordance_dhcpd.sh

package info (click to toggle)
concordance 1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,700 kB
  • sloc: cpp: 7,203; sh: 4,621; ansic: 882; python: 707; perl: 171; makefile: 132; xml: 34
file content (59 lines) | stat: -rw-r--r-- 1,861 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash

# vim:tw=80:ai:tabstop=4:softtabstop=4:shiftwidth=4:expandtab

LOG=/dev/null
PID_FILE=/var/run/dnsmasq_concordance.pid
NM_WAIT_COUNT=3
LOCAL_IP='169.254.1.1'
REMOTE_IP='169.254.1.2'
NETMASK='255.255.0.0'
export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
IPTABLES_RULE="INPUT -p udp -i $INTERFACE --dport 67 -j ACCEPT"
NMCLI=$(which nmcli)

check_nm() {
    IS_RUNNING=0
    if [ ! -z "$NMCLI" ] && [ -x "$NMCLI" ] ; then
        NM_STATUS=$($NMCLI -t -f RUNNING nm)
        if [ "$NM_STATUS" = "running" ]; then
            IS_RUNNING=1
        fi
    fi
    return $IS_RUNNING
}

if [ "$ACTION" = "add" ]; then
    check_nm
    if [ "$?" -eq 1 ]; then
        echo "NetworkManager found running." >>$LOG
        COUNT=0
        while [ "$COUNT" -lt "$NM_WAIT_COUNT" ]; do
            echo "Waiting for NetworkManager." >>$LOG
            $NMCLI -t -f DEVICE,STATE dev \
                | grep "^${INTERFACE}\:connecting" >>$LOG 2>&1
            if [ "$?" = "0" ]; then
                echo "Disconnecting $INTERFACE via NetworkManager." >>$LOG
                $NMCLI dev disconnect iface $INTERFACE >>$LOG 2>&1
                break
            fi
            let COUNT=COUNT+1
            sleep 1
        done
    fi
    echo "Configuring $INTERFACE interface." >>$LOG
    ifconfig $INTERFACE $LOCAL_IP netmask $NETMASK >>$LOG 2>&1
    if [ -x `which iptables` ]; then
        echo "Adding iptables rule." >>$LOG
        iptables -I $IPTABLES_RULE >>$LOG 2>&1
    fi
    dnsmasq --port=0 --interface=$INTERFACE --bind-interfaces --leasefile-ro \
            --dhcp-range=$REMOTE_IP,$REMOTE_IP --pid-file=$PID_FILE
else
    echo "Stopping dnsmasq." >>$LOG
    kill $(cat $PID_FILE)
    if [ -x `which iptables` ]; then
        echo "Removing iptables rule." >>$LOG
        iptables -D $IPTABLES_RULE >>$LOG 2>&1
    fi
fi