Jump to content
Sign in to follow this  
rscl

How to get IP, G/w of desktop (windows 7 & XP)

Recommended Posts

rscl

Hi,

How can I get the IP address & G/w address of any desktop ( compatible with windows 7 & XP).

Thank you.

Sans

Share this post


Link to post
Share on other sites
rscl

I found this thread" http://www.autoitscript.com/forum/index.php?showtopic=82733&st=0&p=811168&hl=ip%20address&fromsearch=1&#entry811168

But I could not get it to work.

I guess I am new to AutoIT.

Some help is appreciated.

Thx

Sans

Share this post


Link to post
Share on other sites
PeterAtkin

This may help i had a similar issues some time back..

#include <EventLog.au3> Local $local_domain = @LogonDNSDomain
 Local $domain_server = @LogonServer
 Local $gateway = GetLANobject("$objItem.DefaultIPGateway(0)", "$objItem.DefaultIPGateway(0)")
 Local $dns_server = GetLANobject("$objItem.DNSServerSearchOrder(0)", "$objItem.DNSServerSearchOrder(0)")
 Local $dhcp_server = GetLANobject("$objItem.DHCPServer(0)", "$objItem.DHCPServer(0)")
 Local $subnet_mask = GetLANobject("$objItem.IPSubnet(0)", "$objItem.IPSubnet(0)")
 Local $mac_address = GetLANobject("$objItem.MACAddress(0)", "$objItem.MACAddress(0)")

Func GetLanIP()
 ;http://www.autoitscript.com/forum/index.php?showtopic=109887&st=0&gopid=772563&#entry772563
 ;Thanks to Phodexis for this.
 ;http://msdn.microsoft.com/en-us/library/aa394217(VS.85).aspx
 Local $colItems = ""
 Local $strComputer = "localhost"
 Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
 Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", 0x10 + 0x20)
 If IsObj($colItems) Then
  For $objItem In $colItems
   If (StringLen($objItem.IPAddress(0)) > 3) And (StringLen($objItem.DefaultIPGateway(0)) > 3) Then
    If @IPAddress1 = $objItem.IPAddress(0) Then Return @IPAddress1
    If @IPAddress2 = $objItem.IPAddress(0) Then Return @IPAddress2
    If @IPAddress3 = $objItem.IPAddress(0) Then Return @IPAddress3
    If @IPAddress4 = $objItem.IPAddress(0) Then Return @IPAddress4
   EndIf
  Next
 Else
  _put_event(1, "What no IP?", @error)
  $error_code = 1
  Return @IPAddress1 ; if you dont have Gateway
 EndIf
EndFunc   ;==>GetLanIP

Func GetLANobject($obj, $obj2)
 Local $colItems = ""
 Local $strComputer = "localhost"
 Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
 Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", 0x10 + 0x20)
 If IsObj($colItems) Then
  For $objItem In $colItems
   If (StringLen($objItem.IPAddress(0)) > 3) And (StringLen(Execute($obj)) > 3) Then
    If @IPAddress1 = $objItem.IPAddress(0) Then Return Execute($obj2)
    If @IPAddress2 = $objItem.IPAddress(0) Then Return Execute($obj2)
    If @IPAddress3 = $objItem.IPAddress(0) Then Return Execute($obj2)
    If @IPAddress4 = $objItem.IPAddress(0) Then Return Execute($obj2)
   EndIf
  Next
 Else
  Return "N/A" ; if you dont have IPSubnet
 EndIf
EndFunc   ;==>GetLANobject

Func _put_event($value, $text, $error_id_code)
 ;SUCCESS = 0
 ;ERROR =1
 ;WARNING =2
 ;INFORMATION =4
 ;AUDIT_SUCCESS =8
 ;AUDIT_FAILURE =16
 Local $hEventLog, $aData[4] = [3, 1, 2, 3]

 $hEventLog = _EventLog__Open("", "Logon Script")
 _EventLog__Report($hEventLog, $value, 0, $error_id_code, @UserName, @CRLF & @CRLF & $text & @CRLF & @CRLF & "Contact Computer Facilities for more information, contact details can be found at [url="http://www.computer-facilities.com"]www.computer-facilities.com[/url] or e-mail [email="support@computer-facilities.com"]support@computer-facilities.com[/email]", $aData)
 _EventLog__Close($hEventLog)
EndFunc   ;==>_put_event

[topic='115020'] AD Domain Logon Script[/topic]

Share this post


Link to post
Share on other sites
evilertoaster

For ipaddress you can use built in macros: @IPAddress1

For Default Gateway: http://www.autoitscript.com/forum/index.php?showtopic=113623&view=findpost&p=794964

Share this post


Link to post
Share on other sites
rscl

@evilertoaster

I just found @IPAddress1 while reading the posts. Thx

But how do I get the default g/w ?

Is there something like @Defaultgw ?

Thx

Share this post


Link to post
Share on other sites
rscl

Peter,

I just run your script, but I got the following error:

G:\Desktop\aaa.au3(63,199) : ERROR: syntax error
 _EventLog__Report($hEventLog, $value, 0, $error_id_code, @UserName, @CRLF & @CRLF & $text & @CRLF & @CRLF & "Contact Computer Facilities for more information, contact details can be found at [url="http
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
G:\Desktop\aaa.au3 - 1 error(s), 0 warning(s)

Any idea what I did wrong ?

Thx

Share this post


Link to post
Share on other sites
evilertoaster

Default Gateway does not have a Macro for it, but you can get it from this function (as linked to above)

Func _GetDefaultGateway()
    Local $count = 0, $aRet[$count+1]
    $objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    $colNicConfigs = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", 48)
    For $objNicConfig In $colNicConfigs
        For $strIPDefaultIPGateway In $objNicConfig.DefaultIPGateway
            $count += 1
            ReDim $aRet[$count+1]
            $aRet[$count] = $strIPDefaultIPGateway
        Next
    Next
    $aRet[0] = $count
    Return $aRet
EndFunc   ;==>_GetDefaultGateway

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.