#!/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