install_hotspot.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #!/bin/bash
  2. # Based on Adafruit Learning Technologies Onion Pi project
  3. # But mainly fixed by Raspberry Pi FR...
  4. if (( $EUID != 0 )); then
  5. echo "This must be run as root. Try 'sudo bash $0'."
  6. exit 1
  7. fi
  8. echo "
  9. $(tput setaf 2)              .~~.   .~~.
  10. $(tput setaf 6)   /         $(tput setaf 2)'. \ ' ' / .'$(tput setaf 6)         \
  11. $(tput setaf 6)  |   /       $(tput setaf 1).~ .~~~..~.$(tput setaf 6)      \   |
  12. $(tput setaf 6) |   |   /  $(tput setaf 1) : .~.'~'.~. :$(tput setaf 6)   \   |   |
  13. $(tput setaf 6)|   |   |   $(tput setaf 1)~ (   ) (   ) ~$(tput setaf 6)   |   |   |
  14. $(tput setaf 6)|   |  |   $(tput setaf 1)( : '~'.~.'~' : )$(tput setaf 6)   |  |   |
  15. $(tput setaf 6)|   |   |   $(tput setaf 1)~ .~ (   ) ~. ~ $(tput setaf 6)  |   |   |
  16. $(tput setaf 6) |   |   \   $(tput setaf 1)(  : '~' :  )$(tput setaf 6)   /   |   |
  17. $(tput setaf 6)  |   \       $(tput setaf 1)'~ .~~~. ~'$(tput setaf 6)       /   |
  18. $(tput setaf 6)   \              $(tput setaf 1)'~'$(tput setaf 6)          /
  19. "
  20. echo "$(tput setaf 6)This script will configure your Raspberry Pi as a wireless access point.$(tput sgr0)"
  21. read -p "$(tput bold ; tput setaf 2)Press [Enter] to begin, [Ctrl-C] to abort...$(tput sgr0)"
  22. echo "$(tput setaf 6)Updating packages...$(tput sgr0)"
  23. apt-get update -y
  24. echo "Installing dnsmasq"
  25. apt install dnsmasq -y
  26. systemctl stop dnsmasq
  27. echo "Configuring Wlan0 static IP for 192.168.4.1/24"
  28. echo "interface wlan0
  29. static ip_address=192.168.4.1/24
  30. nohook wpa_supplicant" >> /etc/dhcpcd.conf
  31. echo "Restarting dhcpcd..."
  32. service dhcpcd restart
  33. echo "Configuring dnsmasq..."
  34. mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
  35. echo "interface=wlan0 # Use the require wireless interface - usually wlan0
  36. dhcp-range=192.168.4.2,192.168.4.20,255.255.255.0,24h" > /etc/dnsmasq.conf
  37. echo "Enabling dnsmasq and restart..."
  38. systemctl unmask dnsmasq
  39. systemctl enable dnsmasq
  40. systemctl restart dnsmasq
  41. echo "Unlock wlan soft lock with rfkill..."
  42. if command -v rfkill &> /dev/null
  43. then
  44. rfkill unblock wlan
  45. fi
  46. echo "Install and enable hostapd..."
  47. apt-get install hostapd -y
  48. systemctl unmask hostapd
  49. systemctl enable hostapd
  50. echo "Configuring Hostapd !"
  51. echo "Choose the hostname for your new network (1-32 char, try to privilege ASCII chars) :"
  52. read ssid
  53. echo "Choose the password for your new network (minimum 8 char):"
  54. read passphrase
  55. echo "interface=wlan0
  56. driver=nl80211
  57. ssid=$ssid
  58. hw_mode=g
  59. channel=7
  60. wmm_enabled=0
  61. macaddr_acl=0
  62. auth_algs=1
  63. ignore_broadcast_ssid=0
  64. wpa=2
  65. wpa_passphrase=$passphrase
  66. wpa_key_mgmt=WPA-PSK
  67. wpa_pairwise=TKIP
  68. rsn_pairwise=CCMP
  69. " > /etc/hostapd/hostapd.conf
  70. echo 'DAEMON_CONF="/etc/hostapd/hostapd.conf"' >> /etc/default/hostapd
  71. echo "Starting hostapd..."
  72. systemctl start hostapd
  73. echo "$(tput setaf 6)Setting IP forwarding to start at system boot...$(tput sgr0)"
  74. cp /etc/sysctl.conf /etc/sysctl.bak
  75. echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
  76. echo "up iptables-restore < /etc/iptables.ipv4.nat" >> /etc/network/interfaces
  77. echo "$(tput setaf 6)Activating IP forwarding...$(tput sgr0)"
  78. sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
  79. echo "$(tput setaf 6)Setting up IP tables to interconnect ports...$(tput sgr0)"
  80. iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  81. iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT
  82. iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
  83. echo "$(tput setaf 6)Saving IP tables...$(tput sgr0)"
  84. sh -c "iptables-save > /etc/iptables.ipv4.nat"
  85. echo "Add auto start on startup the Raspberry Pi."
  86. sed -i '/exit 0/ i iptables-restore < /etc/iptables.ipv4.nat' /etc/rc.local
  87. echo "$(tput setaf 6)Rebooting...$(tput sgr0)"
  88. reboot
  89. exit 0