Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"wifi-password ERROR: Could not retrieve current SSID. Are you connected?" #34

Open
BitGrub opened this issue Mar 16, 2024 · 14 comments
Open

Comments

@BitGrub
Copy link

BitGrub commented Mar 16, 2024

Getting the error message "ERROR: Could not retrieve current SSID. Are you connected?" if I try to use wifi-password

Manually specifying the SSID works, it just can't detect my current Wifi connection. Help?

I'm on MacOS Sonoma 14.4, M1 macbook pro

@DmytroLitvinov
Copy link

Same situation

@BitGrub
Copy link
Author

BitGrub commented Mar 23, 2024

The app doesn't work since Ventura, I think? Something to do with root privileges

@haakonstorm
Copy link

This script relies on the airport cli utility, which is deprecated by Apple now.
("/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport")

Here is a modified bare bones script that gets the current SSID's password via networksetup instead:

#!/usr/bin/env zsh

# Original script by rauchg/wifi-password.
# Ventura/Sonoma deprecated the macos "airport CLI".
# Patched by haakonstorm to use networksetup instead

echo Enter admin pwd when prompted by macos dialog.
ssid=`networksetup -getairportnetwork en0 | awk -F": " '{print $2}'`
echo SSID: "$ssid"
pwd="`security find-generic-password -ga \"$ssid\" 2>&1 >/dev/null`"
pwd=$(echo "$pwd" | sed -e "s/^.*\"\(.*\)\".*$/\1/")
echo password:
echo "\033[96m$pwd\033[39m"

@luckman212
Copy link

That's a good start @haakonstorm, but the WiFi interface won't always be en0. Also, using AWK to split on : could fail if the SSID itself contains colons.

Here's a different approach that I've been using:

#!/usr/bin/env bash

if [[ -n $1 ]]; then
  SSID=$1
else
  WIFI_IF=$(scutil <<< "list" | grep -m1 'AirPort' | awk -F/ '{print $(NF-1)}')
  SSID=$(networksetup -getairportnetwork "${WIFI_IF}" | sed -En 's/Current Wi-Fi Network: (.*)$/\1/p')
  [[ -n $SSID ]] || { echo 1>&2 "error retrieving current SSID. are you connected?"; exit 1; }
fi
echo -e "\033[90m … getting password for \"${SSID}\". \033[39m"
echo -e "\033[90m … keychain prompt incoming. \033[39m"
SECOUT=$(security find-generic-password -ga "${SSID}" 2>&1 >/dev/null)
(( $? == 128 )) && { echo "user cancelled"; exit 0; }
PASS=$(sed -En 's/^password: "(.*)"$/\1/p' <<<"$SECOUT")
[[ -n $PASS ]] || { echo 1>&2 "password for \"${SSID}\" not found in Keychain"; exit 1; }
echo -e "\033[96m ✓ ${PASS} \033[39m"

@pablofullana
Copy link

Any updates or ETA for a fix on this?

@BitGrub
Copy link
Author

BitGrub commented Apr 25, 2024

Any updates or ETA for a fix on this?

Nope

@insasquatchcountry
Copy link

Any updates or ETA for a fix on this?

I just took the script above and put where I keep my executable scripts and gave it permission to run. I could tell you how to do all that, but its best that you learn for yourself :) I named mine "wifi"

@Luminus
Copy link

Luminus commented Jan 17, 2025

I tweaked @luckman212's script a small bit so I don't always have to supply the SSID of the WiFi connection I'm currently on to get it to display the password.

Bear in mind that this doesn't account for cases where your WiFi interface is NOT en0

$WIFI_IF was returing ap1 for me, which meant $SSID was choking, hence this change.

#!/usr/bin/env bash

if [[ -n $1 ]]; then
   SSID=$1
else
   # WIFI_IF=$(iscutil <<< "list" | grep -m1 'AirPort' | awk -F/ '{print $(NF-1)}')
   SSID=$(ipconfig getsummary en0 | awk -F ' SSID : '  '/ SSID : / {print $2}')
   [[ -n $SSID ]] || { echo 1>&2 "error retrieving current SSID. are you connected?"; exit 1; }
fi
echo -e "\033[90m … getting password for \"${SSID}\". \033[39m"
echo -e "\033[90m … keychain prompt incoming. \033[39m"
SECOUT=$(security find-generic-password -ga "${SSID}" 2>&1 >/dev/null)
(( $? == 128 )) && { echo "user cancelled"; exit 0; }
PASS=$(sed -En 's/^password: "(.*)"$/\1/p' <<<"$SECOUT")
[[ -n $PASS ]] || { echo 1>&2 "password for \"${SSID}\" not found in Keychain"; exit 1; }
echo -e "\033[96m ✓ ${PASS} \033[39m"

@insasquatchcountry
Copy link

I tweaked @luckman212's script a small bit so I don't always have to supply the SSID of the WiFi connection I'm currently on to get it to display the password.

Bear in mind that this doesn't account for cases where your WiFi interface is NOT en0

$WIFI_IF was returing ap1 for me, which meant $SSID was choking, hence this change.

This worked for me on macOS 15.2 👍

@muhammed0802
Copy link

muhammed0802 commented Jan 17, 2025 via email

@luckman212
Copy link

If you want a generic method to detect the WiFi interface, that doesn't hardcode en0, en1 etc, here are two functions I wrote that are working for me still as of 15.2

_get_wifi_ifname() {
	scutil <<< list | awk -F/ '/Setup:.*AirPort$/ {print $(NF-1)}'
}
export -f _get_wifi_ifname

_get_wifi_ssid() {
	local IFNAME
	IFNAME=$(_get_wifi_ifname)
	[[ -n $IFNAME ]] || return 1
	ipconfig getsummary "$IFNAME" |
	sed -En 's/^[[:space:]]*SSID : (.*)$/\1/p'
}
export -f _get_wifi_ssid

@Luminus
Copy link

Luminus commented Jan 18, 2025

@luckman212 thanks for sharing these.

Unfortunately, _get_wifi_ifname doesn't like me very much and is retuning an empty string. I'm also on 15.2.

When I run this:

WIFI_IF=$(scutil <<< list | awk '/AirPort$/')

I get the following output, which explains why I was getting ap1 from the previous iteration since it was picking the first result

subKey [94] = State:/Network/Interface/ap1/AirPort
subKey [97] = State:/Network/Interface/awdl0/AirPort
subKey [105] = State:/Network/Interface/en0/AirPort

Are you aware of a surefire way to pick up the currently active interface?

I'm happy to leave things as they are with en0 hardcoded if this is going to be too much hassle.

Thanks loads.

@luckman212
Copy link

luckman212 commented Jan 18, 2025

@Luminus

_get_wifi_ifname doesn't like me very much and is retuning an empty string.

Try this one, it may work for you:

_get_wifi_ifname() {
    scutil <<< list | awk -F/ '/en[0-9]+\/AirPort$/ {print $(NF-1);exit}'
}
export -f _get_wifi_ifname

@Luminus
Copy link

Luminus commented Jan 18, 2025

Thanks loads @luckman212 , that worked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants