Advertisement
Guest User

Untitled

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