1

Switch Raspberry Hotspot "RaspAP" (wlan0) to a simple Wifi Device via a script

What is missing

    #!/bin/bash
# This script will stop the RASPAP Hotspot (AP+DHP) and will Switch your Raspberry to a WIFI Client
# Be care : Before to do that, be sure that you have the correct SSID in : wpa_supplicant.conf

# Run : bash sap2cl.sh

# First :
# sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
#
#       ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
#       update_config=1
#       country=FR
#       network={
#        ssid="Freebox-CKL-I"
#        psk="xxxxxxxxx"
#       }

echo "Stopping hostapd, dnsmasq & dhcpcd"
sudo systemctl stop hostapd.service
sudo systemctl stop dnsmasq.service
sudo systemctl stop dhcpcd.service

echo "Restart wpa_supplicant"
sudo wpa_supplicant -B -Dnl80211,wext -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0

echo "Done."
exit

Nothing happen, after running the script : I can't find the IP address of my Raspberry through a network scanner

3 Answers3

1

Was just having a look through this thread. I created a project a few years ago and wanted to find what has been discussed above, but rather than switching every reboot, I wanted it to connect to the local network if it found it, and if not (when I was out in the field), revert to ad-hoc.(where I could use it as a standalone webserver). I actually found this on another project it has been pretty useful to me, if that is what you need, (and like me, can't code, only follow instructions!) :)

DAvid P
  • 11
  • 1
0

Finally I got the solution. The only point is that I need a reboot to switch from Client to AP. But this is not really an issue in my case. However if there could be a solution in the last script I'm openminded

See script : scl2ap.sh

Thanks

Wifi

Activate Wifi in /etc/wpa_supplicant/wpa_supplicant.conf

    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    country=FR
    network={
        ssid="Freebox-CKL-I"
        psk="xxxxxxxxx"
    }

Kill and restart Wifi

sudo pkill wpa_supplicant
sudo wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0 -B -Dnl80211,wext

Access point:

The Raspberry Pi needs to have the hostapd access point software package installed:

sudo apt-get install hostapd

To create a new configuration file : /etc/hostapd/hostapd.conf Change : country, ssid , wpa_passphrase : to match with yours

    country_code=FR
    interface=wlan0
    ssid=LapemNetwork
    hw_mode=g
    channel=7
    macaddr_acl=0
    auth_algs=1
    ignore_broadcast_ssid=0
    wpa=2
    wpa_passphrase=password
    wpa_key_mgmt=WPA-PSK
    wpa_pairwise=TKIP
    rsn_pairwise=CCMP

Verify that configuration file location in /etc/init.d/hostapd containing is there, if not add. DAEMON_CONF=/etc/hostapd/hostapd.conf

Enable the wireless access point service and set it to start when your Raspberry Pi boots:

sudo systemctl unmask hostapd
sudo systemctl enable hostapd

DHCP Distribution

In order to provide network management services (DNS, DHCP) to wireless clients install dnsmasq

sudo apt-get install dnsmasq

To configure the distribution DHCP IP range we need to configure /etc/dnsmasq.conf At the bottom of the file we need to add the following lines.

    interface=wlan0
    dhcp-range=192.168.0.100,192.168.0.200,12h

activate the dnsmasq service at boot: sudo systemctl enable dnsmasq.service


AP as static

AP needs to have a static address: configured in etc/dhcpcd.conf

Defaut (DHCP) sudo cp /etc/dhcpcd.conf /etc/dhcpcd-dynamic.conf

Static add at the end of the file sudo nano /etc/dhcpcd.conf

    #Static IP
    interface wlan0
        static ip_address=192.168.0.1/24
        nohook wpa_supplicant

sudo cp /etc/dhcpcd.conf /etc/dhcpcd-static.conf

Default mode AP

Command to run to activate AP for next boot

sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl enable dnsmasq.service
sudo cp  /etc/dhcpcd-static.conf /etc/dhcpcd.conf

SWITCH : AP --> Client : definive

file : sap2cl.sh

Effect is dynamic and definive --> Next start will be as Client mode

#!/bin/bash
# This script will stop Hotspot (AP+DHCP) and will Switch your Raspberry to a WIFI Client (Definitely) 
# Effect is dynamic and definive --> Next start will be as Client mode
# Run : bash sap2cl.sh

echo "========================================" echo " Switch from Hotspot (AP+DHP) to Client " echo " Next start will be : Wifi Client mode " echo "========================================" echo " " echo "Stopping hostapd, dnsmasq " sudo systemctl stop hostapd.service sudo systemctl stop dnsmasq.service

echo "Configure Client to recover dhcp" sudo cp /etc/dhcpcd-dynamic.conf /etc/dhcpcd.conf sudo systemctl daemon-reload

echo "Restart wpa_supplicant" sudo pkill wpa_supplicant sleep 2 sudo wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0 -B -Dnl80211,wext

echo "Done." exit

SWITCH : AP --> Client : Next boot will be as AP

file : sap2clnsap.sh

Effect is dynamic --> Next start will be as AP

#!/bin/bash
# This script will stop Hotspot (AP+DHCP) and will Switch your Raspberry to a WIFI Client (Temporarily) 
# Effect is dynamic --> Next start will be as AP
# Run : bash sap2cl.sh

echo "========================================" echo " Switch from Hotspot (AP+DHP) to Client " echo " Next start will be : AP mode " echo "========================================" echo " " echo "Stopping hostapd, dnsmasq " sudo systemctl stop hostapd.service sudo systemctl stop dnsmasq.service

echo "Configure Client to recover dhcp" sudo cp /etc/dhcpcd-dynamic.conf /etc/dhcpcd.conf sudo systemctl daemon-reload

echo "Restart wpa_supplicant" sudo pkill wpa_supplicant sleep 2 sudo wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0 -B -Dnl80211,wext

echo "Prepare for next start as AP" sudo systemctl unmask hostapd sudo systemctl enable hostapd sudo systemctl enable dnsmasq.service sudo cp /etc/dhcpcd-static.conf /etc/dhcpcd.conf

echo "Done." exit

SWITCH : Client --> AP : THIS IS NOT DYNAMIC, AND NEEDS A REBOOT

File : scl2ap.sh

#!/bin/bash
# This script will Switch from Client to Hotspot (AP+DHP) 
# Run : bash scl2ap.sh

echo "=========================================" echo " Switch from Client to Hotspot (AP+DHP) " echo " THIS IS NOT DYNAMIC, AND NEEDS A REBOOT " echo "=========================================" echo " " echo "Start hostapd, dnsmasq"

sudo systemctl restart dnsmasq.service sudo systemctl restart hostapd.service

echo "Configure AP wit static IP Address" sudo cp /etc/dhcpcd-static.conf /etc/dhcpcd.conf sudo systemctl daemon-reload

echo "Restart wpa_supplicant" sudo pkill wpa_supplicant sleep 2 sudo wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0 -B -Dnl80211,wext

echo "A REBOOT is needed here" sudo systemctl unmask hostapd sudo systemctl enable hostapd sudo systemctl enable dnsmasq.service

echo "Done." exit

0

I am successfully switching between Client- AP mode without rebooting my raspberry pi4. I have 3 saved configurations, one for each client and AP mode: hostapd.conf, dnsmasq.conf, dhcpcd.conf

Client to AP:

sudo systemctl stop dnsmasq.service
sudo systemctl stop hostapd.service
echo "Configure AP wit static IP Address"
sudo cp -f /etc/hostapd/hostapd.conf.ap /etc/hostapd/hostapd.conf
sudo cp -f  /etc/dnsmasq.conf.ap /etc/dnsmasq.conf
sudo cp -f /etc/dhcpcd.conf.ap /etc/dhcpcd.conf
sudo systemctl daemon-reload
sudo pkill wpa_supplicant
sleep 2
sudo systemctl start dnsmasq.service
sudo systemctl start hostapd.service

AP to Client:

echo "Configure Client to recover dhcp"
sudo cp -f /etc/dnsmasq.conf.orig /etc/dnsmasq.conf
sudo cp  -f /etc/dhcpcd.conf.orig /etc/dhcpcd.conf
sudo systemctl daemon-reload

echo "Stopping wpa_supplicant" sudo pkill wpa_supplicant sleep 2

sudo systemctl start hostapd.service sudo wpa_supplicant -c/etc/wpa_supplicant/wpa_supplicant.conf -iwlan0 -B -Dnl80211,wext sleep 5

And this is the script to setup the AP mode; run it only once to create the new configuration files. First, backup the original configuration files:

sudo apt-get install dnsmasq hostapd 
sudo cp -f /etc/dhcpcd.conf /etc/dhcpcd.conf.orig
sudo cp -f /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
sudo cp -f /etc/hostapd/hostapd.conf /etc/hostapd/hostapd.conf.orig

The first AP configuration script (change your wifi country code with whatever you want):

ssid="MySSID"
psw="MyPSW"

sudo raspi-config nonint do_wifi_country IE sudo DEBIAN_FRONTEND=noninteractive apt-get install -y netfilter-persistent iptables-persistent sudo touch /etc/dhcpcd.conf.ap sudo sed -i '$ainterface wlan0\nstatic ip_address=10.3.141.1/24\nnohook wpa_supplicant' /etc/dhcpcd.conf.ap sudo touch /etc/sysctl.d/routed-ap.conf echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/routed-ap.conf sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE sudo netfilter-persistent save sudo touch /etc/dnsmasq.conf.ap echo "interface=wlan0" | sudo tee /etc/dnsmasq.conf.ap sudo sed -i '$adhcp-range=10.3.141.5,10.3.141.100,255.255.255.0,24\ndomain=ap\naddress=/rpi.ap/10.3.141.1' /etc/dnsmasq.conf.ap sudo touch /etc/hostapd/hostapd.conf.ap echo "country_code=IE" | sudo tee /etc/hostapd/hostapd.conf.ap sudo sed -i -e '$a' -e "interface=wlan0\nssid=${ssid}\nhw_mode=g\nchannel=2\nmacaddr_acl=0\nauth_algs=1\nignore_broadcast_ssid=0\nwpa=2\nwpa_passphrase=${psw}\nwpa_key_mgmt=WPA-PSK\nwpa_pairwise=TKIP\nrsn_pairwise=CCMP" /etc/hostapd/hostapd.conf.ap