472,101 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,101 software developers and data experts.

How to ping System IP using VB?

Hi,
I want to ping certain systems IP. Everytime I'm using DOS or Start --> Run mode for pinging different PCs. But its becoming very difficult.
So I planned to develop a VB application to make this job easier.
Aplication contains;
1. Option buttons named as PC names
2. A Command Button named as START & another STOP
Whenever a user selects the PC name & clicks on START button, pinging should be start & pinging status should be shown in Text box.


ANybody idea..? Please help me...

Thanx in Advans
Prem
May 22 '09 #1
2 12058
vacvac
6
I'm not so sure about the code, but you can try it
Expand|Select|Wrap|Line Numbers
  1. Public Function ComputerIsOnline(ByVal strComputerName As String) As Boolean
  2.     On Error GoTo ErrorHandler
  3.  
  4.     Dim strResult   As String
  5.     Dim ShellX      As String
  6.     Dim FileNum     As Integer
  7.     Dim lPid        As Long
  8.     Dim lHnd        As Long
  9.     Dim lRet        As Long
  10.  
  11.     'DoEvents
  12.     ShellX = Shell("command.com /c ping -n 2 " & strComputerName & " > log.txt", vbHide)
  13.     lPid = ShellX
  14.     If lPid <> 0 Then
  15.         lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
  16.         If lHnd <> 0 Then
  17.             lRet = WaitForSingleObject(lHnd, INFINITE)
  18.             CloseHandle (lHnd)
  19.         End If
  20.         FileNum = FreeFile
  21.         Open "log.txt" For Input As #FileNum
  22.         strResult = Input(LOF(1), 1)
  23.         Close #FileNum
  24.         ComputerIsOnline = (InStr(strResult, "Lost = 0") > 0)
  25.     End If
  26.     Exit Function
  27. ErrorHandler:
  28.     ComputerIsOnline = False
  29.     Exit Function
  30. End Function
  31.  
Jun 1 '09 #2
jg007
283 100+
it is for vb.net and hopefully I have not made any mistakes converting it but -

on the form I have a textbox for a PC ID or an IP address , a button to start it off and a richtextbox to show the result and a timer called Timer1

Expand|Select|Wrap|Line Numbers
  1.  
  2. Imports System.Net.NetworkInformation
  3.  
  4. Public Class Form1
  5.  
  6.     Dim pingwsname As String = ""
  7.  
  8.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  9.  
  10.     End Sub
  11.  
  12.  
  13.     Private Function GetStatusString(ByVal status As IPStatus) As String
  14.  
  15.         'Convert IPstatus to a string that can be reported to the user
  16.  
  17.  
  18.         Select Case status
  19.  
  20.             Case IPStatus.Success
  21.                 Return "Success."
  22.             Case IPStatus.DestinationHostUnreachable
  23.                 Return "Destination host unreachable."
  24.             Case IPStatus.DestinationNetworkUnreachable
  25.                 Return "Destination network unreachable."
  26.             Case IPStatus.DestinationPortUnreachable
  27.                 Return "Destination port unreachable."
  28.             Case IPStatus.DestinationProtocolUnreachable
  29.                 Return "Destination protocol unreachable."
  30.             Case IPStatus.PacketTooBig
  31.                 Return "Packet too big."
  32.             Case IPStatus.TtlExpired
  33.                 Return "TTL expired."
  34.             Case IPStatus.ParameterProblem
  35.                 Return "Parameter problem."
  36.             Case IPStatus.SourceQuench
  37.                 Return "Source quench."
  38.             Case IPStatus.TimedOut
  39.                 Return "Request timed out."
  40.         End Select
  41.         Return "Ping failed."
  42.     End Function
  43.  
  44.     Private Sub dothatfunkyping(ByVal WSname As String)
  45.  
  46.         pingwsname = WSname
  47.         ' Set the window title
  48.         Me.Text = "Ping results for " + WSname
  49.         'Link the event handling procedure to the timer
  50.  
  51.         '000ms interval between pings
  52.         Timer1.Interval = 1000
  53.         Timer1.Start()
  54.     End Sub
  55.  
  56.  
  57.     Private Sub PingClientForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
  58.  
  59.         'Stop the timer to prevent errors caused by timer event firing after window closure
  60.         Timer1.Stop()
  61.  
  62.     End Sub
  63.  
  64.  
  65.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
  66.  
  67.         Try
  68.  
  69.             'Stop the timer before sending the ping, timer will be restarted after the reply is received,
  70.             'stops it from attempting to send multiple pings at once
  71.  
  72.             Timer1.Stop()
  73.  
  74.             Dim pingSender = New Ping()
  75.  
  76.             'Send the packet with a 1500ms timeout
  77.  
  78.             Dim reply As PingReply = pingSender.Send(pingwsname, 1500)
  79.  
  80.             'Output the reply in the appropriate colour depending on RoundtripTime
  81.  
  82.             If reply.Status = IPStatus.Success Then
  83.  
  84.                 If (reply.RoundtripTime <= 200) Then
  85.  
  86.                     RichTextBox1.SelectionColor = Color.Lime
  87.  
  88.                 ElseIf (reply.RoundtripTime > 200 & reply.RoundtripTime <= 600) Then
  89.  
  90.                     RichTextBox1.SelectionColor = Color.Yellow
  91.  
  92.                 Else
  93.  
  94.                     RichTextBox1.SelectionFont = New Font(Me.Font, FontStyle.Bold)
  95.                     RichTextBox1.SelectionColor = Color.Red
  96.  
  97.                 End If
  98.  
  99.                 RichTextBox1.AppendText("Reply from " + reply.Address.ToString() + ": bytes= " + reply.Buffer.Length.ToString() + " Time= " + reply.RoundtripTime.ToString() + "ms TTL= " + reply.Options.Ttl.ToString() + vbCrLf)
  100.                 RichTextBox1.ScrollToCaret()
  101.  
  102.                 'Adjusts the time to the next ping depending on RoundtripTime
  103.                 If (reply.RoundtripTime > 999) Then
  104.  
  105.                     Timer1.Interval = 1
  106.  
  107.                 Else
  108.  
  109.                     Timer1.Interval = 1000 - reply.RoundtripTime
  110.  
  111.                 End If
  112.  
  113.             Else
  114.  
  115.                 ' Report status if not success
  116.                 RichTextBox1.SelectionFont = New Font(Me.Font, FontStyle.Bold)
  117.                 RichTextBox1.SelectionColor = Color.Red
  118.                 RichTextBox1.AppendText(GetStatusString(reply.Status) + vbCrLf)
  119.                 RichTextBox1.ScrollToCaret()
  120.                 Timer1.Start()
  121.             End If
  122.  
  123.         Catch ex As ApplicationException
  124.  
  125.             'Catch cases of unknown host etc.
  126.             RichTextBox1.SelectionFont = New Font(Me.Font, FontStyle.Bold)
  127.             RichTextBox1.SelectionColor = Color.Red
  128.             RichTextBox1.AppendText(ex.InnerException.Message + " " + pingwsname + vbCrLf)
  129.             'No need for repeat pings so stop the timer
  130.             Timer1.Stop()
  131.         End Try
  132.  
  133.         'Restart the timer to schedule next ping
  134.         Timer1.Start()
  135.  
  136.     End Sub
  137.  
  138.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  139.         dothatfunkyping(TextBox1.Text)
  140.     End Sub
  141. End Class
  142.  
  143.  
Jun 22 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

17 posts views Thread by wana | last post: by
21 posts views Thread by Neel | last post: by
2 posts views Thread by Ryan | last post: by
1 post views Thread by Krish | last post: by
4 posts views Thread by =?Utf-8?B?QWxleCBLLg==?= | last post: by
reply views Thread by SyGC | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.