Windows Vista Forums

Hostname -> IP Address
  1. #1


    Todd Walton Guest

    Hostname -> IP Address

    Any ideas on how best to get the IP address of a hostname, using
    VBScript? Right now I'm exec'ing a ping command, which gives output
    like this:



    ===============================================

    H:\>ping cmp092

    Pinging cmp092.example.com [167.190.237.59] with 32 bytes of data:

    Reply from 167.190.237.59: bytes=32 time=1ms TTL=128
    Reply from 167.190.237.59: bytes=32 time<1ms TTL=128
    Reply from 167.190.237.59: bytes=32 time<1ms TTL=128
    Reply from 167.190.237.59: bytes=32 time<1ms TTL=128

    Ping statistics for 167.190.237.59:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 1ms, Average = 0ms

    ===============================================

    I iterate through line for line, find the one that starts with
    "Pinging" and then use a regex to pull out the IP address into a
    collection.

    Any better ideas?

    -todd

      My System SpecsSystem Spec

  2. #2


    Pegasus \(MVP\) Guest

    Re: Hostname -> IP Address


    "Todd Walton" <tdwalton@xxxxxx> wrote in message
    news:ed68aae7-0520-4ced-b746-4e7e0374d0bc@xxxxxx

    > Any ideas on how best to get the IP address of a hostname, using
    > VBScript? Right now I'm exec'ing a ping command, which gives output
    > like this:
    >
    > ===============================================
    >
    > H:\>ping cmp092
    >
    > Pinging cmp092.example.com [167.190.237.59] with 32 bytes of data:
    >
    > Reply from 167.190.237.59: bytes=32 time=1ms TTL=128
    > Reply from 167.190.237.59: bytes=32 time<1ms TTL=128
    > Reply from 167.190.237.59: bytes=32 time<1ms TTL=128
    > Reply from 167.190.237.59: bytes=32 time<1ms TTL=128
    >
    > Ping statistics for 167.190.237.59:
    > Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    > Approximate round trip times in milli-seconds:
    > Minimum = 0ms, Maximum = 1ms, Average = 0ms
    >
    > ===============================================
    >
    > I iterate through line for line, find the one that starts with
    > "Pinging" and then use a regex to pull out the IP address into a
    > collection.
    >
    > Any better ideas?
    >
    > -todd
    You could use this one-line batch file:
    @echo off
    for /F "tokens=3 delims=: " %%a in ('ping HostName ^| find /i "bytes="') do
    set IP=%%a



      My System SpecsSystem Spec

  3. #3


    Richard Mueller [MVP] Guest

    Re: Hostname -> IP Address

    Todd Walton wrote:

    > Any ideas on how best to get the IP address of a hostname, using
    > VBScript? Right now I'm exec'ing a ping command, which gives output
    > like this:
    >
    > ===============================================
    >
    > H:\>ping cmp092
    >
    > Pinging cmp092.example.com [167.190.237.59] with 32 bytes of data:
    >
    > Reply from 167.190.237.59: bytes=32 time=1ms TTL=128
    > Reply from 167.190.237.59: bytes=32 time<1ms TTL=128
    > Reply from 167.190.237.59: bytes=32 time<1ms TTL=128
    > Reply from 167.190.237.59: bytes=32 time<1ms TTL=128
    >
    > Ping statistics for 167.190.237.59:
    > Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    > Approximate round trip times in milli-seconds:
    > Minimum = 0ms, Maximum = 1ms, Average = 0ms
    >
    > ===============================================
    >
    > I iterate through line for line, find the one that starts with
    > "Pinging" and then use a regex to pull out the IP address into a
    > collection.
    >
    I have four VBScript example functions linked on this page that retrieve the
    IP address of the local computer:

    http://www.rlmueller.net/PingComputers.htm

    The page explains what is required for each function.

    --
    Richard Mueller
    MVP Directory Services
    Hilltop Lab - http://www.rlmueller.net
    --



      My System SpecsSystem Spec

  4. #4


    James Whitlow Guest

    Re: Hostname -> IP Address

    "Todd Walton" <tdwalton@xxxxxx> wrote in message
    news:ed68aae7-0520-4ced-b746-4e7e0374d0bc@xxxxxx

    > Any ideas on how best to get the IP address of a hostname, using
    > VBScript? Right now I'm exec'ing a ping command, which gives output
    > like this:
    >
    > ===============================================
    >
    > H:\>ping cmp092
    >
    > Pinging cmp092.example.com [167.190.237.59] with 32 bytes of data:
    >
    > Reply from 167.190.237.59: bytes=32 time=1ms TTL=128
    > Reply from 167.190.237.59: bytes=32 time<1ms TTL=128
    > Reply from 167.190.237.59: bytes=32 time<1ms TTL=128
    > Reply from 167.190.237.59: bytes=32 time<1ms TTL=128
    >
    > Ping statistics for 167.190.237.59:
    > Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    > Approximate round trip times in milli-seconds:
    > Minimum = 0ms, Maximum = 1ms, Average = 0ms
    >
    > ===============================================
    >
    > I iterate through line for line, find the one that starts with
    > "Pinging" and then use a regex to pull out the IP address into a
    > collection.
    >
    > Any better ideas?
    You can use 'Win32_PingStatus Class' if you are using Windows XP or
    above. This is the link to the Microsoft page:
    http://msdn.microsoft.com/en-us/libr...50(VS.85).aspx

    Below is a small sample:

    sHost = "MYCOMPUTER"

    For Each oIP in GetObject("winmgmts:").ExecQuery _
    ("SELECT * FROM Win32_PingStatus WHERE address = '" & sHost & "'")
    sIP = oIP.ProtocolAddress
    Next

    MsgBox sIP, , sHost



      My System SpecsSystem Spec

  5. #5


    Todd Walton Guest

    Re: Hostname -> IP Address

    On Sep 11, 4:07*pm, "James Whitlow" <jwhitlow.60372...@xxxxxx>
    wrote:

    > * * You can use 'Win32_PingStatus Class' if you are using Windows XP or
    > above. This is the link to the Microsoft page:http://msdn.microsoft.com/en-us/libr...50(VS.85).aspx
    Beautiful! Simple! I like it.

    Thank you James.

    -todd

      My System SpecsSystem Spec

Hostname -> IP Address problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
$ping.send(hostname) pings ipv6 instead of ipv4 address proxb PowerShell 3 04 Feb 2010
Net use with hostname fails, IP address works fine Luc Kumps Vista networking & sharing 2 04 Mar 2008
Extract hostname from distinguished name cmyers PowerShell 11 07 Feb 2008
Hostname to IP translation Joe Freedom PowerShell 14 12 Dec 2007
Invalid Hostname Oscette Vista networking & sharing 0 28 Jan 2007