Instant WLAN Access-Point


SUBMITTED BY: Guest

DATE: Dec. 5, 2013, 2:45 p.m.

FORMAT: Bash

SIZE: 10.8 kB

HITS: 779

  1. #!/bin/bash
  2. ## Instant WLAN Access-Point
  3. ## elektronenblitz63 ubuntuusers.de 2012
  4. ## published under GPL v3
  5. ##
  6. ## Version 1.6.0 vom 31.Januar 2012
  7. # Bridged-Modus möglich
  8. # kleinere Fehler beseitigt
  9. # Ausgaben verbessert
  10. #
  11. ## Version 1.5.2 vom 12.November 2011
  12. # voreingestellte freie DNS geändert
  13. # Restart des Network-Managers erst bei -stop
  14. # entferne iptables-Filter bei -stop
  15. ##
  16. ## Beispielkonfiguration der dnsmasq.conf
  17. ##
  18. # # DHCP-Server dnsmasq aktiv für Interface
  19. #
  20. # interface=wlan0
  21. ## DHCP-Server dnsmasq nicht aktiv für Interface
  22. #
  23. # no-dhcp-interface=eth0
  24. # # IP-Adressbereich / Lease-Time
  25. # dhcp-range=192.168.3.20,192.168.3.25,infinite
  26. #
  27. ## Ende Beispielkonfiguration dnsmasq.conf
  28. ## freie Variablen
  29. ## Konfiguration der Ethernet-Schnittstelle
  30. ## LAN statisch (Standard ist automatisch über DHCP) / Startoption [-f]
  31. laniface=wlan1
  32. laddress=192.168.178.6
  33. lbroadcast=192.168.178.255
  34. lnetmask=255.255.255.0
  35. lgateway=192.168.178.1
  36. lmacaddress=00:12:79:c0:49:ae
  37. #
  38. ## Konfiguration der WLAN-Schnittstelle
  39. ## WLAN statisch
  40. wlaniface=wlan2
  41. waddress=192.168.3.1
  42. wbroadcast=192.168.3.255
  43. wnetmask=255.255.255.0
  44. iptablemask=192.168.0.0/24
  45. ### manuelle DNS (drei DNS Einträge, 1xDomain und 1xSearch sind möglich)
  46. # Beispiel
  47. # dns="nameserver 192.168.178.1 nameserver 192.168.178.1 nameserver 192.168.178.1 domain fritz.box search fritz.box"
  48. dns="nameserver 8.8.4.4 nameserver 8.8.8.8 nameserver 213.73.91.35"
  49. ## dnsmasq-base Konfiguration
  50. # DHCP-Adresspool umfasst x-Adressen
  51. ipaddresses=10
  52. # Basisadresse DHCP-Adresspool (WLAN-IP + X)
  53. wlanbaseip=1
  54. # Lease-Time
  55. leasetime=infinite
  56. ## MAC-Adresse (optional) (Startoption [-m])
  57. lmacaddress=00:12:79:c0:49:ae
  58. ## Pause vor LAN-Verbindungstest
  59. pause=4
  60. ## Proxyserver (squid)
  61. proxy="squid"
  62. ## Proxy Server auf Port x (squid 3128 / tinyproxy 3128 / polipo 8123)
  63. proxyport=3128
  64. ## Bridge-Konfiguration
  65. ## vor Ubuntu 11.x - /usr/sbin/brctl
  66. ## ab Ubuntu 11.x - /sbin/brctl
  67. br_util=/sbin/brctl
  68. bridge0=br0
  69. brdelay=5
  70. brstp=0
  71. ## Steuerung Dienste
  72. ## alt
  73. # hostapdrestart="/etc/init.d/hostapd"
  74. ## neu
  75. hostapdservice="service hostapd"
  76. ## Ende freie Variablen
  77. ##
  78. # Skript
  79. #
  80. pingout=""
  81. ## aut. Adressberechnung DHCP-Range für dnsmasq
  82. ## gemäß Vorgabe WLAN-Schnittstelle
  83. ipaddresses=$[$ipaddresses+$wlanbaseip]
  84. baseendaddr="`echo $waddress | tr -s . " " | awk {'print $4'}`"
  85. basestartaddr="`echo $waddress | tr -s . " " | awk {'print $1,$2,$3'} | tr -s " " .`"
  86. endaddr="$basestartaddr""."$[$startaddr+$ipaddresses]
  87. startaddr="$basestartaddr""."$[$baseendaddr+$wlanbaseip]
  88. pingout=""
  89. A=1
  90. B=0
  91. D=0
  92. P=0
  93. Br=0
  94. if [ "$1" = "-h" ]; then
  95. echo Verwendung: instant_AP.sh [-start] [-restart] [-stop] [-D] [-d] [-f] [-m] [-h] [-B]
  96. echo Syntax:
  97. echo "sudo ./instant_AP.sh wie [-d] startet mit Standardparametern (DHCP)"
  98. echo "sudo ./instant_AP.sh -f statische LAN-Konfiguration"
  99. echo "sudo ./instant_AP.sh -f -m statische LAN-Konfiguration, MAC-Änderung"
  100. echo "sudo ./instant_AP.sh -D verwendet dnsmasq.conf und nicht dnsmasq-base"
  101. echo "sudo ./instant_AP.sh -start -f statische LAN-Konfiguration"
  102. echo "sudo ./instant_AP.sh -restart -f statische LAN-Konfiguration"
  103. echo "sudo ./instant_AP.sh -B Bridged-Modus ohne DHCP-Server. Dynamische und Statische Schnittstellenparameter werden ignoriert"
  104. echo "sudo ./instant_AP.sh -stop beendet den AP"
  105. echo "Ende"
  106. exit
  107. fi
  108. while getopts ":DdfmhPB" OPTION ; do
  109. case $OPTION in
  110. D) echo "vewende dnsmasq.conf und nicht dnsmasq-base"; D=1;;
  111. d) echo "konfiguriere LAN über DHCP"; A=1;;
  112. f) echo "konfiguriere LAN statisch"; A=2;;
  113. m) echo "MAC-Change LAN ein"; B=1;;
  114. P) echo "Portumleitung für Proxy-Server Port" $proxyport "aktiviert"; P=1;;
  115. B) echo "Bridge-Modus aktiviert"; Br=1;;
  116. esac
  117. done
  118. echo "starte gewählte Konfiguration ..."
  119. sleep 2
  120. if [ "$Br" = "1" ]; then A=3
  121. echo "Bridge-Mode aktiviert - Sonstige Parameter für Schnittstelleneinstellungen werden ignoriert"
  122. fi
  123. if [ "$1" != "-start" ]; then
  124. echo "stoppe alle Dienste, und Verbindungen, lösche Itables-Filter ..."
  125. # Konfiguration löschen
  126. /sbin/iptables -F
  127. /sbin/iptables -X
  128. /sbin/iptables -t nat -F
  129. defgw="`route -n | grep UG | awk {'print $2'}`"
  130. /sbin/route del default gw $defgw $laniface
  131. echo '' | tee /etc/resolv.conf
  132. $hostapdservice stop
  133. /sbin/ifconfig $wlaniface down
  134. sleep 1
  135. /sbin/iwconfig $wlaniface mode managed
  136. sleep 1
  137. /sbin/ifconfig $laniface down
  138. sleep 1
  139. /usr/bin/killall dnsmasq
  140. /sbin/sysctl -w net.ipv4.ip_forward=0
  141. /sbin/modprobe -rfv iptable_nat ipt_MASQUERADE xt_conntrack iptable_filter
  142. ## Bridge löschen
  143. /sbin/ifconfig $bridge0 down
  144. sleep 1
  145. $br_util delif $bridge0 $laniface
  146. $br_util delif $bridge0 $wlaniface
  147. $br_util delbr $bridge0
  148. if [ "$1" = "-stop" ]; then
  149. echo
  150. echo "reaktiviere Network-Manager."
  151. service network-manager start
  152. service network-manager restart
  153. echo "WLAN Access-Point Konfiguration beendet."
  154. exit
  155. fi
  156. fi
  157. ## MAC-Adresse abgleichen
  158. if [ "$B" = "1" ]; then
  159. currentmac="`ifconfig $laniface | grep Adresse | awk {'print $6'}`"
  160. echo Schnittstelle $laniface, MAC-Adresse: $currentmac
  161. echo Vorgabe: $lmacaddress
  162. if [ "$currentmac" = "$lmacaddress" ]; then
  163. echo Ãœbereinstimmende MAC-Adresse
  164. else
  165. /sbin/ifconfig $laniface down
  166. /sbin/ip link set dev $laniface addr $lmacaddress
  167. currentmac="`ifconfig $laniface | grep Adresse | awk {'print $6'}`"
  168. echo versuche MAC-Adresse zu ändern ...
  169. echo Schnittstelle $laniface, MAC-Adresse: $currentmac
  170. if [ "$currentmac" = "$lmacaddress" ]; then
  171. echo Änderung der MAC-Adresse erfolgreich!
  172. else
  173. echo Änderung der MAC-Adresse nicht erfolgreich!
  174. echo fahre fort ...
  175. fi
  176. fi
  177. fi
  178. # Grundkonfiguration
  179. echo beende Network-Manager
  180. service network-manager stop
  181. echo "starte alle Dienste, und Verbindungen ..."
  182. # LAN aut.m über DHCP nur wenn kein Bridged-Mode gewählt
  183. if [ "$Br" = "0" ] & [ "$A" = "1" ]; then
  184. echo "starte automatische LAN-Verbindung ..."
  185. /sbin/dhclient $laniface
  186. fi
  187. # LAN statisch nur wenn kein Bridged-Mode gewählt
  188. if [ "$Br" = "0" ] & [ "$A" = "2" ]; then
  189. echo "starte statische LAN-konfiguration ..."
  190. /sbin/ifconfig $laniface down
  191. sleep 2
  192. /sbin/ifconfig $laniface $laddress broadcast $lbroadcast netmask $lnetmask
  193. sleep 2
  194. echo
  195. echo setze Gateway und Route ...
  196. /sbin/route add default gw $lgateway $laniface
  197. sleep 1
  198. echo
  199. echo setze DNS
  200. echo '# erzeugt durch instant_AdHoc.sh' | tee /etc/resolv.conf
  201. echo $dns | awk {'print $1,$2'} | tee -a /etc/resolv.conf
  202. echo $dns | awk {'print $3,$4'} | tee -a /etc/resolv.conf
  203. echo $dns | awk {'print $5,$6'} | tee -a /etc/resolv.conf
  204. echo $dns | awk {'print $7,$8'} | tee -a /etc/resolv.conf
  205. echo $dns | awk {'print $9,$10'} | tee -a /etc/resolv.conf
  206. fi
  207. sleep $pause
  208. ## Vorbereitung Bridge
  209. if [ "$Br" = "1" ]; then
  210. ## vorhandene Schnittstellenkonfiguration löschen
  211. /sbin/ifconfig $wlaniface down
  212. sleep 1
  213. /sbin/ifconfig $wlaniface up
  214. sleep 1
  215. /sbin/ifconfig $wlaniface 0.0.0.0
  216. sleep 1
  217. /sbin/ifconfig $laniface down
  218. sleep 1
  219. /sbin/ifconfig $laniface up
  220. sleep 1
  221. ## Bridge anlegen
  222. $br_util addbr $bridge0
  223. $br_util addif $bridge0 $laniface
  224. $br_util stp $bridge0 $brstp
  225. $br_util setfd $bridge0 $brdelay
  226. sleep 2
  227. ## Dienste steuern
  228. service dnsmasq stop
  229. echo "fordere IP-Adresse für Bridge an ..."
  230. /sbin/dhclient $bridge0
  231. $hostapdservice restart
  232. $br_util addif $bridge0 $wlaniface
  233. else
  234. ## ohne Bridge-Mode
  235. ## WLAN-Schnittstelle statisch konfigurieren
  236. echo "WLAN-Schnittstelle initialisieren ..."
  237. /sbin/ifconfig $laniface up
  238. sleep 1
  239. /sbin/ifconfig $wlaniface $waddress broadcast $wbroadcast netmask $wnetmask
  240. $hostapdservice restart
  241. echo
  242. echo "starte IP-Forward, Masquerading und NAT"
  243. /sbin/iptables -A FORWARD -o $laniface -i $wlaniface -s $iptablemask -m conntrack --ctstate NEW -j ACCEPT
  244. /sbin/iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  245. /sbin/iptables -t nat -A POSTROUTING -o $laniface -j MASQUERADE
  246. /sbin/sysctl -w net.ipv4.ip_forward=1
  247. fi
  248. ## dnsmasq-base starten
  249. echo
  250. echo starte dnsmasq-base
  251. if [ "$D" = "0" ]; then
  252. echo DHCP-Range dnsmasq-base - Startadresse: $startaddr Endadresse: $endaddr
  253. /usr/sbin/dnsmasq -i $wlaniface -I $laniface -F $startaddr,$endaddr,$leasetime
  254. echo
  255. else
  256. ## dnsmasq neu starten
  257. echo "verwende dnsmasq.conf"
  258. /etc/init.d/dnsmasq restart
  259. echo fertig ...
  260. fi
  261. ## Portumleitung für Squid Proxyserver
  262. if [ "$P" = "1" ]; then
  263. /sbin/iptables -t nat -A PREROUTING -i $wlaniface -p tcp --dport 80 -j REDIRECT --to-port $proxyport
  264. ## optional Port 443 HTTPS
  265. # /sbin/iptables -t nat -A PREROUTING -i $wlaniface -p tcp --dport 443 -j REDIRECT --to-port $proxyport
  266. echo "Port 80 (HTTP) " $wlaniface "auf Port $proxyport umgeleitet ("$proxy "Proxyserver)"
  267. echo "Starte" $proxy "Proxyserver ..."
  268. sleep 2
  269. if [ "$proxy" != "squid" ]; then
  270. /etc/init.d/$proxy restart
  271. else
  272. service squid start -n
  273. fi
  274. fi
  275. ## Ausgabe der aktuellen Konfiguration
  276. echo "DNS-Konfiguration"
  277. cat /etc/resolv.conf
  278. echo
  279. /sbin/route -n
  280. echo
  281. if [ "$Br" = "1" ]; then
  282. echo "Konfiguration Bridge:"
  283. $br_util show
  284. echo
  285. /sbin/ifconfig $bridge0 | egrep 'Link|inet Adresse'
  286. echo
  287. fi
  288. echo "Konfiguration LAN:"
  289. /sbin/ifconfig $laniface | egrep 'Link|inet Adresse'
  290. echo
  291. echo "Konfiguration WLAN:"
  292. /sbin/ifconfig $wlaniface | egrep 'Link|inet Adresse'
  293. echo
  294. /sbin/iwconfig $wlaniface | egrep 'IEEE|Power|Mode'
  295. echo
  296. /sbin/iwconfig mon.$wlaniface
  297. exit 0

comments powered by Disqus