From 7ce6154e51f16b734cc76fe9f38da1b3ddb7e038 Mon Sep 17 00:00:00 2001 From: Craig Mulligan Date: Tue, 11 Feb 2020 15:23:21 +0000 Subject: [PATCH] add linux support --- README.md | 10 +++--- wifi-qr.sh | 104 +++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 78 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index aa20512..bad823f 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ wifi-qr attempts to simplify the process of sharing passwords with mobiles by ge wifi-qr depends on [qrencode](https://linux.die.net/man/1/qrencode) -Currently this script only supports macOS. +Currently this script only supports macOS and linux (with Network Manager). Inspired by [wifi-password](https://github.com/rauchg/wifi-password) @@ -16,7 +16,7 @@ Inspired by [wifi-password](https://github.com/rauchg/wifi-password) With [Homebrew](https://github.com/Homebrew/homebrew): -``` shell +```shell $ brew install qrencode ``` @@ -24,7 +24,7 @@ $ brew install qrencode With `curl`: -``` shell +```shell $ curl -L https://raw.github.com/aranw/wifi-qr/master/wifi-qr.sh -o ~/bin/wifi-qr && chmod +x ~/bin/wifi-qr ``` @@ -35,13 +35,13 @@ similar. To get the password for the WiFi you're currently logged onto: -``` shell +```shell $ wifi-qr ``` To get it for a specific SSID: -``` shell +```shell $ wifi-qr ``` diff --git a/wifi-qr.sh b/wifi-qr.sh index 04b666d..d37f181 100755 --- a/wifi-qr.sh +++ b/wifi-qr.sh @@ -1,5 +1,4 @@ -#!/usr/bin/env sh - +#!/usr/bin/env bash version="0.1.0" while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do @@ -13,44 +12,87 @@ while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do done if [[ "$1" == "--" ]]; then shift; fi -# locate airport(1) -airport="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport" -if [ ! -f $airport ]; then - echo "ERROR: Can't find \`airport\` CLI program at \"$airport\"." - exit 1 -fi - # merge args for SSIDs with spaces args="$@" -# check for user-provided ssid -if [ "" != "$args" ]; then - ssid="$@" -else - # get current ssid - ssid="`$airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'`" - if [ "$ssid" = "" ]; then - echo "ERROR: Could not retrieve current SSID. Are you connected?" >&2 +function linux { + # check for user-provided ssid + if [ "" != "$args" ]; then + ssid="$args" + exists="`nmcli -f NAME connection | egrep "${ssid}"`" + + if [[ $exists == "" ]]; then + echo "ERROR: Could not find SSID \"$ssid\"" >&2 + exit 1 + fi + else + # get current ssid + ssid="`nmcli -t -f active,ssid dev wifi | egrep '^yes' | cut -d\: -f2`" + if [ "$ssid" = "" ]; then + echo "ERROR: Could not retrieve current SSID. Are you connected?" >&2 + exit 1 + fi + fi + + pwd="`sudo grep psk= /etc/NetworkManager/system-connections/${ssid}* | cut -d "=" -f2`" + + if [ "" == "$pwd" ]; then + echo "ERROR: Could not get password. Did you enter your credentials?" >&2 exit 1 fi -fi -sleep 2 + echo "WIFI:T:WPA;S:${ssid};P:${pwd};;" | qrencode -t UTF8 +} -# source: http://blog.macromates.com/2006/keychain-access-from-shell/ -pwd="`security find-generic-password -ga \"$ssid\" 2>&1 >/dev/null`" -if [[ $pwd =~ "could" ]]; then - echo "ERROR: Could not find SSID \"$ssid\"" >&2 - exit 1 -fi +function mac { + # locate airport(1) + airport="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport" + if [ ! -f $airport ]; then + echo "ERROR: Can't find \`airport\` CLI program at \"$airport\"." + exit 1 + fi + + # check for user-provided ssid + if [ "" != "$args" ]; then + ssid="$@" + else + # get current ssid + ssid="`$airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'`" + if [ "$ssid" = "" ]; then + echo "ERROR: Could not retrieve current SSID. Are you connected?" >&2 + exit 1 + fi + fi + + sleep 2 + # source: http://blog.macromates.com/2006/keychain-access-from-shell/ + pwd="`security find-generic-password -ga \"$ssid\" 2>&1 >/dev/null`" + + if [[ $pwd =~ "could" ]]; then + echo "ERROR: Could not find SSID \"$ssid\"" >&2 + exit 1 + fi + + # clean up password + pwd=$(echo "$pwd" | sed -e "s/^.*\"\(.*\)\".*$/\1/") + + if [ "" == "$pwd" ]; then + echo "ERROR: Could not get password. Did you enter your Keychain credentials?" >&2 + exit 1 + fi + + echo "WIFI:T:WPA;S:${ssid};P:${pwd};;" | qrencode -t UTF8 +} -# clean up password -pwd=$(echo "$pwd" | sed -e "s/^.*\"\(.*\)\".*$/\1/") -if [ "" == "$pwd" ]; then - echo "ERROR: Could not get password. Did you enter your Keychain credentials?" >&2 - exit 1 +if [[ "$OSTYPE" == "linux-gnu" ]]; then + linux + exit 0 +elif [[ "$OSTYPE" == "darwin" ]]; then + mac + exit 0 fi -echo "WIFI:T:WPA;S:${ssid};P:${pwd};;" | qrencode -t UTF8 \ No newline at end of file +echo "ERROR: Unsupported OS" >&2 +exit 1