From 17e43b42b896d974ad4f7e480b66eba502af8c40 Mon Sep 17 00:00:00 2001 From: ache Date: Mon, 4 Feb 2019 15:36:10 +0100 Subject: Merge tethapp and autoDHCP --- autoDHCP.sh | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- tethapp.sh | 26 +++++------ 2 files changed, 139 insertions(+), 27 deletions(-) diff --git a/autoDHCP.sh b/autoDHCP.sh index c03fdae..67109bc 100755 --- a/autoDHCP.sh +++ b/autoDHCP.sh @@ -2,10 +2,15 @@ interface=""; server="dhcpd" -ip="10.5.5.11" +ip="10.5.6.11" range="24" interface_internet="" +# For Wifi teth +ssid=🩄 +passphrase=chocolat + + # Todo : List interface ✓ # Select first ✓ # Select dhcpd or dnsmasq ✓ @@ -18,6 +23,14 @@ interface_internet="" # Default interface is the first non-wireless interfaces (sorted alpha-num) +function guess_wifi { + for i in `ls /sys/class/net/`; do + if [ -d "/sys/class/net/$i/wireless" ] ; then + _interface="$i" + fi + done + echo "${_interface}" +} function guess_nowifi { for i in `ls /sys/class/net/`; do if [ ! -d "/sys/class/net/$i/wireless" ] ; then @@ -40,24 +53,26 @@ function guess_internet { cut -d' ' -f 2) } - - - - - if [ "$1" == "dns" ] ; then server="dnsmasq" shift elif [ "$1" == "teth" ] ; then interface_internet=$(guess_internet) shift -elif [ "$1" == "teth_dns" ] ; then +elif [ "$1" == "teth_dns" ] || [ "$1" == "teth_dnsmasq" ] ; then interface_internet=$(guess_internet) server="dnsmasq" shift -fi - -if [ "$1" == "help" -o "$1" == "-h" -o "$1" == "--help" ] ; then +elif [ "$1" == "teth_wifi_dnsmasq" ] ; then + interface_internet=$(guess_internet) + interface=$(guess_wifi) + server="dnsmasq" + hotspot="yes" + shift +elif [ "$1" == "quit" ] || [ "$1" == "close" ] ; then + sudo pkill dnsmasq + exit +elif [ "$1" == "help" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then echo 'Usage : autoDHCP [interface] [ip/range] [dns]' echo '' echo 'You also put the server type at the start of the command' @@ -65,10 +80,10 @@ if [ "$1" == "help" -o "$1" == "-h" -o "$1" == "--help" ] ; then 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' + exit fi - if [ "$1" ] ; then interface="$1" echo "Interface set to ${interface}" @@ -100,6 +115,12 @@ if [ -z "$interface" ] ; then guess_nowifi fi + +######################################################################### +######################## ACTION ######################################### +######################################################################### + + sudo ip l set "$interface" up sudo ip a r "${ip}/${range}" dev "$interface" #sudo ip r a "${ip}/${range}" dev "$interface" src "${ip}" @@ -109,6 +130,7 @@ if [ "$server" == "dhcpd" ] ; then elif [ "$server" == "dnsmasq" ] ; then echo 'Configure dnsmasq' + pidFile=$(mktemp) 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\}\)//') @@ -121,11 +143,16 @@ elif [ "$server" == "dnsmasq" ] ; then echo " interface=${interface} listen-address=${ip} - bind-interfaces dhcp-range=${dhcp_range}" ) > /tmp/dnsmasq.conf - sudo dnsmasq --conf-file=/tmp/dnsmasq.conf - + if sudo dnsmasq --conf-file=/tmp/dnsmasq.conf --pid-file="${pidFile}"; then + echo 'Ok dnsmasq prĂȘt' + echo "PID : ${pidFile}" + else + echo 'Erreur : Impossible de dĂ©marrer dnsmasq' + echo 'Abandon' + exit 1 + fi else echo "Server name ${server} unknow" fi @@ -139,5 +166,88 @@ if [ -n "$interface_internet" ] ; then sudo iptables -A FORWARD -i "${interface}" -o "${interface_internet}" -j ACCEPT fi +if [ "${hotspot}" == "yes" ] ; then + echo "Configuration de hostapd" + + cat <<< $( + echo " +interface=${interface} +ssid=${ssid} + +wpa=1 +wpa_passphrase=${passphrase} + +channel=1 +hw_mode=g + +logger_syslog=-1 +logger_syslog_level=2 +logger_stdout=-1 +logger_stdout_level=2 + +ctrl_interface=/var/run/hostapd +ctrl_interface_group=0 + +beacon_int=100 +dtim_period=2 +max_num_sta=255 +rts_threshold=2347 +fragm_threshold=2346 +macaddr_acl=0 +auth_algs=3 +ignore_broadcast_ssid=0 + +wmm_enabled=1 +wmm_ac_bk_cwmin=4 +wmm_ac_bk_cwmax=10 +wmm_ac_bk_aifs=7 +wmm_ac_bk_txop_limit=0 +wmm_ac_bk_acm=0 +wmm_ac_be_aifs=3 +wmm_ac_be_cwmin=4 +wmm_ac_be_cwmax=10 +wmm_ac_be_txop_limit=0 +wmm_ac_be_acm=0 +wmm_ac_vi_aifs=2 +wmm_ac_vi_cwmin=3 +wmm_ac_vi_cwmax=4 +wmm_ac_vi_txop_limit=94 +wmm_ac_vi_acm=0 +wmm_ac_vo_aifs=2 +wmm_ac_vo_cwmin=2 +wmm_ac_vo_cwmax=3 +wmm_ac_vo_txop_limit=47 +wmm_ac_vo_acm=0 + +vht_oper_chwidth=3 +eap_message=hello +eapol_key_index_workaround=0 +eap_server=0" + ) > /tmp/hostapd.conf + + echo "Lancement du hostpot Wifi" + if sudo hostapd /tmp/hostapd.conf ; then + echo 'Yeah, i wish you enjoyed it' + else + echo 'Error: there is a problem with hostapd' + fi +else + echo 'Should be ready ;)' + sleep 100d +fi + +echo 'Clean des iptables' +sudo iptables --flush +sudo iptables -t nat --flush + +echo 'Clean config' + +rm /tmp/dnsmasq_hotspot.conf /tmp/hostapd.conf /tmp/dnsmasq.conf 2> /dev/null + +if [ "${server}" == "dnsmasq" ] ; then + echo 'Switch off dnsmasq' + sudo kill $(cat $pidFile) +fi -echo 'Should be ready ;)' +echo 'Bye 💋' +echo 'Have a nice day <3' diff --git a/tethapp.sh b/tethapp.sh index adc9edd..b980f75 100755 --- a/tethapp.sh +++ b/tethapp.sh @@ -2,19 +2,20 @@ # Ceci est un script bash ayant pour but de crĂ©er un partage de connection # -# +# ssid=🩄 passphrase=chocolat -ip=10.5.5.11 -network=10.5.5.0/24 -dhcp_range=$(echo 10.5.5.{12\,,254}),12h +ip=10.5.6.11 +range=24 +network=10.5.6.0/24 +dhcp_range=$(echo 10.5.6.{12\,,254}),12h function guess_wifi { for i in `ls /sys/class/net/`; do - if [ -d "/sys/class/net/$i/wireless" ] ; then + if [ -d "/sys/class/net/$i/wireless" ] ; then _interface="$i" fi done @@ -38,7 +39,7 @@ function guess_internet { echo 'Voici la configuration des interfaces :' sudo ip a echo -echo +echo echo 'Veuillez vĂ©rifier la table de routage :' sudo ip r echo @@ -57,22 +58,22 @@ fi echo "Interface Wifi : ${interface}" -if [ -z "${2}" ] ; then +if [ -z "${2}" ] ; then echo "DĂ©tection automatique de l'interface connectĂ©e Ă  internet" interface_internet="$(guess_internet)" else interface="${2}" fi -echo "${interface_internet}" +echo "Interface internet : ${interface_internet}" # sudo iw dev wlp3s0 interface add wlan1 type station sudo pkill dnsmasq -echo "Configuration de l'IP 10.5.5.11" -sudo ifconfig "${interface}" up 10.5.5.11 netmask 255.255.255.0 +echo "Configuration de l'IP 10.5.6.11" +sudo ip a r "${ip}/${range}" dev "$interface" echo "Configuration de DNSQMASQ" @@ -85,7 +86,7 @@ dhcp-range=${dhcp_range}" -echo 'Lancement de DNSMASQ (DHCP sur le rĂ©seau 10.5.5.0/24)' +echo 'Lancement de DNSMASQ (DHCP sur le rĂ©seau 10.5.6.0/24)' if sudo dnsmasq --conf-file=/tmp/dnsmasq_hotspot.conf ; then echo 'Ok dnsmasq prĂȘt' else @@ -98,10 +99,11 @@ echo "Activation de l'interface ${interface}" sudo ip link set "${interface}" up echo 'Ajout du rĂ©seau dans la table de routage' -sudo ip r add 10.5.5.0/24 via 10.5.5.11 dev "${interface}" +sudo ip r add 10.5.6.0/24 via 10.5.6.11 dev "${interface}" echo "Routage des connections internet" +sudo sysctl net.ipv4.ip_forward=1 sudo iptables -t nat -A POSTROUTING -o "${interface_internet}" -j MASQUERADE sudo iptables -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT sudo iptables -A FORWARD -i "${interface}" -o "${interface_internet}" -j ACCEPT -- cgit v1.2.3