Jump to content

using @IPAddress# macros to determine if a computer moved to another network


Recommended Posts

I have a drive mapping script that needs to know which office the computer is in or if the computer is out of the office for laptop users.  Each office has a static Public IP address which I obtain by launching a separate script at startup that runs _GetIP().

I save the public IP address to the registry so I only have to run _GetIP() once per startup because I have noticed it sometimes fails and I have to run it until I get an IP address.  If I run this within the drive mapping script, it may halt the script for several seconds (about 20 at the most I have noticed) and the user will think it is not running. That is why I have a separate script for determining the external IP address.

$PublicIP = _GetIP()
While $PublicIP = -1
    Sleep(1000)
    $attempt = $attempt + 1
    $PublicIP = _GetIP()
    If $attempt = 30 Then
        ExitLoop
    EndIf
WEnd

 

My drive mapping script will check the registry to determine what office it is in or if it is out of the office and map drives accordingly.  However my concern is if a laptop starts up while in the office and the laptop is put to sleep and woken back up while out of the office, the drive mapping script will think it is still in the office.  I was thinking about using the startup script to record @IPAddress1 to the registry and my drive mapping script will compare the first couple octets of @IPAddress1 with what was recorded.  If it is different, then it will know the network has changed and will run the startup script to determine which office it is in or if it is out of the office.

I also am not sure if using only @IPAddress1 is good enough or if I should record each @IPAddress macro since some computers may have multiple adapters.

Would comparing the IP address with the recorded IP address solve my problem or is there a better way to go about doing this.

Thank you in advance for any input!

Link to comment
Share on other sites

 

Would comparing the IP address with the recorded IP address solve my problem or is there a better way to go about doing this.

If by IP address you mean _GetIP() function then it wouldn't. That ip can be changed manually even if you are still connected to the same network. If you don't think that someone of your users could do that then I think it could work.

However I think _GetIP() function will not be working anymore as the server will go down, correct me if I am wrong. So either re-create the function adding external servers other than the standard ones used in the function, either find an other way.

Edit: my bad. Only a server was removed

Edited by AutID
Link to comment
Share on other sites

That's why I was thinking of comparing each @IPAddress macro.  I know I have a Virtual Box network connection that is the same no matter where I go.  My thought was to compare all 4 macros and if any of them are differnet, then I'm going to assume that the computer has probably moved to another network.

Another question.  Why are there 4 @IPAddress macros?  Where is Autoit getting them from?

Link to comment
Share on other sites

Some computers have more than one NIC in them, especially if it's a laptop. That's why @IPADDRESS goes up to 4. I've seen servers with 8.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

getaddrinfo() returns 4 IP address records to me, but three of them are IPv6 address types. I don't know why this works this way.

#include <Array.au3>

#region Part of my socket_UDF

Global Const $AF_UNSPEC = 0
Global Const $AF_INET = 2
Global Const $AF_INET6 = 23
Global Const $tagSockAddr_In = "short sin_family;USHORT sin_port;ULONG sin_addr;char sin_zero[8]"
Global Const $tagSockAddr_In6 = "short sin6_family;USHORT sin6_port;ULONG sin6_flowinfo;byte sin6_addr[16];ULONG sin6_scope_id"
Global Const $tagAddrInfo = "int ai_flags;int ai_family;int ai_socktype;int ai_protocol;uint ai_addrlen;ptr ai_canonname;ptr ai_addr;ptr ai_next"

;More than TCPNameToIP(), returns an array of IP addresses
Func _getaddrinfo($sNodeName, $AF = $AF_UNSPEC)
    Local $pResult, $tAddrInfo, $sIP, $tName
    Local $aRet = DllCall("ws2_32.dll", "int", "getaddrinfo", "str", $sNodeName, "ptr", 0, "ptr", 0, "ptr*", 0)
    $pResult = $aRet[4]
    Do
        $tAddrInfo = DllStructCreate($tagAddrInfo, $pResult)
        Switch DllStructGetData($tAddrInfo, "ai_family")
            Case $AF_INET
                $tName = DllStructCreate($tagSockAddr_In, DllStructGetData($tAddrInfo, "ai_addr"))
                $sIP &= _inet_ntoa(DllStructGetData($tName, "sin_addr")) & ";"
            Case $AF_INET6
                $tName = DllStructCreate($tagSockAddr_In6, DllStructGetData($tAddrInfo, "ai_addr"))
                $sIP &= _inet_ntop(DllStructGetPtr($tName, "sin6_addr"), $AF_INET6) & ";"
        EndSwitch
        $pResult = DllStructGetData($tAddrInfo, "ai_next")
    Until $pResult = 0
    _freeaddrinfo($aRet[4])
    Return StringSplit(StringTrimRight($sIP, 1), ";", 2)
EndFunc   ;==>_getaddrinfo

;More than TCPNameToIP(), returns an array of IP addresses
Func _getaddrinfo_old($sNodeName)
    Local $pResult, $tAddrInfo, $sIP
    Local $aRet = DllCall("ws2_32.dll", "int", "getaddrinfo", "str", $sNodeName, "ptr", 0, "ptr", 0, "ptr*", 0)
    $pResult = $aRet[4]
    Do
        $tAddrInfo = DllStructCreate($tagAddrInfo, $pResult)
        Local $tName = DllStructCreate($tagSockAddr_In, DllStructGetData($tAddrInfo, "ai_addr"))
        $sIP &= _inet_ntoa(DllStructGetData($tName, "sin_addr")) & ";"
        $pResult = DllStructGetData($tAddrInfo, "ai_next")
    Until $pResult = 0
    _freeaddrinfo($aRet[4])
    Return StringSplit(StringTrimRight($sIP, 1), ";", 2)
EndFunc   ;==>_getaddrinfo

Func _freeaddrinfo($pAddrInfo)
    DllCall("ws2_32.dll", "none", "freeaddrinfo", "ptr", $pAddrInfo)
EndFunc   ;==>_freeaddrinf

Func _inet_ntoa($pIn_Addr) ;only IPv4 (deprecated)  --> use _inet_ntop() instead
    Local $aRet = DllCall("ws2_32.dll", "str", "inet_ntoa", "ptr", $pIn_Addr)
    Return $aRet[0]
EndFunc   ;==>_inet_ntoa

Func _inet_ntop($pIn_Addr, $iFamily = $AF_INET) ;IPv4 and IPv6 --> Vista and above
    Local $tBuffer = DllStructCreate("char[46]") ;16 for IPv4, 46 for IPv6
    Local $aRet = DllCall("ws2_32.dll", "ptr", "inet_ntop", "int", $iFamily, "ptr", $pIn_Addr, "struct*", $tBuffer, "int", DllStructGetSize($tBuffer))
    Return DllStructGetData($tBuffer, 1)
EndFunc   ;==>_inet_ntop

Func _WSAGetLastError()
    Local $aRet = DllCall("ws2_32.dll", "int", "WSAGetLastError")
    Return $aRet[0]
EndFunc   ;==>_WSAGetLastError

#endregion Part of my socket_UDF

ConsoleWrite(@IPAddress1 & @LF)
ConsoleWrite(@IPAddress2 & @LF)
ConsoleWrite(@IPAddress3 & @LF)
ConsoleWrite(@IPAddress4 & @LF)
ConsoleWrite(@LF)

TCPStartup()
Global $aIP = _getaddrinfo_old("..localmachine")
Global $aIP6 = _getaddrinfo("..localmachine")
TCPShutdown()

_ArraySort($aIP, 1)
_ArraySort($aIP6, 1)

For $i = 0 To UBound($aIP) -1
    ConsoleWrite($aIP[$i] & @LF)
Next
ConsoleWrite(@LF)
For $i = 0 To UBound($aIP6) -1
    ConsoleWrite($aIP6[$i] & @LF)
Next

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Link to comment
Share on other sites

I am a little bit lost with what you are trying to achieve here?
So each office has a public IP address which you save at the startup of what you are doing.

And you want to check the computers and laptops connection to determine in which office they are connected or if they are connected else where. Is that right?

If that is the case then why don't you simply get the public ip of each laptop or computer, loop through throw your array with the different public ip addresses of the offices and then either exit when you find the offices addres, either return  "other connection" of it isn't in the list which will tell you that it is connected elsewhere...???

Link to comment
Share on other sites

So he is running the script from 1 computer and checking all other computers and offices like this?

Global $sReturn = False
;~ your separate script at startup that runs _GetIP() will return an array like this i suppose.
Global $sOffices[10] = ["OfficeIP1", "OfficeIP2", "OfficeIP3", "OfficeIP4", "OfficeIP5", "OfficeIP6", "OfficeIP7", "OfficeIP8", "OfficeIP9", "OfficeIP10"]   ; Arrays of IPs of offices
;~ and these are you computers
Global $sComputers[10][2] = [["Computer1", "OfficeIP1"], _     ;Array of the computers and their addresses
   ["Computer2", "OfficeIP2"], _
   ["Computer3", "OfficeIP3"], _
   ["Computer4", "IP4"], _
   ["Computer5", "OfficeIP5"], _
   ["Computer6", "OfficeIP6"], _
   ["Computer7", "OfficeIP7"], _
   ["Computer8", "OfficeIP8"], _
   ["Computer9", "OfficeIP9"], _
   ["Computer10", "OfficeIP10"]]
   For $i = 0 To UBound($sComputers) -1
    For $j = 0 To UBound($sOffices) -1
     If $sComputers[$i][1] = $sOffices[$j] Then
      $sReturn = True
      ExitLoop
     Else
      $sReturn = False
      ContinueLoop
     EndIf
    Next
    If $sReturn Then
     ConsoleWrite($sComputers[$i][0] & " is connected to office " & $sOffices[$j] & @LF)
    Else
     ConsoleWrite($sComputers[$i][0] & " is not connected to an office." & @LF)
    EndIf
   Next

or running each script on each computer?
If the second then only loop through the officesip array and determine where you are by the return.

If the problem is not to continually do this then you gave the answer in the #post3

Link to comment
Share on other sites

I really want to know how can you compare _GetIP() with @IPAddress1.
_GetIP returns the public ip address of the network and @IPAddress1 returns the ip address of the first network adapter.

You are saving the public IP addresses of the offices on start up with _GetIP() and then running a seperate script to determine in which office you are connected by comparing the public ip addresses of the offices with your network adapters ipaddress.
How can you compare this 127.0.0.1 or this 192.168.1.4 with something like this 83.103.281.741

Edited by AutID
Link to comment
Share on other sites

S/He is not.

That's why I was thinking of comparing each @IPAddress macro.  I know I have a Virtual Box network connection that is the same no matter where I go.  My thought was to compare all 4 macros and if any of them are differnet, then I'm going to assume that the computer has probably moved to another network.

Another question.  Why are there 4 @IPAddress macros?  Where is Autoit getting them from?

But for the record @ipaddress1 can return your public IP.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

It does not for me now, but if I take the router out of the equation and connect directly to cable modem it does.

Because the DHCP server assigns network card that.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

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...