aboutsummaryrefslogtreecommitdiff
path: root/autoDHCP
blob: f6e144d6cfc1d04f22e9ce33ff60f7beb30ad621 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/env bash

interface="";
server="dhcpd"
ip="10.5.5.11"
range="/24"

# Todo : List interface ✓ 
#        Select first ✓
#        Select dhcpd or dnsmasq ✓
#        Select ip range ✓
#        Select ip ✓
#        Help ✓
#        Install from Makefile ✓

# Default interface is the first non-wireless interfaces (sorted alpha-num)

if [ "$1" == "dns" ] ; then
    server="dnsmasq"
    shift
fi

if [ "$1" == "help" -o "$1" == "-h" -o "$1" == "--help" ] ; then
    echo 'Usage : autoDHCP [interface] [ip/range] [dns]'
    echo ''
    echo 'You also put the server type at the start of the command'
    echo 'Ex :'
    echo '> autoDHCP wlp3s0 10.5.5.11/24'
    echo '> autoDHCP eth0 192.168.0.1 dns'
    echo '> autoDHCP dns eth0 192.168.0.1/30'
fi



if [ "$1" ] ; then
    interface="$1"
    echo "Interface set to ${interface}"
fi

if [ "$2" ] ; then
    if [[ "$2" =~ .*/.* ]] ; then
        ip=$(echo $2 | cut -d'/' -f1)
        range="`echo $2 | cut -d'/' -f2 `"
        echo "Range set to ${range}"
    else
        ip="$2"
    fi
    echo "Ip set to ${ip}"
fi

if [ "$3" == "dns" ] ; then
    server="dnsmasq"
fi







function guess_nowifi {
    for i in `ls /sys/class/net/`; do
        if [ ! -d "/sys/class/net/$i/wireless" ] ; then 
            if [ "$interface" ] ; then 
                if [[ "$i" < "$înterface" ]] ; then
                    interface="$i"
                fi
            else
                interface="$i"
            fi
        fi
    done
}



if [ -z "$interface" ] ; then
    guess_nowifi
fi

sudo ip l set "$interface" up
sudo ip a r "${ip}/${range}" dev "$interface"

if [ "$server" == "dhcpd" ] ; then 
    sudo /usr/bin/dhcpd -4 -q -pf /run/dhcpd4.pid "$interface"
elif [ "$server" == "dnsmasq" ] ; then

    echo 'Configure dnsmasq'

    ip_start=$(echo "${ip}" | sed 's/\(\([0-9]\{1,3\}\.\)\{3\}\)[0-9]\{1,3\}/\1/')
    ip_start_tmp=$(echo "${ip}" | sed 's/\(\([0-9]\{1,3\}\.\)\{3\}\)//')
    ip_start_tmp=$((ip_start_tmp+1))

    dhcp_range=$(echo ${ip_start}{$ip_start_tmp\,,254}),12h
    echo "$dhcp_range"

    cat <<< $(
    echo "
        interface=${interface}
        listen-address=${ip}
        bind-interfaces
        dhcp-range=${dhcp_range}"
    ) > /tmp/dnsmasq.conf
    sudo dnsmasq --conf-file=/tmp/dnsmasq.conf

else
    echo "Server name ${server} unknow"
fi

echo 'Should be ready ;)'