Jump to content

Determine the current SSID


Recommended Posts

I apologize if this is a stupid question, as I am still fairly new to AutoIt, but I would like to run a specific program only when connected to a specific wireless network. Is there an easy/quick way to do this, or what would be the most effective way of accomplishing this? I want it to be as simple and efficienct as possible..

I am trying to do this on a Vista machine. I know it is possible on an XP system but cant seem to get it to work on Vista.

Thanks..

--- EDIT 6/20/08 ---------------------------

This is the solution that was developed:

:) Success!!

Func _GetActiveSSID()
    Local $line, $foo = Run(@ComSpec&" /c "&'netsh wlan show int', @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) 
    
    While @OSVersion = "WIN_VISTA"
        $line = StringStripWS(StdoutRead($foo, "peek=True"), 4)
        If @error Then ExitLoop
        If StringInStr($line, "SSID") Then
            $line = StringTrimLeft($line, StringInStr($line, "SSID")+6)
            Return (StringTrimRight($line, StringLen($line)-StringInStr($line, "BSSID")+2))
        ElseIf StringInStr($line, "disconnected") Then
            Return("None")
        EndIf
    Wend    ;<== While @OSVersion = "WIN_VISTA"
EndFunc     ;<== _GetActiveSSID

This works for me!! :P I am gonna try an XP version of this then add it when I am sure it works. Thanks for all the help guys!

Edited by Shalm
Link to comment
Share on other sites

I apologize if this is a stupid question, as I am still fairly new to AutoIt, but I would like to run a specific program only when connected to a specific wireless network. Is there an easy/quick way to do this, or what would be the most effective way of accomplishing this? I want it to be as simple and efficienct as possible..

Thanks..

This uses WMI and works in XP, should work in Vista(?)

MsgBox(0, "Active SSID", _GetActiveSSID())

Func _GetActiveSSID()
    Local $SSID = ""
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\wmi")
    $objMSNdis_80211_ServiceSetIdentifierSet = $objWMIService.ExecQuery("Select * from MSNdis_80211_ServiceSetIdentifier Where active=true")
    For $objMSNdis_80211_ServiceSetIdentifier In $objMSNdis_80211_ServiceSetIdentifierSet
        $ID = ""
        For $i = 0 To $objMSNdis_80211_ServiceSetIdentifier.Ndis80211SsId(0)
            $ID = $ID & Chr($objMSNdis_80211_ServiceSetIdentifier.Ndis80211SsId($i + 4))
        Next
        $SSID = $ID
    Next
    Return $SSID
EndFunc  ;==>_GetActiveSSID
Link to comment
Share on other sites

This uses WMI and works in XP, should work in Vista(?)

MsgBox(0, "Active SSID", _GetActiveSSID())

Func _GetActiveSSID()
    Local $SSID = ""
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\wmi")
    $objMSNdis_80211_ServiceSetIdentifierSet = $objWMIService.ExecQuery("Select * from MSNdis_80211_ServiceSetIdentifier Where active=true")
    For $objMSNdis_80211_ServiceSetIdentifier In $objMSNdis_80211_ServiceSetIdentifierSet
        $ID = ""
        For $i = 0 To $objMSNdis_80211_ServiceSetIdentifier.Ndis80211SsId(0)
            $ID = $ID & Chr($objMSNdis_80211_ServiceSetIdentifier.Ndis80211SsId($i + 4))
        Next
        $SSID = $ID
    Next
    Return $SSID
EndFunc ;==>_GetActiveSSID
Thank you so much for your response, but for some reason this is not returning anything. It may be a Vista thing.. :) Any other thoughts?
Link to comment
Share on other sites

Thank you so much for your response, but for some reason this is not returning anything. It may be a Vista thing.. :) Any other thoughts?

Have you got an active wireless connection when you test it?

If not, it will return an empty string.

Other than that, will have to rely on somebody else for Vista testing as I won't be near a Vista machine for about a week.

Edited by ResNullius
Link to comment
Share on other sites

Have you got an active wireless connection when you test it?

If not, it will return an empty string.

Other than that, will have to rely on somebody else for Vista testing as I won't be near a Vista machine for about a week.

Yeah, I tested the code (in Vista) while actively connected to a wireless network and it returned a blank field with no name. :)

Also, I tested the code on an XP machine and it worked like a charm :P so this has to be a Vista Problem..

Thank you for your input, it worked great on an XP machine but unfortunately I need to do this in Vista.

Anyone know how to do this on a Vista Machine?

Edited by Shalm
Link to comment
Share on other sites

Yeah, I tested the code (in Vista) while actively connected to a wireless network and it returned a blank field with no name. :)

Also, I tested the code on an XP machine and it worked like a charm :P so this has to be a Vista Problem..

Thank you for your input, it worked great on an XP machine but unfortunately I need to do this in Vista.

Anyone know how to do this on a Vista Machine?

Here's a version with a couple of error check messages thrown in.

What happens when you run this?

MsgBox(0, "Active SSID", _GetActiveSSID())

Func _GetActiveSSID()
    Local $SSID = ""
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\wmi")
    If Not IsObj($objWMIService) Then Exit (MsgBox(0, "Exiting...", "Initial WMI query failed!"))
    
    $objMSNdis_80211_ServiceSetIdentifierSet = $objWMIService.ExecQuery("Select * from MSNdis_80211_ServiceSetIdentifier Where active=true")
    If Not IsObj($objMSNdis_80211_ServiceSetIdentifierSet) Then Exit (MsgBox(0, "Exiting...", "''$objMSNdis_80211_ServiceSetIdentifierSet'' not an Object!"))
    
    For $objMSNdis_80211_ServiceSetIdentifier In $objMSNdis_80211_ServiceSetIdentifierSet
        $ID = ""
        For $i = 0 To $objMSNdis_80211_ServiceSetIdentifier.Ndis80211SsId(0)
            $ID = $ID & Chr($objMSNdis_80211_ServiceSetIdentifier.Ndis80211SsId($i + 4))
        Next
        $SSID = $ID
    Next
    Return $SSID
EndFunc  ;==>_GetActiveSSID
Link to comment
Share on other sites

I would try to have it run as Administrator somehow. I know there is AutoIt methods to overcome some of the security enhancements in Vista. Since you're getting an empty string, Vista is most likely denying the access that XP is granting you with that script.

I'm sure others have a lot more experience in the department of authenticating against Vista security in Autoit, and hopefully they can throw in some useful info. :)

Link to comment
Share on other sites

#requireadmin is what is needed to get around most script issues within vista.

The other is User Account Control. Disabling UAC will make most scripts run like they do under XP.

Kerros===============================================================How to learn scripting: Figure out enough to be dangerous, then ask for assistance.

Link to comment
Share on other sites

I turned off UAC and used #requireadmin and still get nothing back. I used the updated code as supplied for error correction but still dont get anything.

post-36765-1213655051_thumb.gif

Is this even possible to do?

Link to comment
Share on other sites

I turned off UAC and used #requireadmin and still get nothing back. I used the updated code as supplied for error correction but still dont get anything.

post-36765-1213655051_thumb.gif

Is this even possible to do?

Apparently the following command line (Vista only) is supposed to give you info that you can parse to get the SSID

netsh wlan show interfaces

As I said, I just don't have a Vista machine with wireless to test on right now.

Link to comment
Share on other sites

Apparently the following command line (Vista only) is supposed to give you info that you can parse to get the SSID

netsh wlan show interfaces

As I said, I just don't have a Vista machine with wireless to test on right now.

Awesome, thank you, this command gives up the information I am looking for :drool:

How would I go about getting this info into my program? Can I do it without opening the command prompt window? I am unsure of how to rip that stuff into my program as efficiently as possible so as not to create too much lag or flickering.. Not too familiar with "parsing" and stuff.

Link to comment
Share on other sites

You could use ControlRead() And if i'm not mistaken, StDoutRead().

Best of Luck, J.B.

Would you/anyone be able to give an example of how to use these functions? I am still learning and haven't used anything like that.. it would save me alot of time! :)

Link to comment
Share on other sites

I was just checking on that, and I can't get the Control stuff for a cmd, and StDoutRead() Returns nothing. I will still be looking for a solution!

Thank you, it is greatly appreciated!

Link to comment
Share on other sites

Has this thread bonked out? :) Just wondering if anyone can help me out with this.. I have kinda stalled for a while.

Can you redirect the output to a text file

netsh wlan show interfaces>wlan_int.txt

and then FileRead() the result and get the string with the SSID that way?

That would be a stop gap work around untill someone can figure out the StdOut thing (like me if and when I get to a Vista laptop....)

Edited by ResNullius
Link to comment
Share on other sites

Can you redirect the output to a text file

netsh wlan show interfaces>wlan_int.txt

and then FileRead() the result and get the string with the SSID that way?

That would be a stop gap work around untill someone can figure out the StdOut thing (like me if and when I get to a Vista laptop....)

I got this to work... kinda. I shows a blank box first but then shows the intended information:

#include <Constants.au3>
Local $foo = Run("netsh wlan show int", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) 
Local $line
While 1
    $line = StdoutRead($foo)
    If @error Then ExitLoop
    MsgBox(0, "STDOUT read:", $line)    
Wend

Now is where my newbeism shines.. I get a large box with multi-lines that has everthing listed included the SSID. How can I sort through this and rip out only the SSID to a variable of my choosing? Sorry if this is simplistic.

Link to comment
Share on other sites

I got this to work... kinda. I shows a blank box first but then shows the intended information:

#include <Constants.au3>
Local $foo = Run("netsh wlan show int", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) 
Local $line
While 1
    $line = StdoutRead($foo)
    If @error Then ExitLoop
    MsgBox(0, "STDOUT read:", $line)    
Wend

Now is where my newbeism shines.. I get a large box with multi-lines that has everthing listed included the SSID. How can I sort through this and rip out only the SSID to a variable of my choosing? Sorry if this is simplistic.

Can you post the contents of $line?

We should be able to work something out...

Link to comment
Share on other sites

Can you post the contents of $line?

We should be able to work something out...

post-36765-1213923585_thumb.gif post-36765-1213923602_thumb.gif

Here are the two outputs I get from the "netsh wlan show int". The first one is a blank pop up that I havent found away around, but that shouldnt be a problem right? When it goes to the next $line that stuff should be discarded or possibly merged. Anywho.. after I hit [OK] the second one pops up. What I have been trying to do is:

_StringSplit($line, @CR)
... then smash it into an array then sort the first 4 characters of each Array entry to find the SSID and take the next word after that when found. I just cant seem to get the code right, I am probably doing the backwards or something. It has been a VERY long week. Anywho.. is that the way to go about it or is there a better way? It just seems like there should be an easier way that I am not thinking of/finding. Thank you for your help! Edited by Shalm
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...