Announcement

Collapse
No announcement yet.

Discover the router or connection name?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Discover the router or connection name?

    I work on a laptop and in a given week, I could be working at one of three locations.

    I would like to write a program that, when I open the laptop and it awakens from hibernation, will query the connection point and learn its name (or some similar identification).

    Based on that location, the program will then handle a number of setup tasks, including such things as (among a bunch of other things) informing me of which local printers are available.

    This is way out of my knowledge area, so I need a place to start and from which to move forward. My concern is these two tasks:

    1. How to tell when the laptop comes out of hibernation?
    2. How to query the connection point (for me, they're routers) and obtain the name? (My connections are typically from the list of wireless connections, but can also be via Ethernet cable...)

    I don't even know the proper terminology, so my searches online, in various Help files, and in Source Code forum postings are not turning up anything useful to study.

    So, I'm asking for guidance - where do I look to read, learn, and study?

    Thanks,
    -John




  • #2
    Click image for larger version  Name:	john.png Views:	0 Size:	9.6 KB ID:	782398
    You could execute a program or a batch file like the above "something.bat."
    Later also create a shortcut on the desktop to the .bat file or .exe file.

    To automatically start when the computer turns on it could be saved to a windows startup folder.
    shell:command startup will open one of the windows startup folders which can be used

    This was done without programming, but might be of some help.

    rem something.bat
    echo off
    cls
    hostname
    net use
    net share
    pause
    ipconfig
    winver
    pause
    systeminfo
    pause
    many other commands/programs

    Note: All the above commands could be piped to a file or files and viewed as a single document.
    Many commands have switches to display only the needed results.

    If all working with command line tools in a batch file then Windows api functions can optionally be found to write a program.

    About all command have help files with them like ipconfig /?
    ipconfig /? > ipconfig.txt
    ipconfig.txt

    Some people on these forums are network administrators and could give you exactly want you want

    If you need a common name to connect to yourself over the internet when you travel, check out www.freemyip.com
    It updates your current local ip address to a name on there server if you visit there server once, example: heidi.freemyip.com
    Really nice, free and no program needed for this simple dynamic dns.
    Last edited by Mike Doty; 27 Jun 2019, 09:02 AM.
    https://duckduckgo.com instead of google

    Comment


    • #3
      Thanks Mike, lots to explore from that post... (I'm driving for a few days, so will be experimenting evenings...)

      I've found a few other things that look promising:
      - It appears that I can set an event in the Scheduler to run when "awakening", so should be easy to invoke my program at the proper time.
      - Alternatively there are "power manager" functions that I'm looking into... like ManagementEventWatcher, or a counterpart to SetSystemPowerState

      I can see that ipconfig returns the "default gateway" IP, so I suppose I can use that instead of a text string...
      I see how I can direct output from ipconfig to a file, then open and parse it.

      Thanks!
      -John

      FOUND IT!
      The commandline would be:
      Code:
      netsh wlan show int  > NETSH.TXT
      Then parse for the value of "SSID" or "Profile" ...

      I have verified that this command will capture the info, so next, I will have to build the proper SHELL statement...later, when I'm running my laptop.

      -John






      Comment


      • #4
        Here's what's working for me now. It can certainly be tightened up a lot, but for now I'm on to other challenges...

        I hope it's helpful to someone!
        -John

        [ADDED: I kinda wish I knew how to get this result via some API call, and avoid the reliance on NETSH and parsing the redirected file...]



        Code:
        FUNCTION GetLocalGatewayName AS STRING
           LOCAL sTemp, sCmd, sWork, sTemp2 AS STRING
           LOCAL i, lRet, lFileBuf, lPos1, lPos2 AS LONG
        
           sTemp = EXE.PATH$ & "NWSI.txt"
           IF ISFILE(sTemp) THEN KILL sTemp
        
           sCmd  = ENVIRON$("COMSPEC") + " /C NETSH.exe WLAN SHOW INT > " & sTemp
           SHELL(sCmd, 6)
        
           lFileBuf = FREEFILE
           OPEN sTemp FOR BINARY AS #lFileBuf
           GET$ #lFileBuf, LOF(#lFileBuf), sWork
           CLOSE #lFileBuf
        
           lPos1 = INSTR(sWork, "SSID")
           lPos2 = INSTR(lPos1 + 1, sWork, $CRLF)
           sTemp2 = TRIM$(MID$(sWork, lPos1 +4 TO lPos2), ANY $SPC & ":" & $CRLF)
        
           IF ISFILE(sTemp) THEN KILL sTemp
           FUNCTION = sTemp2
        END FUNCTION

        Comment


        • #5
          Hi John, first I could not get it to work, but then I realized that some of the names in my path to the tempfile contained more than 8 signs.

          I added two $DQ at the end and then it worked just fine.
          sCmd = ENVIRON$("COMSPEC") + " /C NETSH.exe WLAN SHOW INT > " & $DQ & sTemp & $DQ

          Does anybody know if there is a API call to get the same info?

          /Mikael

          Comment


          • #6
            Mikael,
            I just started trying to find API calls equivalent to NETSH.exe...

            So far, found this: https://stackoverflow.com/questions/...ia-windows-sdk but I'm not sure how helpful it's going to be. I have a lot more searching to do...

            -John


            Comment


            • #7
              https://www.cezeo.com/tips-and-trick...dministration/

              The helper DLL's: netsh show helper

              You might find what DLL these utilities use:
              http://www.nirsoft.net/network_tools.html
              https://duckduckgo.com instead of google

              Comment


              • #8
                WinAPI WlanGetAvailableNetworkList and WlanScan may get you what you want.

                I could only test code below on my laptop, so I don't know how universal it is.

                It shows how to get SSID and connected/disconnected status

                Code:
                #COMPILE EXE ' PBwin10    Jose Roca Includes
                #DIM ALL
                #INCLUDE "Win32Api.inc"
                #INCLUDE "wlanapi.inc"
                
                '______________________________________________________________________________
                
                FUNCTION GetFirstInterfaceGui CDECL ALIAS "GetFirstInterfaceGui"() AS STRING
                 LOCAL pLanInterfaceInfoList AS WLAN_INTERFACE_INFO_LIST PTR
                 LOCAL NegotiatedVersion, ClientHandle AS DWORD
                
                 IF WlanOpenHandle(BYVAL 2, BYVAL 0, NegotiatedVersion, ClientHandle) = %ERROR_SUCCESS THEN
                   IF WlanEnumInterfaces(ClientHandle, BYVAL 0, pLanInterfaceInfoList) = %ERROR_SUCCESS THEN
                     IF (pLanInterfaceInfoList) AND (@pLanInterfaceInfoList.dwNumberOfItems) THEN
                       FUNCTION = GUIDTXT$(@pLanInterfaceInfoList.InterfaceInfo(0).InterfaceGuid)
                     END IF
                     WlanFreeMemory(pLanInterfaceInfoList)
                   END IF
                   WlanCloseHandle(ClientHandle, BYVAL 0)
                 END IF
                
                END FUNCTION
                '______________________________________________________________________________
                
                FUNCTION GetSSID CDECL ALIAS "GetSSID"(gui AS STRING) AS STRING
                 LOCAL NegotiatedVersion, ClientHandle AS DWORD
                 LOCAL inter AS GUID
                 LOCAL pWlist AS WLAN_AVAILABLE_NETWORK_LIST PTR
                
                 inter = GUID$(GUI)
                 IF WlanOpenHandle(BYVAL 2, BYVAL 0, NegotiatedVersion, ClientHandle) = %ERROR_SUCCESS THEN
                   IF WlanScan(ClientHandle, inter, BYVAL 0, BYVAL 0, BYVAL 0) = %ERROR_SUCCESS THEN
                     IF WlanGetAvailableNetworkList(ClientHandle, inter, BYVAL 2, BYVAL 0,BYVAL VARPTR(pWlist)) = %ERROR_SUCCESS THEN
                         IF (@pWlist.network(0).dwFlags AND %WLAN_AVAILABLE_NETWORK_CONNECTED) = 1 THEN
                           FUNCTION = "Connected : " + @pWlist.network(0).dot11Ssid.ucSSID
                         ELSE
                           FUNCTION = "Not Connected : " + @pWlist.network(0).dot11Ssid.ucSSID
                         END IF
                     WlanFreeMemory(pWlist)
                     END IF
                   END IF
                
                   WlanCloseHandle(ClientHandle, BYVAL 0)
                 END IF
                END FUNCTION
                '______________________________________________________________________________
                
                FUNCTION PBMAIN()
                
                 MSGBOX GetSSID(GetFirstInterfaceGui)
                
                END FUNCTION

                Comment


                • #9
                  @Mike, I've been browsing those sites (and others that they've led to), and they look good, but they do raise another question (of course!)...
                  I see a list of .DLLs like that, but I have no info on what routines they contain, no prototypes, no explanations of how to use...
                  So I do searches for one of the filenames in: the Win32 Help file, the Jose Roca include files, MSDN, and google ...
                  Sometimes I get hits, but most often not. Even if I get a hit, the information is not readily understandable, and often takes me awhile to
                  absorb it, then determine if it's relevant. Then comes attempts at code...

                  So, the question is: do you know of a site that contains good info on what routines and structures are in those files?

                  Any guidance would be gladly accepted!



                  @Rod, That code looks educational - in other words, it's unfamiliar enough to me that I'll devote some study time to it. Thanks!

                  Thanks, both of you!
                  -John


                  Comment


                  • #10
                    Thank you @Rod,

                    I was looking at Pierre Bellisle code at http://pages.videotron.com/lyra/PowerBASIC/index.html as a starting point, but your example made it easier.

                    PB have a great forum /Mikael

                    Comment


                    • #11
                      Hi Rod
                      Your code in # 8 only scans for the first SSID and how do we enumerate or list out all the SSID that are available ?

                      Thanks in advance

                      Comment


                      • #12
                        Maybe knowing your public IP address would be helpful as well.
                        You can get it from the command line like this:
                        Code:
                        nslookup myip.opendns.com resolver1.opendns.com
                        

                        Comment


                        • #13
                          Maybe knowing your public IP address would be helpful as well.
                          You can get it from the command line like this:
                          How? Is myip supposed to be your web server?

                          nslookup myip.opendns.com resolver1.opendns.com
                          Server: Unknown
                          Address: 2620:119:35::35

                          www.whatismyip.com www.whatismyipaddress.com www.howtofindmyipaddress.com www.geobytes.com/iplocator/ www.iplocation.net/
                          Last edited by Mike Doty; 10 Jul 2019, 07:31 PM.
                          https://duckduckgo.com instead of google

                          Comment

                          Working...
                          X