connecting to WMI on remote computer using IP Address

Posted on 2004-04-22
Last Modified: 2010-05-18

I am connecting to WMI on the remote computer using the computername as follows

here is the code.

      Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")                        
'Set objSWbemServices = objSWbemLocator.ConnectServer _
 (Computername,"root\cimv2", "Computername\localadmin","password")

this code works perfectly fine..which actually connects using the LocalAdmin accounts of the remote computer.

but, in some of the cases...i do not get the "computername" for the remote computer.I only have the IP Address.

So, in that case when i try to use the code...

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")                        
'Set objSWbemServices = objSWbemLocator.ConnectServer _
 (IPAddress,"root\cimv2", "IPAddress\localadmin","password")

it give me an error as
"The object exporter specified was not found"

so, does anybody know how do i connect to the remote computer using the IP Address, using the above code ?

thanks a lot.
Question by:godkedar

Expert Comment

ID: 10900218
Hi godkedar,

think youre code is OK...

...except the use of "IPAddress\localadmin" - try ".\localadmin" instead of IPAddress.

The dot will be interpreted as the local-machinename - to wich you are connected successfully via an IPAddress.

Happy Wbemming!


Accepted Solution

Level10Access earned 125 total points
ID: 10902263
I think he wants to connect to a REMOTE computer. Also, i did notice that apostrophe in front of the second line. Did you remember to take that off? Also, try to just use the username and password without the "ip\":

Set objSWbemLocator = CreateObject("WbemScripting.SWbemLocator")                    
Set objSWbemServices = objSWbemLocator.ConnectServer_ IPAddress,"root\cimv2", "localadmin","password")

Assisted Solution

plq earned 125 total points
ID: 10912018
Here's the code to lookup a computer name from an ip address, using sockets.

Just put this code in a new module and use

remotecomputername = getname(sipaddress)

Then you can call this from your code before doing the WMI connectserver


Option Explicit
Option Compare Text

Private Const WS_VERSION_REQD = &H101
Private Const MIN_SOCKETS_REQD = 1
Private Const SOCKET_ERROR = -1
Private Const WSADESCRIPTION_LEN = 256
Private Const WSASYS_Status_Len = 128

Const AF_INET = 2

Private Type WSADATA
    wHighVersion As Long
    szDescription As String * WSADESCRIPTION_LEN
    szSystemStatus As String * WSASYS_Status_Len
    iMaxSockets As Long
    iMaxUdpDg As Long
    lpVendorInfo As Long
End Type

Private Type HOSTENT
    h_name As Long 'LPSTR
    h_aliases As Long 'LPLPSTR
    h_addrtype As Integer
    h_length As Integer
    h_addr_list As Long 'char FAR * FAR * h_addr_list
End Type
Private Type sockaddr_in
        sin_family As Long
        sin_port As Integer
        in_addr As Long
        sin_zero As String * 8
End Type

Private Type in_addr
    b1 As Byte
    b2 As Byte
    b3 As Byte
    b4 As Byte
End Type
Private Declare Sub RtlMoveMemory Lib "kernel32" (hpvDest As Any, ByVal hpvSource&, ByVal cbCopy&)
Private Declare Function gethostname Lib "WSOCK32.DLL" (ByVal hostname$, ByVal HostLen As Long) As Long
Private Declare Function WSAStartup Lib "WSOCK32.DLL" (ByVal wVersionRequested As Integer, ByRef lpWSADATA As WSADATA) As Long
Private Declare Function WSACleanup Lib "WSOCK32.DLL" () As Long
Private Declare Function gethostbyaddr Lib "WSOCK32.DLL" (addr As Any, ByVal iLen As Long, ByVal itype As Long) As Long
Private Declare Function getnameinfo Lib "WSOCK32.DLL" (addr As Any, ByVal iLen As Long, ByVal host As String, ByVal HostLen As Long, ByVal serv As String, ByVal servlen As Long, ByVal flags As Long) As Long
Private Declare Function inet_addr Lib "WSOCK32.DLL" (ByVal cp As String) As Long
Private Declare Function gethostbyname Lib "WSOCK32.DLL" (ByVal name As String) As Long 'HOSTENT
Private Declare Function WSAGetLastError Lib "WSOCK32.DLL" () As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Long) As Long
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, lpString2 As Any) As Long

Public Function GetName(cpAddr As String) As String
    Dim Adata As WSADATA
    Dim addr As Long
    Dim p As Long
    Dim host As HOSTENT
    Dim buf As String
    Dim sHost As String
    Dim sServ As String
    Dim sArr() As String
    Dim sWork As String
    sWork = ""
    If WSAStartup(&H101, Adata) <> 0 Then Exit Function
    addr = inet_addr(cpAddr & Chr$(0))
    If addr <> INADDR_NONE Then
        p = gethostbyaddr(ByVal VarPtr(addr), 4, AF_INET)
        If p Then
            CopyMemory ByVal VarPtr(host), ByVal p, LenB(host)
            buf = Space(lstrlen(host.h_name))
            lstrcpy buf, ByVal host.h_name
            sWork = Trim(Left(buf, lstrlen(host.h_name)))
        End If
    End If
    If InStr(sWork, ".") > 0 Then
        sArr = Split(sWork, ".")
        If sArr(0) <> "" Then
            sWork = sArr(0)
        End If
    End If
    GetName = sWork
End Function

Private Function SocketsInitialize() As String
    Dim iReturn As Integer
    Dim sLowByte As String, sHighByte As String, sMsg As String

    iReturn = WSAStartup(WS_VERSION_REQD, WSAD)

    If iReturn <> 0 Then
        SocketsInitialize = "Socket error " & Trim$(Str$(iReturn)) & " occurred in Startup "
        Exit Function
    End If

    SocketsInitialize = sMsg
End Function

Private Function SocketsCleanup() As String
    Dim lReturn As Long
    lReturn = WSACleanup()

    If lReturn <> 0 Then
        SocketsCleanup = "Socket error " & Trim$(Str$(lReturn)) & " occurred in Cleanup "
    End If

End Function

Private Function hibyte(ByVal wParam As Integer)

    hibyte = wParam \ &H100 And &HFF&

End Function

Private Function lobyte(ByVal wParam As Integer)

    lobyte = wParam And &HFF&

End Function


Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
withoutTen challenge 14 88
MaxSpan challenge 9 68
advertisement module in core php 4 94
C# DataGridView_RowsAdded event not firing 1 40
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
A short article about problems I had with the new location API and permissions in Marshmallow
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now