You are not logged in.
Hi everyone, I'm trying to automate my home wireless connection via wpa_supplicant.
This is the current situation:
#!/bin/bash
[ $USER != "root" ] && { echo "You must be root to run this script."; exit 1; }
[ ?# -ne 3 ] && { echo "Usage: $0 SSID password wireless_interface_id "; exit 1; }
[ ip link | grep $3 ] || { echo "$3 is not a valid network interface"; exit 1; }
SSID=$1
Pwd=$2
if=$3
[ ip link show up | grep $if ] || ip link set $if up &> /dev/null
wpa_passphrase $SSID $Pwd | tee /etc/wpa_supplicant/wpa_supplicant.conf &> /dev/null
[ ps -e | grep "wpa_supplicant" ] && killall wpa_supplicant
wpa_supplicant -B -i $if -c /etc/wpa_supplicant/wpa_supplicant.conf
exit 0I will always get:
Successfully initialized wpa_supplicantBut, when I check the wireless connection
iw dev <wireless_network-interface> link this is, sometimes - not always... - the answer:
Not connectedAny idea? Thanks in advance,
VDA
P.S. I'm sorry for my english. I'm still learning.
Last edited by vda (2019-06-15 16:17:29)
I'm sorry for my english: I'm still learning...
Offline
Offline
Definitely see the above - this script doesn't really do anything worthwhile except run wpa_passphrase to overwrite the wpa_supplicant.conf. And do you really want to overwrite it every time you connect to a new network? If you do, you'll need to do it all again when you go back to a network you've used before. Instead, append networks to your wpa_supplicant.conf so it can connect automatically to known networks on subsequent uses.
But some comments strictly on the scripting below:
#!/bin/bash
[ $USER != "root" ] && { echo "You must be root to run this script."; exit 1; }
# You specify bash as the shebang, so you might as well use the better [[ ]] bash test rather than [ ].
# You coul also simplify this by using `sudo -v` at the start then using sudo before commands that need root access in the script. The result will be to prompt for a password if needed, but not otherwise.
[ ip link | grep $3 ] || { echo "$3 is not a valid network interface"; exit 1; }
# No need for the explicit [ ] test:
ip link | grep -q $3 || { echo "..."; exit 1; }
# ...
[ ip link show up | grep $if ] || ip link set $if up &> /dev/null
# same as above, but really this command is not needed at all, wpa_supplicant handles this
wpa_passphrase $SSID $Pwd | tee /etc/wpa_supplicant/wpa_supplicant.conf &> /dev/null
# Why tee and redirect stdout? Just write to the file in the first place:
wpa_passphrase "$SSID" "$Pwd" > /etc/wpa_supplicant/wpa_supplicant.conf
# Also note the quotes, they are necessary here and their absence may be why your script fails sometimes.
[ ps -e | grep "wpa_supplicant" ] && killall wpa_supplicant
# No need for the test, just do it
killall wpa_supplicant
# ...
exit 0
# ah, that's the default if we've made it this far, so this serves no purpose at all"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Below is my configuration for wpa_suppliant, not sure it is the best way but it works well for me. It is much faster than when I used NetworkManager and Wicd, altought sometimes it help to have wpa-cute in order to add new networks quickly.
/etc/systemd/network/wireless.network
[Match]
Name=wl*
[Network]
DHCP=yes
RouteMetric=20
IPv6PrivacyExtensions=true/etc/wpa_supplicant/wpa_supplicant-wlp1s0.conf
ctrl_interface=/var/run/wpa_supplicant
ctrl_interface_group=wheel
update_config=1
network={
ssid="dlink"
psk="PASSWORD"
}
network={
ssid="UNIVERSITY-securise"
key_mgmt=WPA-EAP
identity="USER"
password="PASSWORD"
priority=1
}
network={
ssid="eduroam"
key_mgmt=WPA-EAP
identity="USER@UNIVERSITY.ca"
password="PASSWORD"
}
network={
ssid="Open network example"
bssid=xx:xx:xx:xx:xx:xx
key_mgmt=NONE
auth_alg=OPEN
}Finally
rm /etc/resolv.conf
systemctl enable systemd-networkd
systemctl enable wpa_supplicant@wlp1s0
systemctl enable systemd-resolved
systemctl start systemd-networkd
systemctl start wpa_supplicant@wlp1s0
systemctl start systemd-resolved
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.confOffline
Finally, here is my solution (obviously disable all network services different from dhcpcd).
$ cat /opt/IPv4Mng/wireless_connect
#!/bin/bash
#
# Description: it connects the specified wireless interface to the network,
# according to the configuration described in /etc/wpa_supplicant.conf
#
# Usage: wireless_connect
#
# Ex. ./wireless_connect
#
# Author: VDA
#
# Last update: 2019/06/07
#
#!/bin/bash
[ $EUID -ne 0 ] && { echo "You must be root to run this script."; exit 1; }
### SETTINGS ###
#
# set here the wireless_interface_id (wif), the wait_time (wtime i.e.
# the time the system have to wait after an hardware status change), and
# the maximum number of attempts (max_attempts) the system can try to
# connect to the network
#
wif="wlp2s0"
wtime=1
max_attempts=50
#
# END SETTINGS #
ip link show up | grep "$wif" > /dev/null || ip link set $wif up
ip link show up | grep "$wif" > /dev/null || { echo "Not connected: unable to activate wireless network interface $wif"; exit 1; }
k=1
while [ $(iw dev wlp2s0 link | grep "Not connected" > /dev/null; echo $?) -eq 0 ]
do
ps -e | grep wpa_supplicant > /dev/null && killall wpa_supplicant
wpa_supplicant -B -i $wif -c /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null && sleep $wtime
[ $k -lt $max_attempts ] && ((k=$k+1)) || { echo "Unable to connect to wireless network"; exit 1; }
done
# dhcpcd $wif
iw dev wlp2s0 link
exit 0
----------------------------------------------------------------------------------------
$ cat /etc/wpa_supplicant/wpa_supplicant.conf
network={
ssid="TP-LINK_E4FA54"
psk=b842135592be46f336c5a10bc7e63ca11e91a091667f2effbd05f707a1235cbc
}
network={
ssid="Nokia 5.1"
key_mgmt=NONE
}
network={
key_mgmt=NONE
priority=-999
}
----------------------------------------------------------------------------------------
$ cat /etc/systemd/system/VDA_Network.service
[Unit]
Description=VDA_Network
[Service]
Type=forking
ExecStart=/opt/IPv4Mng/wireless_connect
[Install]
WantedBy=multi-user.target
----------------------------------------------------------------------------------------
$ sudo systemctl enable VDA_Network.service
$ sudo systemctl start VDA_Network.serviceAnd it works very well.
Thank you all.
VDA
Last edited by vda (2019-06-17 13:45:32)
I'm sorry for my english: I'm still learning...
Offline