aboutsummaryrefslogtreecommitdiff
path: root/autoDHCP
blob: aee73c9c16d0a16f0bc314a267fa40cc63154348 (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
#!/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 'Not yet implemented'
else
    echo "Server name ${server} unknow"
fi