Jump to content
Renderer

AutoIT connect to wifi network

Recommended Posts

Renderer

Hello there! Is there any posibility to connect to a wifi network via AutoIt? Is there any COM Object able to do that? 

Thanks in advance.

Share this post


Link to post
Share on other sites
Renderer
On 12.04.2017 at 1:44 PM, JLogan3o13 said:

yes, you can use netsh, look at the connect command:

https://technet.microsoft.com/en-us/library/cc755301(v=ws.10).aspx

I think this command only allows me to connect to the networks i've got in the "Manage Wireless Networks" list. I can't connect to different wifi networks, that require a password.

Edited by Renderer

Share this post


Link to post
Share on other sites
JLogan3o13

Apologies, you stated "connect to wifi"; didn't realize you were after connecting to a new network, passing SSID, password, etc. The only way I have accomplished this is when doing it for a large number of machines: you configure it once manually, then export the config with netsh, then import on all your other machines. It doesn't sound like that is what you are after, however.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Renderer
14 hours ago, JLogan3o13 said:

Apologies, you stated "connect to wifi"; didn't realize you were after connecting to a new network, passing SSID, password, etc. The only way I have accomplished this is when doing it for a large number of machines: you configure it once manually, then export the config with netsh, then import on all your other machines. It doesn't sound like that is what you are after, however.

Let's assume i've got a new network on my list,that is not mine. Does your advices apply on this situation too? And let's assume i know the password too. How do i connect to the new network i've just discovered,with the given data (network name and password). 

Thanks for your previous answers.

Share this post


Link to post
Share on other sites
JLogan3o13

Again, the only way I know of doing it is to do so manually, then export the wifi profile using netsh. If you are looking to do it on only one device, however, that doesn't help you much. The only other option would be an attempt to manipulate the Manage Wireless Networks control panel item, which I have never had need to do, personally. Someone else may wander along with other suggestions.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
JLogan3o13

The first link does precisely what I am suggesting to do natively, connects through an existing (exported) profile. The sourceforge project states it can only connect if there is no authentication (open network). I am unfamiliar with the arubnetworks tool, but am hesitant on any application that is so undocumented.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Belini

@Renderer I also need to connect on different wifi networks that ask password to connect, did you get to do this? Can you give hint how to create a new profile to connect?

Share this post


Link to post
Share on other sites
Bilgus

So I looked at the links using 

netsh wlan export profile key=clear

in a cmd prompt as admin..

it was easy to get an xml file here is a simple reproduction for you

Note that the tabs aren't needed and you can probably use Execute() to make it cleaner but you get the idea

Local $Wifi_Name = "Wifi_network"
Local $Wifi_Auth = "WPAPSK"
Local $Wifi_Encrypt = "AES"
Local $Wifi_KeyType = "passPhrase"
Local $Wifi_Pass = "123456"
Local $Wifi_Xml = _
'<?xml version="1.0"?>' & @CRLF & _
'<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">' & @CRLF & _
@Tab & '<name>' & $Wifi_Name & '</name>'& @CRLF & _
@Tab & '<SSIDConfig>'& @CRLF & @Tab & @TAB & '<SSID>' & @CRLF & _
@Tab &@Tab & @Tab & '<hex>' & StringTrimLeft(StringToBinary($Wifi_Name), 2) & '</hex>' & @CRLF & _
@Tab &@Tab & @Tab & '<name>' & $Wifi_Name& '</name>'& @CRLF &@Tab & @TAB & '</SSID>'& @CRLF & _
@Tab & '</SSIDConfig>'& @CRLF & _
@Tab & '<connectionType>ESS</connectionType>'& @CRLF & _
@Tab & '<connectionMode>auto</connectionMode>'& @CRLF & _
@Tab & '<MSM>'& @CRLF & @Tab & @TAB &  '<security>'& @CRLF & @Tab & @Tab & @TAB & '<authEncryption>'& @CRLF & _
@Tab &@Tab & @Tab & @TAB &  '<authentication>'& $Wifi_Auth & '</authentication>'& @CRLF & _
@Tab &@Tab & @Tab & @TAB &  '<encryption>' & $Wifi_Encrypt & '</encryption>'& @CRLF & _
@Tab &@Tab & @Tab & @TAB &  '<useOneX>false</useOneX>'& @CRLF & @Tab & @Tab & @TAB & '</authEncryption>' & @CRLF & _
@Tab & @Tab & @TAB & '<sharedKey>'& @CRLF & _
@Tab &@Tab & @Tab & @Tab &'<keyType>' & $Wifi_KeyType & '</keyType>'& @CRLF & _
@Tab &@Tab & @Tab & @Tab &'<protected>false</protected>'& @CRLF & _
@Tab &@Tab & @Tab & @Tab &'<keyMaterial>' & $Wifi_Pass & '</keyMaterial>'& @CRLF & _
@Tab & @Tab & @Tab &'</sharedKey>'& @CRLF &@Tab & @Tab &'</security>'& @CRLF & @Tab & '</MSM>'& @CRLF & _
'</WLANProfile>'
;;;;
consoleWrite($Wifi_Xml)

 

Share this post


Link to post
Share on other sites
Belini

@Bilgus with your example I was able to see how to get the hex value but still need to know how to get the keymaterial encrypted because it does not accept to put the key as it is!

EDIT: @Bilgus the .xml in the way you created it did not work, I created it another way and it worked, now I just have to be able to encrypt the keymaterial so I can create a new profile from the network name and password!

Script to create a new profile

Local $name = "Algar"
Local $connectionType = "ESS"
Local $connectionMode = "auto"
Local $authentication = "WPAPSK"
Local $encryption = "AES"
Local $useOneX = "false"
Local $keyType = "passPhrase"
Local $protected = "true"
Local $keyMaterial = "01000000D08C9DDF0115D1118C7A00C04FC297EB01000000ABDCD6E3A4830D49AC271E6CF601A57F000000000200000000001066000000010000200000001AD5A2710D171BC7E630E643FDC673762CF3D841465D1510296CFC1E86E848FA000000000E80000000020000200000003593137CBDC0E2E5835904DEB9CB8EFE448C854AA0F1E886A26578BC6919477910000000F0F3C7B8F260245B02AA7944B1A7397540000000E20FB924B105B40B2CB570E2B6921E55DCDBD6DDC375C192B1D739658C61ECD3EBA58214DE35A57C23D9B6F31A83705A0253284EE5AD122BE5CE63B324BB416A"

Local $Wifi_Xml = '<?xml version="1.0"?>' & @CRLF & _
        '<WLANProfile xmlns="http://www.microsoft.com/networking/WLAN/profile/v1">' & @CRLF & _
        '   <name>' & $name & '</name>' & @CRLF & _
        '   <SSIDConfig>' & @CRLF & _
        '       <SSID>' & @CRLF & _
        '           <hex>' & StringTrimLeft(StringToBinary($name), 2) & '</hex>' & @CRLF & _
        '           <name>' & $name & '</name>' & @CRLF & _
        '       </SSID>' & @CRLF & _
        '   </SSIDConfig>' & @CRLF & _
        '   <connectionType>' & $connectionType & '</connectionType>' & @CRLF & _
        '   <connectionMode>' & $connectionMode & '</connectionMode>' & @CRLF & _
        '   <MSM>' & @CRLF & _
        '       <security>' & @CRLF & _
        '           <authEncryption>' & @CRLF & _
        '               <authentication>' & $authentication & '</authentication>' & @CRLF & _
        '               <encryption>' & $encryption & '</encryption>' & @CRLF & _
        '               <useOneX>' & $useOneX & '</useOneX>' & @CRLF & _
        '           </authEncryption>' & @CRLF & _
        '           <sharedKey>' & @CRLF & _
        '               <keyType>' & $keyType & '</keyType>' & @CRLF & _
        '               <protected>' & $protected & '</protected>' & @CRLF & _
        '               <keyMaterial>' & $keyMaterial & '</keyMaterial>' & @CRLF & _
        '           </sharedKey>' & @CRLF & _
        '       </security>' & @CRLF & _
        '   </MSM>' & @CRLF & _
        '</WLANProfile>'

ConsoleWrite($Wifi_Xml & @CRLF)
FileWrite(@ScriptDir & "\" & $name & ".xml", $Wifi_Xml)

With this script I can connect to the profile that was created

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <nativewifi.au3>
;#include <Array.au3>

Global $Cb_rede, $Inp_pass, $But_refresh, $But_connect, $sreason

_Wlan_StartSession(); inicia a sessão
;_wlan_deleteprofile("Algar"); deleta um perfil
Gui()

;$scan = _Wlan_GetNetworks(True)
;_ArrayDisplay($scan)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $But_refresh
            update_combo()
        Case $But_connect
            $rede = GUICtrlRead($Cb_rede)
            _wlan_setprofile(fileread(@ScriptDir & "\" & $rede & ".xml"), $sreason); importa um perfil
            _Wlan_Connect($rede, True); conecta a um perfil
    EndSwitch
WEnd

Func Gui()
    GUICreate("Wifi connection", 434, 104, -1, -1)
    GUICtrlCreateLabel("Rede wifi:", 8, 20, 51, 17)
    GUICtrlCreateLabel("Password:", 253, 20, 53, 17)
    $Cb_rede = GUICtrlCreateCombo("Choose a network", 64, 16, 169, 25, BitOR($CBS_DROPDOWN, $CBS_AUTOHSCROLL))
    update_combo()
    $Inp_pass = GUICtrlCreateInput("", 312, 16, 113, 21, BitOR($ES_PASSWORD, $ES_CENTER, $ES_NUMBER))
    $But_refresh = GUICtrlCreateButton("Refresh", 40, 64, 139, 25)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $But_connect = GUICtrlCreateButton("Connect", 246, 64, 139, 25)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    GUICtrlSetState($But_connect, $GUI_FOCUS)
    GUISetState(@SW_SHOW)
EndFunc   ;==>Gui

Func update_combo()
    Local $data = "", $scan = ""
    GUICtrlSetData($Cb_rede, "")
    $scan = _Wlan_GetNetworks(True)
    $avaiable = UBound($scan) - 1
    If $avaiable > 0 Then
        For $i = 0 To $avaiable
            $data &= $scan[$i][1] & "|"
        Next
        GUICtrlSetData($Cb_rede, $data, $scan[0][0])
    EndIf
EndFunc   ;==>update_combo

Does anyone know how I can encrypt the password for keymaterial?

Edited by Belini

Share this post


Link to post
Share on other sites
Bilgus

There is no such requirement that I see

at least for all the WPA stuff

I deleted my current test wifi network

I then used 

netsh wlan add profile filename="s:\Wireless.xml" user=all

It connected fine if you have to have the key encrypted then we are back to make connect the network manually and save the xml

You might also check by generating a new xml on your machine after creating the connection manually (don't forget key=clear) and see if they match

Other than this I don't know what to tell you

 

Share this post


Link to post
Share on other sites
Earthshine

He likes exclamation marks!


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
Belini
7 minutes ago, Bilgus said:

There is no such requirement that I see

at least for all the WPA stuff

I deleted my current test wifi network

I then used 

netsh wlan add profile filename="s:\Wireless.xml" user=all

It connected fine if you have to have the key encrypted then we are back to make connect the network manually and save the xml

You might also check by generating a new xml on your machine after creating the connection manually (don't forget key=clear) and see if they match

Other than this I don't know what to tell you

 

I can not create a profile manually because my program will be open all the time and the user will not have the taskbar, desktop, explorer and manager on.

I will put notebooks at commercial points and at each point I will find a different wifi network and with its own password there will give the user access only to choose the network, enter the password and connect.

 

Share this post


Link to post
Share on other sites
Belini

In this case you have the key to do the decryption and what I need is the opposite that would be to encrypt the password to put in keymaterial, maybe what I want is neither possible and only windows can encrypt the password stored in keymaterial.

EDIT: I think that in this way I will not be able to achieve this, I'm looking for other alternative here

Edited by Belini

Share this post


Link to post
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

×