Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Convert hostname text file to IP text file

Reply
 
Old 05-25-2009   #1 (permalink)
indytoatl


 
 

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
Old 05-25-2009   #2 (permalink)
Richard Mueller [MVP]


 
 

Re: Convert hostname text file to IP text file


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

> 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
Reply

Thread Tools


Similar Threads
Thread Forum
Append text to top of text file? PowerShell
Read a line from a text file, without loading the entire file inmemory PowerShell
Looping Through A Text File To Find Patterns From Another Text Fil PowerShell
Howto: Add lines of text from a specific point in a text file.. VB Script
How do I read a text file and sort text by fixed positions? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46