Link to home
Start Free TrialLog in
Avatar of mihu332
mihu332

asked on

VB 2008 - Ping servers from a .txt file !

Hi,
I am looking to add the following function to my existing software.
When button1 is pressed, the software should ping all servers that are in servers.txt file.
The software should make a list of all reachable servers and a list of unreachable servers. ( reachable.txt / unreachable.txt )
We are talking about over 2000 servers in that list, so pinging all at once would not be a smart idea.
The best way would be to ping 10 servers at once. Preferably a single ping for each server.
If someone has any idea, I gladly accept it.
Thanks a lot
Avatar of mihu332
mihu332

ASKER

Update :

The servers.txt is in the following format :

Customer Hostname IP

example :

COLGATE AHSBE21 172.29.33.22
COLGATE AHSBE23 172.29.33.25
COLGATE AHSBE24 172.29.33.21
COLGATE AHSBE25 172.29.33.26

The software should ping the servers by the IP address, not hostname
try This
Dim entrada As New IO.StreamReader("Source.txt")
        Dim Reachable As New IO.StreamWriter("Reachable.txt")
        Dim Unreachable As New IO.StreamWriter("Unreachable.txt")
        Dim server As String
        While Not entrada.EndOfStream
            server = entrada.ReadLine()
            If (My.Computer.Network.Ping(server)) Then
                Reachable.WriteLine(server & "OK")
            Else
                Unreachable.WriteLine(server & "Bad")
            End If
        End While
        entrada.Close()
        Reachable.Close()
        Unreachable.Close()

Open in new window

Sorry you should replace this line for the format of your file
server = entrada.ReadLine()
with
server = Split(entrada.ReadLine())(2)
Avatar of mihu332

ASKER

Hi
I get some trouble running the app as it is because the form is checking for existence of the files before loading.
I got a solution for this, I added to the form the following controls:
OpenFileDialog1 ( source file )
OpenFileDialog1 ( good servers file )
OpenFileDialog1 ( bad servers file )
ProgressBar1 ( to show the progress trough the list of servers )

The OpenFileDialog(s) will be called via buttons ( OpenFileDialog1.ShowDialog() )
can you please remake the code based on this new information ?
Thanks a lot
Avatar of mihu332

ASKER

Thanks for the attempt but I know how to ping a server, my requirements are different.
Thanks
Avatar of mihu332

ASKER

I also get this error regarding  server = Split(entrada.ReadLine())(2) :
Index was outside the bounds of the array.

My code is like this now :
Public Class Form1
    Dim entrada As New IO.StreamReader("c:\servers.txt")
    Dim Reachable As New IO.StreamWriter("c:\goodservers.txt")
    Dim Unreachable As New IO.StreamWriter("c:\badservers.txt")
    Dim server As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        While Not entrada.EndOfStream
            server = Split(entrada.ReadLine())(2)
            If (My.Computer.Network.Ping(server)) Then
                Reachable.WriteLine(server & "OK")
            Else
                Unreachable.WriteLine(server & "Bad")
            End If
        End While
        entrada.Close()
        Reachable.Close()
        Unreachable.Close()
    End Sub

 End Class

Open in new window

Your source file must be separated by spaces and must be in three columns, just like the example that you put. If your file have less columns you will get the index-outside error.
You can also use a openfiledialog and savefiledialog in order to set the text files. See code below
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSource.Click
        OpenFileDialog1.ShowDialog()
        TextBox1.Text = OpenFileDialog1.FileName
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBad.Click
        SaveFileDialog1.ShowDialog()
        TextBox2.Text = SaveFileDialog1.FileName
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        SaveFileDialog1.ShowDialog()
        TextBox3.Text = SaveFileDialog1.FileName
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnScan.Click
        Dim entrada As New IO.StreamReader(TextBox1.Text)
        Dim Reachable As New IO.StreamWriter(TextBox3.Text)
        Dim Unreachable As New IO.StreamWriter(TextBox2.Text)
        Dim server As String
        While Not entrada.EndOfStream
            server = Split(entrada.ReadLine())(2)
            Label1.Text = "Scaning: " & server
            Label1.Refresh()
            If (My.Computer.Network.Ping(server)) Then
                Reachable.WriteLine(server & " - OK")
            Else
                Unreachable.WriteLine(server & " - BAD")
            End If
        End While
        entrada.Close()
        Reachable.Close()
        Unreachable.Close()
        Label1.Text = "Scan Finished"
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label1.Text = ""
        OpenFileDialog1.Filter = "All files (*.*)|*.*|Text files (*.txt)|*.txt"
        SaveFileDialog1.Filter = "All files (*.*)|*.*|Text files (*.txt)|*.txt"
        SaveFileDialog1.DefaultExt = "txt"
    End Sub
End Class

Open in new window

Avatar of mihu332

ASKER

Hi,

The software works great now !
The only issue remaining are the output files.
You marked them with 'Unreachable.WriteLine(server & " - BAD")'
I need them to be left in the original layout 'Customer Hostname IP' because I will import the results in other scripts after that.
Also error handling for the Index was outside the bounds of the array might be useful something like msgbox "file format incorrect".... but is not really necesary.
You earned your points anyway.
Thanks a lot for your help, you are very capable.
ASKER CERTIFIED SOLUTION
Avatar of killaskto
killaskto
Flag of Mexico image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mihu332

ASKER

Very able developer.
Thanks for your time