Windows Vista Forums

Convert hostname text file to IP text file
  1. #1


    indytoatl Guest

    Convert hostname text file to IP text file

    I'm trying to convert a list of servers names from hostnames to IP
    addresses. Can someone tell
    me whats wrong with my script? thanks


    Const FOR_READING = 1
    Const FOR_APPENDING = 8


    Dim WshShell, objInputFile, objOutputFile
    Dim objNetwork, IP
    Dim RegEx, WSH, strTarget, arrTargets

    Set RegEx = New RegExp

    Set WSH = CreateObject("WScript.Shell")
    Set WshShell = WScript.CreateObject("WScript.Shell")
    Set FileSys = CreateObject("Scripting.FileSystemObject")
    Set objNetwork = WScript.CreateObject("WScript.Network")



    Set objInputFile = FileSys.OpenTextFile("c:\hostnames.txt",
    For_Reading)
    Set objOutputFile = FileSys.CreateTextFile("c:\IP.txt", For_Appending)

    arrServers = (objInputFile.ReadAll)
    arrTargets = Split(arrServers, vbTab)
    objInputFile.Close





    'WScript.Echo arrTargets(0)

    For Each strTarget In arrTargets

    Set oExec = WSH.Exec("ping.exe -n 2 " & strTarget)
    RegEx.Pattern = "^Reply from ([^:]+).*$"


    Do Until oExec.Status = 0
    WScript.Sleep 100
    Loop

    Do Until oExec.StdOut.AtEndOfStream
    Line = oExec.StdOut.ReadLine
    If RegEx.Test(Line) Then IP = RegEx.Replace(Line, "$1")
    Loop

    WScript.Echo IP
    objOutputFile.WriteLine IP

    Next

      My System SpecsSystem Spec

  2. #2


    Richard Mueller [MVP] Guest

    Re: Convert hostname text file to IP text file


    "indytoatl" <indytoatl@xxxxxx> wrote in message
    news:a2a96951-aa65-4acb-9650-f6115232580f@xxxxxx

    > I'm trying to convert a list of servers names from hostnames to IP
    > addresses. Can someone tell
    > me whats wrong with my script? thanks
    >
    >
    > Const FOR_READING = 1
    > Const FOR_APPENDING = 8
    >
    >
    > Dim WshShell, objInputFile, objOutputFile
    > Dim objNetwork, IP
    > Dim RegEx, WSH, strTarget, arrTargets
    >
    > Set RegEx = New RegExp
    >
    > Set WSH = CreateObject("WScript.Shell")
    > Set WshShell = WScript.CreateObject("WScript.Shell")
    > Set FileSys = CreateObject("Scripting.FileSystemObject")
    > Set objNetwork = WScript.CreateObject("WScript.Network")
    >
    >
    >
    > Set objInputFile = FileSys.OpenTextFile("c:\hostnames.txt",
    > For_Reading)
    > Set objOutputFile = FileSys.CreateTextFile("c:\IP.txt", For_Appending)
    >
    > arrServers = (objInputFile.ReadAll)
    > arrTargets = Split(arrServers, vbTab)
    > objInputFile.Close
    >
    >
    >
    > 'WScript.Echo arrTargets(0)
    >
    > For Each strTarget In arrTargets
    >
    > Set oExec = WSH.Exec("ping.exe -n 2 " & strTarget)
    > RegEx.Pattern = "^Reply from ([^:]+).*$"
    >
    >
    > Do Until oExec.Status = 0
    > WScript.Sleep 100
    > Loop
    >
    > Do Until oExec.StdOut.AtEndOfStream
    > Line = oExec.StdOut.ReadLine
    > If RegEx.Test(Line) Then IP = RegEx.Replace(Line, "$1")
    > Loop
    >
    > WScript.Echo IP
    > objOutputFile.WriteLine IP
    >
    > Next
    The script works for me except:

    1. I cannot write to the root of C: drive on my Vista computer. I assume the
    same is true of W2k8 and Win7. I believe this can be corrected by using "Run
    as administrator".
    2. My firewall requires that I acknowledge all ping requests, so that
    blocked the script.
    3. I could not get the script to read a tab delimited file correctly.

    When I run the script on a W2k3 client with no firewall in my test domain it
    worked, after I wrote one computer name per line in the file hostnames.txt
    and changed:

    arrTargets = Split(arrServers, vbTab)

    to:

    arrTargets = Split(arrServers, vbCrLf)

    However, the IP address for the last computer was listed twice until I added
    a check for blank lines to the script. That is, I used:

    For Each strTarget In arrTargets
    If (Trim(strTarget) <> "") Then
    .....

    End If
    Next

    If you get an error, what is it?

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



      My System SpecsSystem Spec

Convert hostname text file to IP text file problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
compare two text files and report differences in a text file format Larry__Weiss PowerShell 7 13 Jan 2010
Append text to top of text file? Get-James PowerShell 20 23 Feb 2009
Looping Through A Text File To Find Patterns From Another Text Fil GWold PowerShell 5 26 Sep 2008
Howto: Add lines of text from a specific point in a text file.. Daz VB Script 13 24 Jun 2008
How do I read a text file and sort text by fixed positions? Cornelius PowerShell 5 20 Jul 2007