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
  • If you search this site you will find a few posts with similar objectives. I even posted one which didn't require restart - which kind of worked. Whether any of them work is doubtful. Your idea is unlikely to work because it fails to understand how dhcpcd works. I believe it may be possible with systemd.networkd but this requires totally different setup.
    – Milliways
    Mar 9, 2022 at 10:30
  • So I have certainly to start from the beginning, and not necessarily use RASPAP, but hostapd configuration that makes it easier to switch from client mode to Routed Wireless Access Point mode.. Mar 9, 2022 at 11:05
  • You can try to remove -B and add -d in wpa_supplicant command line to see debug lines. Also, wpa_supplicant not assign an IP address you must use dhclient. dhclient --help and in /etc/dhcp/dhclient.conf you can find config example. 1) start wpa_supplicant with & at the end or add -B, 2) start dhclient. NOTE: also you can create one second wireless interface ` iw dev wlan0 interface add ap0 type __ap` , one for the client (for wpa_supplicant, wlan0), one for the AP (for hostapd, ap0)
    – Ephemeral
    Mar 9, 2022 at 13:59

2 Answers 2

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!) :)

1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Chenmunka
    Oct 15, 2023 at 20:24
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.