Click here to Skip to main content
15,888,527 members
Articles / Programming Languages / VBScript
Article

How to get the IP Address from your workstation

Rate me:
Please Sign up or sign in to vote.
3.13/5 (8 votes)
15 Feb 2002 355.4K   25   18
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.

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

VBScript
' 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


Written By
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
AnswerA great way to find you're IP address Pin
shaialo29-Jun-09 5:23
shaialo29-Jun-09 5:23 
GeneralMy vote of 1 Pin
shaialo29-Jun-09 5:22
shaialo29-Jun-09 5:22 
GeneralAlmost a improved way to get ip...based on anne version Pin
leucos29-Aug-07 9:29
leucos29-Aug-07 9:29 
'The script have some changes to find out every network card...is still uncomplet at all but fully functional
'below reads the registry to check in the Bindings list for TCPIP to check which card to use
'modified by LeuCos ... v 1.2 !?

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

'Option explicit ' force all variables to be declared

' ************************************************************
' *** 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 z, x 'pointers
Dim key
Dim IPAddress
Dim tempIPAddress
Dim sh
Dim TCPIPKey
Dim InterfaceTmp
Dim Interface
Dim IPAddressKey

Set Sh = CreateObject("WScript.Shell")

TCPIPKey="HKLM\SYSTEM\CurrentControlSet\Services\TCPIP\"
InterfaceTmp=sh.regread(TCPIPKey&"Linkage\Bind")

While UBound(InterfaceTmp)-1 => z
If InStr(InterfaceTmp(z), "{") Then 'may i use Right, but this crashes...i don´t know whay?
Interface=Interface & StrReverse(Left(StrReverse(InterfaceTmp(z)), instr(StrReverse(InterfaceTmp(z)), "{\"))) & "#"
End If
z=z+1
Wend

Interface=Split(Interface, "#")
t=UBound(Interface)

While UBound(Interface)-1 => x
' First check which network card to use
IPAddressKey=TCPIPKey+"Parameters\Interfaces\"+Interface(x)+"\"

' Now read the IP Address from that card
tempIPAddress=sh.RegRead (IPAddressKey+"IPAddress")
IPAddress=tempIPAddress(0)

' If the address="0.0.0.0" then asume that this is DHCP
If IPAddress="0.0.0.0" Then
If (sh.RegRead(IPAddressKey+"EnableDHCP")=1) Then
On Error Resume Next
IPAddress=sh.RegRead (IPAddressKey+"DHCPIPAddress")
If Not Err.Number <> 0 Then x=UBound(Interface) End If
End If
End If
x=x+1
Wend
' the IP addresss is now readable from ipaddress

GetIPAddress=IPAddress
End Function


Function GetIPOctet (nOctet)
' This function retrieves a given octet out of the IP Address
Dim IPAddress

IPAddress=Split(GetIPAddress, ".", 4)
GetIPOctet=IPAddress(nOctet-1)
End Function


very good work lady...i searching for some thing like that 'cuz wmi don´t work in some pcs!?

it´s a possible...i have some problems with first version ... i had a old net card but i´ve removed them so they informations is on the registry and it´s crashes when try to find a broken informations...i add a error handle that works 2gether with pointer that is used to make a card select...but...it´s not useful when u use 2 functional net cards that use diferent ip's.
i expect some changes...
GeneralRe: Almost a improved way to get ip...based on anne version Pin
leucos22-Jan-09 0:27
leucos22-Jan-09 0:27 
Questioncan u tell me more abt this.-How to get the IP Address Pin
mafiaboy200229-Dec-06 4:37
mafiaboy200229-Dec-06 4:37 
GeneralSend IP to my server listener using VBScript Pin
John Shafferd7-Mar-04 5:41
sussJohn Shafferd7-Mar-04 5:41 
QuestionGet IP address (dhcp) from registry? Pin
debeliou bouche10-Jan-03 4:54
sussdebeliou bouche10-Jan-03 4:54 
GeneralPerhaps a Better Way Pin
Joshua Allen27-Feb-02 20:43
Joshua Allen27-Feb-02 20:43 
GeneralRe: Perhaps a Better Way Pin
Anne Jan Elsinga28-Feb-02 7:47
Anne Jan Elsinga28-Feb-02 7:47 
GeneralRe: Perhaps a Better Way Pin
Joshua Allen28-Feb-02 9:31
Joshua Allen28-Feb-02 9:31 
GeneralRe: Perhaps a Better Way Pin
S.M. Stefanov21-Jun-12 4:29
S.M. Stefanov21-Jun-12 4:29 
GeneralRewrite of IP check Pin
Anne Jan Elsinga19-Feb-02 8:50
Anne Jan Elsinga19-Feb-02 8:50 
GeneralIncorrect method to find the ip address Pin
srinivas vaithianathan19-Feb-02 8:14
srinivas vaithianathan19-Feb-02 8:14 
GeneralRe: Incorrect method to find the ip address Pin
Anne Jan Elsinga19-Feb-02 8:48
Anne Jan Elsinga19-Feb-02 8:48 
GeneralRe: Incorrect method to find the ip address Pin
inSiStKool11-Oct-05 10:00
inSiStKool11-Oct-05 10:00 
GeneralRe: Incorrect method to find the ip address Pin
27-Jun-02 5:40
suss27-Jun-02 5:40 
QuestionRe: Incorrect method to find the ip address Pin
tamirlavi27-Mar-07 7:05
tamirlavi27-Mar-07 7:05 

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

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