需要有关我创建的Vb代码的帮助

时间:2015-11-29 19:14:58

标签: vb.net cmd ping

我正在处理这个项目,我登录到路由器。 ping一些IP,如果我将数据包大小增加到4,则O / p将显示Percent&的结果。与数据包计数100相比,往返更快,这需要时间。所以需要代码,以便Vb实时检查文件,如“筛选窗口”,如果它找到了百分比&在cmd窗口上往返它会将注销/退出/我的东西发送到cmd并显示在MsgBox中。

我需要帮助Vb实时检查txt文件,如果没有找到,关闭文件并再次打开进行重新检查等等,或者可能就像“筛选窗口”..

帮助。

我的原始代码如下。

====================

Private Sub Runcode()
i = Shell("\windows\system32\cmd.exe", vbNormalFocus)

Dim MY_SERVER_IP As String
Dim LocalHost As String
Dim SourceIP As String
Dim DestinationIP As String
Dim Number As String
Dim Size As String
MY_SERVER_IP = "97.1.2.2"


Application.Wait (Now + TimeValue("0:00:02"))


SendKeys "telnet " & MY_SERVER_IP & " 6000 -f D:\Report\PING.txt{ENTER}"
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys " Command to Login the Router"
Application.Wait (Now + TimeValue("0:00:02"))
SendKeys " PING IP: SRN=0, SN=14, SIPADDR=some_Source_IP , DESTIP=Some_Destination_IP, CONTPING=NO, TIMES=4, PKTSIZE=1400; "

Application.Wait (Now + TimeValue("0:00:50"))
' for short packet Number also i have to wait around 50 sec
' here currently Times=4 means it will recieve 4 reply packets which will show the result in 4 sec
' but same if i change it to 50 it wll take around 30-50 sec
Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim Fields As String
Dim buffer As String
buffer = "round-trip"
sFileName = "D:\Report\PING.txt"
''//Does the file exist?
If Len(Dir$(sFileName)) = 0 Then
MsgBox ("File not Created")
End If
iFileNum = FreeFile()
Open sFileName For Input As iFileNum
Do While Not EOF(iFileNum)
Line Input #iFileNum, Fields
If Fields <> buffer Then
Call hint
Else
MsgBox ("didnt Work")
End If

Loop
Close iFileNum
End Sub
Close sFileName


Sub hint()

Dim myFile As String, text As String, textline As String, pkt As String, rndtrip As String

Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.AppActivate "Telnet " & MY_SERVER_IP
objShell.SendKeys "Logout{enter}"
objShell.Application.Wait (Now + TimeValue("0:00:02"))
objShell.SendKeys "Exit{enter}"

myFile = "D:\Report\PING.txt"
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1


pkt = Trim(Mid(text, InStr(text, "Percent") + 0, (InStrRev(text, "packet") + 0) - (InStr(text, "Percent") + 1)))
rndtrip = Trim(Mid(text, InStr(text, "round-trip") + 0, (InStrRev(text, "ms") + 0) - (InStr(text, "round-trip") + 1)))

Call MsgBox("Percent : " & pkt & " Round-Trip : " & rndtrip & ".", vbOKOnly)

End Sub

1 个答案:

答案 0 :(得分:0)

尝试添加此行。只有文件存在时才会触发你的循环。

 If System.IO.File.Exists(myFile )

 Do Until EOF(1)
 Line Input #1, textline
 text = text & textline
 Loop
 Close #1
 else 

 ' if the file is not there rerun the method. 
 Runcode()

 end if