Click here to Skip to main content
Licence 
First Posted 15 Feb 2002
Views 279,975
Bookmarked 23 times

How to get the IP Address from your workstation

By | 15 Feb 2002 | Article
How to get the IP Address from your workstation to use in your scripts

When using VBScript in a network environment you sometimes want to retrieve the IP Address to use it, for example, to retrieve the correct server from which you want to install software.

This isn't an easy task. The IP address isn't set in the environment when the computer boots, so we've got to use another method to retrieve the address.

The method which I use in the script below is reading the IP address from the CurrentControlSet in the registry. This makes sure that, even when you use DHCP, you get the right IP address.

The script contains two functions: GetIPAddress and GetIPOctet. With GetIPAddress you get an array which contains the IP address. Remember that element 0 in the array is actually the first octet of your IP address.

' Example on how to get a messagebox with the first octet of your IP Address.
wscript.echo GetIPOctet (1)

Above you find a little example which prints out the first octet of the IP address.

Here you find the complete script as it is now. Include it in your own programs at your own risk. Always be careful, since you are reading the registry.

' Script to retrieve the current IP Address on the first network card.
'
' (c) 2002 A.J. Elsinga
'      anne.jan@network-direct.com
'
'      version 1.0
 

' ************************************************************
' ***           Start of functions and procedures          ***
' ************************************************************

Function GetIPAddress
' This function retrieves the IP Address from the registry
' It gets it from the CurrentControlSet, so even when using DHCP 
' it returns the correct IP Address

' Declare variables

   Dim key
   Dim cTempIPAddress
   Dim cIPAddress
   dim cIPAddressKey


   Set oSh = CreateObject("WScript.Shell")

   cInterfacesKey="HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\"
   cNICSearch="HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\1\ServiceName"


' First check which network card to use
   cNicServiceName=oSh.RegRead(cNICSearch)

' Now read the IP Address from that card
   cIPAddressKey=cInterfaceskey + cNicServiceName+"\IPAddress"
   cTempIPAddress=oSh.RegRead (cIPAddresskey)
 
' Split the items in the var tempIPAddress to the octets in array IPAddress
   cIPAddress=split (cTempIPAddress(0),".",4)
 
' the IP addresss is now readable from ipaddress
' for example, to read the first octet, use: FirstOctet=IPAddress(0)
 
   GetIPAddress=cIPAddress
End Function

Function GetIPOctet (nOctet)
' This function retrieves a given octet out of the IP Address
    Dim IPAddress
   
    IPAddress=GetIPAddress
    GetIPOctet=IPAddress(nOctet-1)	
End Function

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Anne Jan Elsinga



Netherlands Netherlands

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
AnswerA great way to find you're IP address Pinmembershaialo5:23 29 Jun '09  
GeneralMy vote of 1 Pinmembershaialo5:22 29 Jun '09  
GeneralAlmost a improved way to get ip...based on anne version Pinmemberleucos9:29 29 Aug '07  
GeneralRe: Almost a improved way to get ip...based on anne version Pinmemberleucos0:27 22 Jan '09  
Questioncan u tell me more abt this.-How to get the IP Address Pinmembermafiaboy20024:37 29 Dec '06  
GeneralSend IP to my server listener using VBScript PinsussJohn Shafferd5:41 7 Mar '04  
QuestionGet IP address (dhcp) from registry? Pinsussdebeliou bouche4:54 10 Jan '03  
GeneralPerhaps a Better Way PinmemberJoshua Allen20:43 27 Feb '02  
GeneralRe: Perhaps a Better Way PinmemberAnne Jan Elsinga7:47 28 Feb '02  
GeneralRe: Perhaps a Better Way PinmemberJoshua Allen9:31 28 Feb '02  
GeneralRewrite of IP check PinmemberAnne Jan Elsinga8:50 19 Feb '02  
GeneralIncorrect method to find the ip address Pinmembervaithsr8:14 19 Feb '02  
GeneralRe: Incorrect method to find the ip address PinmemberAnne Jan Elsinga8:48 19 Feb '02  
GeneralRe: Incorrect method to find the ip address PinmemberinSiStKool10:00 11 Oct '05  
GeneralRe: Incorrect method to find the ip address PinmemberAnonymous5:40 27 Jun '02  
QuestionRe: Incorrect method to find the ip address Pinmembertamirlavi7:05 27 Mar '07  
I tried this script on a winXP pro, and I got an "unable to open registry key" on line 51.
Frown | :(

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 16 Feb 2002
Article Copyright 2002 by Anne Jan Elsinga
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid