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@.service
s.
systemd
. Unfortunately I got stuck here, where you guessed it to be a problem with the old hardware. – jake Feb 18 '19 at 19:51systemd
? Is there a possibility to do it without usingwpa_cli -a SCRIPT.sh
? – jake Mar 06 '19 at 22:33systemd-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:37wpa-cli@wlan0.service
. It would be a cleaner solution if I could define a condition inside thewpa_supplicant@wlan1service
to only be running if somebody is connected to the access point. – jake Mar 24 '19 at 22:32wpa_cli -a SCRIPT.sh
. So only a comment to this. – Ingo May 27 '19 at 09:35