PDA

View Full Version : [Solution] Wifi-autoconnect on boot without NetworkManager



sea
10th June 2015, 03:24 AM
Heyas

From time to time i'm playing around with minimal installations, trying to remove some services.
Having a laptop, gives one certain issue at each and every new (minimal) installation.

One point is especialy annoying, the wifi spots of friends and other spots get lost, unless you did a backup of: /etc/sysconfig/network-scripts/

Now i found the best solution (to me anyway):
After you installed your minimal system, make sure the wpa_supplicant package is installed!

Since one mounts the /home partition anyway, i have created a folder called 'wifi' in my scripts dir:

0 ~/.local/bin/wifi $ ls
install-service.sh wifi-home wifi-home.service wifi-friend wifi-friend.service


Now when i install a new system, i go to that dir (as root), and run:

sh install-service.sh
This will copy the service files to their proper location, and will start the wifi-home script.

#!/bin/sh
cp *.service /etc/systemd/system/
systemctl start wifi-home
systemctl enable wifi-home

wifi-home.service


Description=Wifi-Home

[Service]
Type=oneshot
ExecStart=/home/sea/.local/bin/wifi/wifi-home
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

And execute the script which then connects:
You do not have to use this script as service, you could also place it (start it) in your ~/.bashrc (NOT recomended, but works) or manualy (in which case you should place the scripts in the bindir) if you prefer.
[U]wifi-home

#!/bin/bash
#
# Variables
#
WIFI=wlp3s0 # Your WIFI device
SSID="APNAME" # The AP its name
PASS="password" # Password for that AP
URL="fedoraforum.org" # Which URL to ping
ME=${0##*/}
KEY="$(dirname $0)/$ME.key" # Where to save the keyfile
#
# Internals
#
start=${start:-false}
[ 0 -eq ${UID:-0} ] && \
LOG=/var/log/$ME.log || \
LOG=$HOME/.local/logs/$ME.log
#
# Functions
#
wifi_createkey () { # SSID PASS FILE
# Creates a wpa key file if none exists yet
#
[ -d "$(dirname $3)" ] || mkdir -p "$(dirname $3)"
wpa_passphrase "$1" "$2" > "$3"
}
wifi_start() {
# Start wpa supplicant in background
#
wpa_supplicant -B -i$WIFI -c"$KEY" 1>>"$LOG" 2>>"$LOG" &
dhcpcd -B 1>>"$LOG" 2>>"$LOG"
}
p() { ping -c 1 "$URL" 1>/dev/zero 2>/dev/zero ; }
yesno() {echo "$1";read -n1 buffer;[ y = "${buffer,,}" ] && return 0 || return 1 ;}
#
# Display and action
#
# Is there a key file?
[ ! -f "$KEY" ] && wifi_createkey "$SSID" "$PASS" "$KEY"

# If it cant ping, then there is no internet
if ! p 1>/dev/zero 2>/dev/zero
then start=true
# If they are already running, they are blocking the new connection
for used in dhcpcd ${0##*/}
do # Make sure those processes are killed if they are still running
if ps|grep $used | grep -v $$ >/dev/zero && yesno "$used is already running, stop it?"
then pkill $used
kill $(ps|awk -v user=$used '/used/ {print $1}')
fi
done
fi

# Now only start if we need to
if $start
then echo "Starting $ME..."
wifi_start &
echo "Started $ME with $?"
fi


Assuming the situation that you moved your laptop to a friends home, and that you already made a script (copy-paste-edit) named wifi-friend, at his home, you simply do:

su
systemctl stop wifi-home
systemctl start wifi-friend
That is all left to do, when you move your device.

Grats to a system without NetworkManager!

Hope this helps and have fun :)