Skip to content

Instantly share code, notes, and snippets.

@okeuday
Created February 23, 2017 02:00
Show Gist options
  • Save okeuday/8a3b5de256119a7de36490e64f2c35db to your computer and use it in GitHub Desktop.
Save okeuday/8a3b5de256119a7de36490e64f2c35db to your computer and use it in GitHub Desktop.
Linux /etc/network/interfaces script for wifi
#!/bin/sh
if [ $# -ne 1 ]; then
echo "usage: $0 wlan_interface"
exit 1
fi
DIRECTORY=`dirname $0`
IFACE=$1
ifconfig $IFACE hw ether `$DIRECTORY/mac_address_reset`
ip link set $IFACE up
sleep 1
SCAN=$( \
iwlist $IFACE scan 2>&1 | grep -v "^$IFACE" | grep -v "^$" | \
grep 'Address:\|Quality:\|ESSID:\|Mode:\|Encryption key:\|Channel /' | \
sed -e "s/^\ *//" \
-e "s/^Cell [0-9]\+ - /|/" \
-e "s/^|Address: /|AP=/" \
-e "s/^Quality:\([0-9]\+\)\/.*$/QUALITY=\1/" \
-e "s/^.*Channel \([0-9]\+\).*$/CHANNEL=\1/" \
-e "s/^ESSID:/ESSID=/" \
-e "s/^Mode:/MODE=/" \
-e "s/^Encryption key:/ENC=/" \
-e "s/^[^|].*:.*//" \
-e "s/[ \t#]/~/g" | \
tr "\n|" "|\n" \
)
ip link set $IFACE down
#echo $SCAN
#exit 0
which=""
while read glob scheme; do
AP=""; QUALITY=""; CHANNEL=""; ESSID=""; MODE=""; ENC=""
for i in $SCAN ; do
for ITEM in $(echo $i | sed -e "s/|/ /g" ) ; do
# for ESSIDs with spaces
j=$(echo $ITEM | sed -e "s/~/ /g" )
case $j in
AP=*)
AP=${j#AP=}
;;
QUALITY=*)
QUALITY=${j#QUALITY=}
;;
CHANNEL=*)
CHANNEL=${j#CHANNEL=}
;;
ESSID=*)
ESSID=$(echo ${j#ESSID=} | sed -e "s/ /_/g" )
;;
MODE=*)
MODE=${j#MODE=}
;;
ENC=*)
ENC=${j#ENC=}
;;
esac
done
case "$AP,$ESSID,$ENC,$MODE,$CHANNEL" in
$glob)
echo $scheme
exit 0
;;
esac
done
done
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment