Results 1 to 9 of 9

Thread: How to ping a contents of text file and write resulting IP to different text file?

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Location
    Michigan
    Posts
    3

    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.

  2. #2
    Member
    Join Date
    Dec 2006
    Location
    Derby, UK
    Posts
    58

    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.

  3. #3
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    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

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  4. #4
    Member
    Join Date
    Dec 2006
    Location
    Derby, UK
    Posts
    58

    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.)

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Location
    Michigan
    Posts
    3

    Re: How to ping a contents of text file and write resulting IP to different text file

    Quote Originally Posted by BlindSniper View Post
    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.

  6. #6
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    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

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2011
    Location
    Michigan
    Posts
    3

    Re: How to ping a contents of text file and write resulting IP to different text file

    Quote Originally Posted by dbasnett View Post
    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?

  9. #9
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width