-
Dec 3rd, 2011, 05:43 AM
#1
Thread Starter
New Member
How to ping a contents of text file and write resulting IP to different text file?
I am fairly new to Vb.net, I have spent many hours researching, reading tutorials, and watching tutorials, but have hit a brick wall. I can honestly say what I am trying to do is over my head, and I don't even know how to begin this. But, I hope someone can help me cause I need to understand.
I am trying to ping a text file("C:/Domains.txt") which is a list of domains, then have the resulting IP address written to a different text file ("C:/IP_Addresses.txt"). And this action will be done with a Button_Click.
-
Dec 3rd, 2011, 08:57 AM
#2
Member
Re: How to ping a contents of text file and write resulting IP to different text file
Ping a File? Are you sure you don't mean read the list of Domains in the text file and Ping each one to return its IP Address? If so, have a look here.
-
Dec 3rd, 2011, 09:06 AM
#3
Re: How to ping a contents of text file and write resulting IP to different text file
You don't need to ping something to get it's IP adress.
Code:
Function GetIp(ByVal Domain As String) As IPAddress
Return Dns.GetHostEntry(Domain).AddressList(0)
End Function
-
Dec 3rd, 2011, 09:22 AM
#4
Member
Re: How to ping a contents of text file and write resulting IP to different text file
A much simpler method, certainly - but doesn't it use Ping (or TRACERT) anyway?
I suspect, from reading the original post, that this is an assignment that requires the use of Ping.
(Ah, I see you have the machine that goes Ping! - Monty Python, pure class.)
-
Dec 3rd, 2011, 09:29 AM
#5
Thread Starter
New Member
Re: How to ping a contents of text file and write resulting IP to different text file
Originally Posted by BlindSniper
You don't need to ping something to get it's IP adress.
Code:
Function GetIp(ByVal Domain As String) As IPAddress
Return Dns.GetHostEntry(Domain).AddressList(0)
End Function
I appreciate all your guys help, but I know how to read a text file, write to a text file, get an IP address of one domain. But I don't know is how to mesh all these together. I have two text files, one is a list of domains, and the other is for the resulting IP addresses. With a button click I need to read the domain list and get their IP's for the domains with the results to be written to a text file all in one shot as it would be done with one button.
-
Dec 3rd, 2011, 09:50 AM
#6
Re: How to ping a contents of text file and write resulting IP to different text file
Code:
Dim Ips As New List(Of String)
For Each i As String In IO.File.ReadAllLines("The domain file path")
Ips.Add(GetIp(i).ToString)
Next
IO.File.WriteAllLines("The IP file path", Ips.ToArray)
This code assumes that there is a single domain on each line
-
Dec 3rd, 2011, 10:36 AM
#7
Re: How to ping a contents of text file and write resulting IP to different text file
As blindsniper pointed out you can use DNS to get the address
Code:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim testData() As String = New String() {"yahoo.com", "microsoft.com", "apple.com", "vbforums.com", "google.com"}
Dim dnsIPlist As New List(Of DNSDomainNames)
For Each n As String In testData
Try
Dim ips() As System.Net.IPAddress = System.Net.Dns.GetHostAddresses(n)
Dim foo As New DNSDomainNames
foo.theName = n
For Each addr As System.Net.IPAddress In ips
foo.theIP.Add(addr)
Next
dnsIPlist.Add(foo)
Catch ex As Exception
Debug.WriteLine(ex.Message & " " & n)
End Try
Next
Debug.WriteLine("")
For Each d As DNSDomainNames In dnsIPlist
Debug.Write(d.theName)
For Each i As System.Net.IPAddress In d.theIP
Debug.Write(" " & i.ToString)
Next
Debug.WriteLine("")
Next
End Sub
Class DNSDomainNames
Property theIP As New List(Of System.Net.IPAddress)
Property theName As String
End Class
End Class
-
Dec 3rd, 2011, 10:24 PM
#8
Thread Starter
New Member
Re: How to ping a contents of text file and write resulting IP to different text file
Originally Posted by dbasnett
As blindsniper pointed out you can use DNS to get the address
Code:
Public Class Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim testData() As String = New String() {"yahoo.com", "microsoft.com", "apple.com", "vbforums.com", "google.com"}
Dim dnsIPlist As New List(Of DNSDomainNames)
For Each n As String In testData
Try
Dim ips() As System.Net.IPAddress = System.Net.Dns.GetHostAddresses(n)
Dim foo As New DNSDomainNames
foo.theName = n
For Each addr As System.Net.IPAddress In ips
foo.theIP.Add(addr)
Next
dnsIPlist.Add(foo)
Catch ex As Exception
Debug.WriteLine(ex.Message & " " & n)
End Try
Next
Debug.WriteLine("")
For Each d As DNSDomainNames In dnsIPlist
Debug.Write(d.theName)
For Each i As System.Net.IPAddress In d.theIP
Debug.Write(" " & i.ToString)
Next
Debug.WriteLine("")
Next
End Sub
Class DNSDomainNames
Property theIP As New List(Of System.Net.IPAddress)
Property theName As String
End Class
End Class
@dbasnett From looking at your code, if I'm understanding it right it looks to be close to what I need. Couple questions though.
One, where does it reference the two text files, or where would it?
Two, I see you have referenced {"yahoo.com", "microsoft.com", "apple.com", "vbforums.com", "google.com"}, does it look for or use only these domains or is this just setting a example of what to look for in the text file.
Three, and that small Class at the end, Class DNSDomainNames, do I use it there as is or do I have to add a class to the project like Class1.vb?
-
Dec 4th, 2011, 07:21 AM
#9
Re: How to ping a contents of text file and write resulting IP to different text file
One, where does it reference the two text files, or where would it? I thought you said that you knew how to read the text file, so I just used the array as an example of what might be in the text file.
Two, I see you have referenced {"yahoo.com", "microsoft.com", "apple.com", "vbforums.com", "google.com"}, does it look for or use only these domains or is this just setting a example of what to look for in the text file. Just examples of what might be in the text file.
Three, and that small Class at the end, Class DNSDomainNames, do I use it there as is or do I have to add a class to the project like Class1.vb? There or in a project class.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|