2

I use a RPI 1 as wifi repeater with two wireless dongles. I configured it using hostapd and dnsmasq.

I want the pi only to be connected to the internet if there is somebody connected to my access point. So I wrote this service:

[Unit]
Description=Start hostapd_cli
After=hostapd.service
BindsTo=hostapd.service

[Service]
Type=forking
ExecStart=/usr/sbin/hostapd_cli -p /var/run/hostapd -a /home/pi/autoConnect.sh -i wlan0 
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no

[Install]
WantedBy=hostapd.service

Where autoConnect.sh is:

if [[ "$2" == "AP-STA-CONNECTED" ]]; then
    if ! ip link show wlan1 | grep -q "UP"; then
        sudo ip link set wlan1 up
    fi

elif [[ "$2" == "AP-STA-DISCONNECTED" ]]; then
    sleep 120 &&
    if ! sudo hostapd_cli all_sta | grep -q "AUTHORIZED"; then
        sudo ip link set wlan1 down
    fi
fi

It work's, but after a reboot the pi connects to the internet, which I want to avoid. Adding

wlan0
noup

to /etc/dhcpcd.conf didn't do the trick. (Starting wpa_supplicant through my script is not possible, because it fails when I have to restart hostapd.)

So I would like to know how to set an interface down by default?

(Maybe you got even hints for a better solution in general.)

EDIT:

I followed Ingo's suggestions and changed completely to systemd and disabled the wpa_supplicant@wlan1.service. I now use this service and script to automatically connect and disconnect to the internet:

# /etc/systemd/system/wpa-cli@wlan0.service
[Unit]
Description=wpa_cli to auto connect wifi on event
After=network-online.target wpa_supplicant@%i.service sys-subsystem-net-devices-%i.device
BindsTo=wpa_supplicant@%i.service

[Service]
Type=forking
ExecStart=/sbin/wpa_cli -i %I -a /home/pi/autoConnectWLAN.sh -B
Restart=on-failure
RestartSec=1

[Install]
WantedBy=multi-user.target

The script:

#!/bin/bash

case "$2" in
    AP-STA-CONNECTED)
        systemctl start wpa_supplicant@wlan1.service 
        ;;

    AP-STA-DISCONNECTED)
        sleep 60
        if ! wpa_cli -i wlan0 all_sta | grep -q "AUTHORIZED"; then
            systemctl stop wpa_supplicant@wlan1.service

        fi
        ;;

I now wonder if it would be possible to achieve what I want by just defining dependencies between the two wpa_supplicant@.services.

jake
  • 1,347
  • 10
  • 23

1 Answers1

2

You also asked: "Maybe you got even hints for a better solution in general." I prefer to use systemd-networkd because it has all in one. Using two USB/wifi dongle simplifies it a lot. So my hint is to look at Access point as WiFi repeater with additional WiFi-dongle. It does not fit exactly your needs but it should not be a big problem to adapt it to your problem.

Both interfaces wlan0 and wlan1 are handled independent from the other with interface specific versions of wpa_supplicant services (e.g. wpa_supplicant@wlan0.service). You can setup each interface for what you need and set dependencies between the two services.

And to answer your question: with this setup it is easy to disable e.g. wlan0 on startup with:

rpi ~$ sudo systemctl disable wpa_supplicant@wlan0.service

(If you like to get some assistance please don't hesitate to ask).

Ingo
  • 42,107
  • 20
  • 85
  • 197
  • Thanks for the answer. I'd like to change completely to systemd. Unfortunately I got stuck here, where you guessed it to be a problem with the old hardware. – jake Feb 18 '19 at 19:51
  • I finally made it working. How would achieve what I want with systemd? Is there a possibility to do it without using wpa_cli -a SCRIPT.sh? – jake Mar 06 '19 at 22:33
  • @jake I have just found that it could be that there was a wrong option set for wpa_supplicant used by systemd-networkd: Replacing hostapd with wpa_supplicant but I do not have a feedback now if this fixed the problem, waiting for it. – Ingo Mar 07 '19 at 00:37
  • Is it possible to get what I want without an additional service? Otherwise I'll accept your answer. – jake Mar 24 '19 at 01:25
  • @jake What do you mean with "additional service"? Where to add a service? – Ingo Mar 24 '19 at 10:11
  • I mean my wpa-cli@wlan0.service. It would be a cleaner solution if I could define a condition inside the wpa_supplicant@wlan1service to only be running if somebody is connected to the access point. – jake Mar 24 '19 at 22:32
  • 1
    @jake I will look but have just no idea. The problem is to get information from a running service (the access point). You cannot use dependencies because they are only used to start/stop/delay etc. services depending on other services. With a running service systemd is satisfied. Running wpa_cli as daemon for this is a good idea. But if we can use systemd-notify in any way I would prefer this. – Ingo Mar 25 '19 at 10:53
  • 1
    @jake I have made ifplugd available again for Raspbian: Make ifplugd available again since Raspbian Stretch. But it doesn't make much difference to use wpa_cli -a SCRIPT.sh. So only a comment to this. – Ingo May 27 '19 at 09:35