scre.git
rs.sh
#!/bin/sh
# rs - connect and resolve network
# Copyright (C) 2022-2023 ArcNyxx <me@arcnyxx.net>
# see LICENCE file for licensing information
# replace with desired station from `iwctl device list`
iwctl station wlan0 scan >/dev/null \
|| { echo 'rs: unable to access wlan0' >&2 ; exit ; }
echo 'rs: waiting to scan' >&2 ; sleep 1
NETW=`iwctl station wlan0 get-networks \
|| { echo 'rs: no networks available' >&2 ; exit 1 ; }`
NETW=`echo "$NETW" | sed '1,4d;s/\x1b\[.\{1,5\}m//g;s/.\{6\}//;s/[ *]*$//' \
| awk '{ printf "rs: network found (with " $NF ; --NF ;
print " security): " $0 }' | tee /dev/stderr`
echo -n 'rs: enter network name: ' ; read -r NAME
NETW=`echo "$NETW" | sed -n "/): $NAME/{p;q}"`
[ "$NETW" ] || { echo 'rs: invalid network name' >&2 ; exit 1 ; }
SECR=`echo "$NETW" | cut -d\ -f5`
[ "$SECR" = open -o -f "/var/lib/iwd/`echo $NETW | cut -d\ -f7-`.$SECR" ] \
|| { echo -n 'rs: enter passphrase: ' >&2 ; read -r PASS ; }
iwctl ${PASS:+--passphrase=$PASS} station wlan0 connect "`echo $NETW \
| cut -d\ -f7-`" || { echo 'rs: unable to connect' >&2 ; exit 1 ; }
while [ -z "`ip route`" ] ; do sleep 1 ; done
echo "nameserver `ip route | sed q | cut -d\ -f3`" >/etc/resolv.conf
echo 'rs: connected to network' >&2