Detect SSID Of NetworkInterface?

Is there a way to determine what wireless network my host OS is using (and, ideally, which networks are in range and broadcasting their names)? This is known as the SSID. The NetworkInterface object is pretty sparse with what it exposes, and is likely not the right place to expose this anyway.

Is there some other way to find out? I need a Windows and Mac solution, plugins are fine.

Thanks!

– Kimball

I don’t know about Windows, and I’m sure there is an MBS plugin for this.

On the Mac, you can use System Profiler, and I included a class for this within the MacOSLib project. This code will work, and I’m going to include an SSID property for future versions.

    dim sp as new MacSystemProfiler( "AirPort" )
    dim profiles() as MacPListBrowser = sp.Profile.FindByKey( "spairport_current_network_information" )
    for each thisProfile as MacPListBrowser in profiles
      if thisProfile.HasKey( "_name" ) then
        r = thisProfile.Child( "_name" ).VariantValue
        exit
      end if
    next
    return r

Hi, Kem - thanks for the quick response. This works perfectly on the Mac. For anyone playing along at home, be sure you include the “Additional Modules” folder from MacOSLib, or else you won’t get the MacSystemProfiler class.

Thanks!

Also, for Windows, it appears I’m out of luck. The Windows Functionality Suite does not seem to have anything in it that will reveal the currently connected SSID, nor do the MBS plugins. The best I could find is a pretty old post back on the old RealStudio forums that leverages a VB Script and some WMI magic. Ick.

Anyone know how to do this on windows?

Thanks!

Either WMI, VB script, a plugin (MBS?) or you could use declares.

I’m looking at a possible solution that involves the windows shell - “NETSH WLAN SHOW INTERFACE” seems promising…

For the record, here’s how I’m doing this cross platform - Windows and Mac. Tested on Mac OS X 10.8.x, as well as Windows 7 pro and home. Will test Win 8 tomorrow, and follow up if there are problems:

This is not particularly clean / optimized code, but it seems to work solidly…

//Figure out what SSID we are connected to. dim currSSID as String = "" #if targetMacOS dim r as Variant dim sp as new MacSystemProfiler( "AirPort" ) dim profiles() as MacPListBrowser = sp.Profile.FindByKey( "spairport_current_network_information" ) for each thisProfile as MacPListBrowser in profiles if thisProfile.HasKey( "_name" ) then r = thisProfile.Child( "_name" ).VariantValue exit end if next currSSID = r.StringValue #elseif TargetWin32 dim cmd as String = "NETSH WLAN SHOW INTERFACE" dim sh as new Shell sh.mode = 0 sh.execute(cmd) dim result as String = sh.result if instr(result, ": connected") > 0 then //There is a current wireless link. dim reg as new RegEx dim match as RegExMatch reg.searchPattern = "^\\ *SSID.*: (.*)" match = reg.Search(result) if match <> nil then //we found it! if match.SubExpressionCount = 2 then currSSID = match.SubExpressionString(1) end if end if end if #endif

FYI, version 156 of MacOSLib (to be up soon, I hope) will have a shared property for CurrentSSID, so you can reduce your code to:

#if TargetMacOS
  currSSID = MacSystemProfiler.CurrentSSID
#else
  …

The master project is here:

http://github.com/macoslib/macoslib

My branch is here if you want to keep up with my changes directly (note, not all of my changes make it into the master branch):

http://github.com/ktekinay/macoslib

Also, I’ll add your code to the WFS project.

Sweet. Thanks, Kem.

I was working on this to work on all three platforms, and have come up with the following code. It has been tested and works on Windows 7 Home, OS X (10.9), and ubuntu 12.04 LTS. I include the code for the main call and two of my little utility string functions that are needed. It would probably be better with RegEx, but that’s not my strength.

(As an added note, the Mac version runs more slowly as the terminal call Current Network Information: brings up information about every Wi-Fi router in range.)

[code]Function SSID() As String
//Figure out what SSID we are connected to.
// We are looking by the Hardware specific Console or Terminal call.
// 20131208 gfmc

Dim SSID, sBefore, sAfter, sHold, cmd As String
Dim sh As Shell
#if targetMacOS
sBefore = “Current Network Information:”
sAfter = “:”
cmd = “system_profiler SPAirPortDataType”
#elseif TargetWin32
sBefore = "SSID : "
sAfter = EndOfLine
cmd = “NETSH WLAN SHOW INTERFACE”
#elseif TargetLinux Then
sBefore = “ESSID:”
sAfter = EndOfLine
cmd = “iwconfig wlan0”
#endif

sh= New Shell
sh.mode = 0
sh.execute(cmd)
sHold = sh.result

SSID = Trim(GetMiddleText(sHold,sBefore, sAfter))

// get rid of any quotes
SSID = SSID.ReplaceAll(Chr(34), “”)

Return SSID
End Function
[/code]

[code]Function GetMiddleText(Source As String, TxtOpen As String, TxtClose As String, WhichOne As Integer = 1, IncludeMarkText As Boolean = False) As String
//This will look for information in between TxtOpenand TxtClose
// It will return that text for the WhichOneth Time
// and if it says IncludeMarkText, then we’ll send the text back with it
// 20121011, gfmc

Dim posOpen As Integer = -1
Dim posClose As Integer, num As Integer
Dim ct As Integer = Count(Source, TxtOpen)
if ct < WhichOne Then Return “”

While num <> WhichOne
posOpen = posOpen + 1
posOpen = inStr(posOpen, Source, TxtOpen)
num = num + 1
Wend

posClose = inStr((posOpen + TxtOpen.len), Source, TxtClose)
if posClose = 0 Then Return “”

’ we should be at the right open pisition
posOpen = posOpen + TxtOpen.len
Dim RetStr As String = Source.Mid(posOpen, posClose - posOpen)
if IncludeMarkText Then
RetStr = TxtOpen + RetStr + TxtClose
End If

Return RetStr

End Function
[/code]

[code]Function Count(Source As String, toFind As String) As Integer
Dim ct As Integer = 0
Dim pos As Integer = 0

pos = inStr(pos, Source, toFind)
While pos <> 0
ct = ct + 1
pos = pos + 1
pos = inStr(pos, Source, toFind)
Wend

Return ct

End Function
[/code]

Hope this helps.
Gary