Skip to content

Commit

Permalink
Merge pull request #2 from hobochild/linux-support
Browse files Browse the repository at this point in the history
Add linux support
  • Loading branch information
aranw authored Feb 11, 2020
2 parents 75360a9 + 7ce6154 commit 9099c68
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 36 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -16,15 +16,15 @@ Inspired by [wifi-password](https://github.com/rauchg/wifi-password)

With [Homebrew](https://github.com/Homebrew/homebrew):

``` shell
```shell
$ brew install qrencode
```

### Install

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
```

Expand All @@ -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 <ssid>
```

Expand Down
104 changes: 73 additions & 31 deletions wifi-qr.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env sh

#!/usr/bin/env bash
version="0.1.0"

while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do
Expand All @@ -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
echo "ERROR: Unsupported OS" >&2
exit 1

0 comments on commit 9099c68

Please sign in to comment.