how to find IP Adress

Discussion in 'Scripting' started by Miztiik, Mar 20, 2007.

  1. Miztiik

    Miztiik Guest

    i want to find the ip address fo the computer..

    how would i do it
     
    Miztiik, Mar 20, 2007
    #1
    1. Advertisements

  2. Miztiik

    McKirahan Guest

    If you have access to the computer, type
    "ipconfig" or "winipcfg"
    on a command line or prompt.
     
    McKirahan, Mar 20, 2007
    #2
    1. Advertisements

  3. Miztiik

    Miztiik Guest

    i want to do it in a vb script
     
    Miztiik, Mar 20, 2007
    #3
  4. Here is a function I use that runs on any 32-bit client:
    ================
    Function GetIPAddresses()
    ' Based on a Michael Harris script, modified by Torgeir Bakken
    '
    ' Returns array of IP Addresses as output
    ' by IPConfig or WinIPCfg...
    '
    ' Win98/WinNT have IPConfig (Win95 doesn't)
    ' Win98/Win95 have WinIPCfg (WinNt doesn't)
    '
    ' Note: The PPP Adapter (Dial Up Adapter) is
    ' excluded if not connected (IP address will be 0.0.0.0)
    ' and included if it is connected.

    Dim objShell, objFSO, objEnv, strWorkFile, objFile
    Dim arrData, intIndex, n, arrIPAddresses, arrParts

    Set objShell = CreateObject("wscript.shell")
    Set objFSO = CreateObject("scripting.filesystemobject")
    Set objEnv = objShell.Environment("PROCESS")
    If (objEnv("OS") = "Windows_NT") Then
    strWorkFile = objEnv("TEMP") & "\" & objFSO.GetTempName
    objShell.Run "%comspec% /c IPConfig >" & Chr(34) _
    & strWorkFile & Chr(34), 0, True
    Else
    ' WinIPCfg in batch mode sends output to
    ' filename WinIPCfg.out
    strWorkFile = "WinIPCfg.out"
    objShell.Run "WinIPCfg /batch", 0, True
    End If
    Set objShell = Nothing
    Set objFile = objFSO.OpenTextFile(strWorkFile)
    arrData = Split(objFile. ReadAll, vbCrLf)
    objFile.Close
    Set objFile = Nothing
    objFSO.DeleteFile strWorkFile
    Set objFSO = Nothing
    arrIPAddresses = Array()
    intIndex = -1
    For n = 0 To UBound(arrData)
    If (InStr(arrData(n), "IP Address") > 0) Then
    arrParts = Split(arrData(n), ":")
    If (InStr(Trim(arrParts(1)), "0.0.0.0") = 0) Then
    intIndex = intIndex + 1
    ReDim Preserve arrIPAddresses(intIndex)
    arrIPAddresses(intIndex)= Trim(CStr(arrParts(1)))
    End If
    End If
    Next
    GetIPAddresses = arrIPAddresses
    End Function
    ==========
    If the client has Windows XP or above you can use the Win32_PingStatus class
    for this. See this link:

    http://www.microsoft.com/technet/scriptcenter/scripts/hardware/monitor/hwmovb07.mspx?mfr=true

    You would revise this to return Address. Note that there can be more than
    one.
     
    Richard Mueller [MVP], Mar 20, 2007
    #4
  5. Strangely, I cannot find where people use Win32_PingStatus to retrieve the
    IP address of a computer. I found the following works for me, but I cannot
    say if it will work in all situations:

    ========
    Set objNetwork = CreateObject("Wscript.Network")
    strComputer = objNetwork.ComputerName

    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set colComputers = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strComputer & "'")

    For Each objComputer In colComputers
    Wscript.Echo objComputer.ProtocolAddress
    Next
    ========
    On XP clients, this retrieves the IPv4 IP addresses, on Vista it retrieves
    the IPv6 addresses. Off hand I don't know how to retrieve the IPv4 addresses
    on Vista. I also see that the function I posted earlier using IPConfig fails
    on Vista clients. All of this needs to be reinvented unfortunately. A
    revised function that worked on Vista for me simply replaced this statement
    in the GetIPAddresses function:

    If (InStr(arrData(n), "IP Address") > 0) Then

    with the following:

    If (InStr(arrData(n), "IPv4 Address") > 0) Then

    Finding a method that works on all clients may be a challenge.
     
    Richard Mueller [MVP], Mar 20, 2007
    #5
  6. Miztiik

    J Ford Guest

    sMachineName = "."

    Set objWMIService = GetObject("winmgmts:\\" & sMachineName & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT IPAddress FROM
    Win32_NetworkAdapterConfiguration WHERE IPEnabled='True'")

    For Each objItem In colItems
    wscript.echo Join(objItem.IPAddress, ",")
    Next
     
    J Ford, Mar 20, 2007
    #6
  7. Much better.

    --
    Richard Mueller
    Microsoft MVP Scripting and ADSI
    Hilltop Lab - http://www.rlmueller.net
    --

     
    Richard Mueller [MVP], Mar 20, 2007
    #7
  8. Miztiik

    Miztiik Guest

    Join(objItem.IPAddress, ",")

    what is the word Join do here

    and what about the "," thing here
     
    Miztiik, Mar 21, 2007
    #8
  9. Miztiik

    McKirahan Guest

    McKirahan, Mar 21, 2007
    #9
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.