frame

Welcome

Search content for your interest. If you want to get more answers or contribute, click one of the buttons below!

Sign In Register

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

FREE bash script to setup Raspberry Pi 3 onboard Wi-Fi in Access point (AP) or client mode.

edited January 2017 in Plex

This will create a script that when run queries your SSID and Password
and automatically sets up the onboard Wi-Fi adapter as a wireless AP.
There is also commented section that guides you to change the script
if you need to instead setup a connection as a client to an existing network.

Create a script
sudo nano /root/enable_wifi_ap.sh
Copy and paste the script below

#!/usr/bin/env bash
if [ "$EUID" -ne 0 ]
        then echo "Must be root"
        exit
fi
echo -e "Your password must be a minimum of 8 and max 63 characters."
echo -e  "Enter your password?"
read APPASS
echo -e "Enter your SSID"
read APSSID
sudo apt-get autoremove --purge hostapd -yqq
sudo apt-get install -t jessie hostapd dnsmasq -yqq --force-yes
cat > /etc/dnsmasq.conf <<DNS_EOF
interface=wlan0
dhcp-range=10.0.0.2,10.0.0.100,255.255.255.0,12h
DNS_EOF
cd /etc/init.d && /usr/sbin/update-rc.d dnsmasq defaults
cat > /etc/hostapd/hostapd.conf <<HOSTAPD_EOF
interface=wlan0
hw_mode=g
channel=6
ieee80211n=1
auth_algs=1
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
rsn_pairwise=CCMP
wpa_passphrase=$APPASS
ssid=$APSSID
HOSTAPD_EOF
echo -e "Is this a repeat install y/n?"
read REPEAT
if  [[ "$REPEAT" == "n" ]]
then
cp /etc/network/interfaces /etc/network/interfaces.backup
cat >> /etc/network/interfaces <<'INTERFACES_EOF'
iface wlan0 inet static
        address 10.0.0.1
        netmask 255.255.255.0
        pre-up ifconfig wlan0
        hostapd /etc/hostapd/hostapd.conf
 #use the configs below if you want to join a network
 #and remove hostapd and dnsmasq with the following command
 #/usr/sbin/update-rc.d -f dnsmasq remove
 #/usr/sbin/update-rc.d -f hostapd remove
 #rm -rf /etc/hostapd/hostapd.conf
 #rm -rf /etc/dnsmasq.conf
 #uncomment all the lines below change the SSID and Secret to suit the AP you want to connect to as a client
 #and comment all the line out above up to and including iface wlan0 inet static
 #iface wlan0 inet dhcp
 #   wpa-scan-ssid 1
 #   wpa-ap-scan 1
 #   wpa-key-mgmt WPA-PSK
 #   wpa-proto RSN WPA
 #   wpa-pairwise CCMP TKIP
 #   wpa-group CCMP TKIP
 #   wpa-ssid "YourSSID"
 #   wpa-psk "YourSecret"
INTERFACES_EOF
OLD11='iface wlan0 inet manual'
NEW11='#iface wlan0 inet manual'
OLD12='wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf'
NEW12='#wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf'
sed -i 's,'"$OLD11"','"$NEW11"',g' /etc/network/interfaces
sed -i 's,'"$OLD12"','"$NEW12"',g' /etc/network/interfaces
echo "All done!"
reboot
exit
fi
if [[ "$REPEAT" == "y" ]]
then
echo -e "Skipping altering the /etc/network/interfaces file"
echo "All done!"
reboot
exit
fi
 # visit us @ http://www.iiot.co.za

Save an exit [CTRL] +[X], [Y], [ENTER]
Make the script executable
chmod +x /root/enable_wifi_ap.sh
Run the script type
/root/enable_wifi_ap.sh

Restoring your original settings: (if needed)
This script above backed up your existing configuration /etc/network/interfaces file to /etc/network/interfaces.backup
If you need to back out the changes then copy and paste the following code into your shell session.

#stop hostapd and dnsmasq services.
service hostapd stop
service dnsmasq stop
 #remove the dnsmasq autostart 
/usr/sbin/update-rc.d -f dnsmasq remove
 #uninstall and purge the hostapd and dnsmasq packages
sudo apt-get autoremove --purge hostapd -yqq
sudo apt-get autoremove --purge dnsmasq -yqq
 #delete all the config files
rm -rf /etc/dnsmasq.conf
rm -rf /etc/hostapd/hostapd.conf
rm -rf /etc/network/interfaces
 #restore original network config
mv /etc/network/interfaces.backup /etc/network/interfaces
 #restart your device
reboot

Enabling SSH on Raspbian versions 25-11-2016 or later
For headless installations create a blank file called ssh on the boot or dos partition of your MicroSD card.
For desktop versions use the desktop GUI to enable remote ssh access.

Assigning root user a password and switching to root user:
From headless remote SSH shell session or console login as user pi with password raspberry.
From desktop use terminal session login as user pi with password raspberry.
Assign root user a password, enter a new password twice.
sudo passwd root
Switch to root user, enter the password assigned in the step above.
su root

Sign In or Register to comment.

Contact us

info@iiot.co.za

Get In Touch