Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VB 2008 - Ping servers from a .txt file !

Posted on 2010-01-07
11
Medium Priority
?
474 Views
Last Modified: 2013-11-10
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
0
Comment
Question by:mihu332
  • 6
  • 4
11 Comments
 

Author Comment

by:mihu332
ID: 26204838
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
0
 
LVL 3

Expert Comment

by:killaskto
ID: 26205734
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

0
 
LVL 3

Expert Comment

by:killaskto
ID: 26205758
Sorry you should replace this line for the format of your file
server = entrada.ReadLine()
with
server = Split(entrada.ReadLine())(2)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:mihu332
ID: 26208022
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
0
 
LVL 13

Expert Comment

by:game-master
ID: 26208152
0
 

Author Comment

by:mihu332
ID: 26208215
Thanks for the attempt but I know how to ping a server, my requirements are different.
Thanks
0
 

Author Comment

by:mihu332
ID: 26208224
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

0
 
LVL 3

Expert Comment

by:killaskto
ID: 26211207
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

0
 

Author Comment

by:mihu332
ID: 26212260
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.
0
 
LVL 3

Accepted Solution

by:
killaskto earned 2000 total points
ID: 26213642
Replace Button4_Click Sub with this:
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 Line As String
        Dim server As String
        While Not entrada.EndOfStream
            Line = entrada.ReadLine()
            Try
                server = Split(Line)(2)
                Label1.Text = "Scaning: " & server
                Label1.Refresh()
                If (My.Computer.Network.Ping(server)) Then
                    Reachable.WriteLine(Line)
                Else
                    Unreachable.WriteLine(Line)
                End If
            Catch ex As Exception
                MsgBox("Error reading line: " & Line & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical, "Error")
            End Try
        End While
        entrada.Close()
        Reachable.Close()
        Unreachable.Close()
        Label1.Text = "Scan Finished"
    End Sub

Open in new window

0
 

Author Closing Comment

by:mihu332
ID: 31674276
Very able developer.
Thanks for your time
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question