[2005] Using the system console to run a command
Just a simple question:
I'm wanting to run a traceroute in my program. I have methods in place to interpret the output, but I couldn't get to this bit before because I was in university accommodation which blocks traceroutes.
Anyhow, how can I do one within my program? The plan is to do it without showing a command prompt, then pass the output to my parsing method. I also can't afford to pay for some third party implementation of it - someone suggested before that I can use the system console or words to a similar effect, I just don't know how.
Thanks.
Re: [2005] Using the system console to run a command
Yes, you can use the cmd and pass the dos commands as arguments with an output to a textfile. Or even with a bit more work you can redirect the output to eliminate the extra file need. Gigemboy has a codebank thread on redirecting outputs.
Re: [2005] Using the system console to run a command
Thanks for the response.
What I was actually after was the way to actually do this within the code - what I have to type to, say, execute "tracert 140.191.222.5" for example. That's just the bit I'm stuck on, since I've never actually done it before.
I don't think I need to redirect the output since I plan to be storing it in text files anyway.
Re: [2005] Using the system console to run a command
It should look something like this
Code:
Dim proc As New Process()
With proc.StartInfo
.FileName = "tracert.exe"
.Arguments = "140.191.222.5"
.CreateNoWindow = True
.UseShellExecute = False
.RedirectStandardOutput = True
End With
proc.Start()
proc.WaitForExit()
Dim output As String = proc.StandardOutput.ReadToEnd()
Dim writer As New IO.StreamWriter("your output file path here")
writer.Write(output)
writer.Close()
proc.Close()
Re: [2005] Using the system console to run a command
Thanks very much, I'll give that a go.
I feel a bit bad asking such rudimentary questions, I have bought 2 books but I can't seem to find anything about that specific thing in either :rolleyes:
Re: [2005] Using the system console to run a command
Yes stanav but with one more argument for the output file as it makes it easier.
.Arguments = "140.191.222.5 > c:\MyFile.txt"
Re: [2005] Using the system console to run a command
Okay, this is what I have:
Code:
Private Function traceRoute(ByVal ip As String, ByVal outputFile As String)
Dim proc As New Process()
With proc.StartInfo
.FileName = "tracert.exe"
.Arguments = ip
.CreateNoWindow = True
.UseShellExecute = False
.RedirectStandardOutput = True
End With
proc.Start()
proc.WaitForExit()
Dim output As String = proc.StandardOutput.ReadToEnd()
Dim writer As New IO.StreamWriter(outputFile)
writer.Write(output)
writer.Close()
proc.Close()
End Function
This is what I use in the button that calls it:
Code:
traceRoute("72.14.207.99", "C:\TESTTracert.txt")
What happens is I see tracert pop up in process explorer, so it is indeed starting, but it seems like nothing happens after that - the program stops responding. Traceroute doesn't complete and the file I passed over is not created.
Is this a problem with my using blackslash in the file path? I can't remember if there were issues in this language with doing that - I know there are in some others. I have tried using \\.
Re: [2005] Using the system console to run a command
You need to use the argument as I posted because the ">" character tells dos to output the results to the file specified.
Re: [2005] Using the system console to run a command
Ahh of course - I was typing that in when I was doing them manually but I suffered a 'brain disengaged' error when trying to implement it.
Right. This time around it starts the tracert process but doesn't create any file. It doesn't hang this time.
This is my current function:
Code:
Private Function traceRoute(ByVal ip As String, ByVal outputFile As String)
Dim proc As New Process()
With proc.StartInfo
.FileName = "tracert.exe"
.Arguments = ip & " > " & outputFile
.CreateNoWindow = True
.UseShellExecute = False
'.RedirectStandardOutput = True
End With
proc.Start()
proc.WaitForExit()
'Dim output As String = proc.StandardOutput.ReadToEnd()
'Dim writer As New IO.StreamWriter(outputFile)
'writer.Write(output)
'writer.Close()
proc.Close()
End Function
I didn't think I could use your exact argument text since I need to be able to specify the output file and IP in variables, but I assume this is meant to have the same effect?
Re: [2005] Using the system console to run a command
Re: [2005] Using the system console to run a command
What is the values of your argument if you output them to the console window?
Re: [2005] Using the system console to run a command
Sorry about the delay, the topic was temporarily buried so I thought it had died.
I have tried sending the argument as .Arguments = "72.14.207.99 > c:\test.txt" and it doesn't work. However, when I enter that as the argument manually in a command prompt outside of this program it works fine. Sending just the IP and no filepath works fine within the program too.
Re: [2005] Using the system console to run a command
I figured out the argument issue in this thread:
http://vbforums.com/showthread.php?p...38#post2828238
Setting the working directory is the key so you dont need to pass the entire path of the output file. :)
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oPSI As New System.Diagnostics.ProcessStartInfo
Dim oProcess As System.Diagnostics.Process
With oPSI
.FileName = "C:\Windows\System32\cmd.exe"
.CreateNoWindow = True
.WindowStyle = ProcessWindowStyle.Hidden
.WorkingDirectory = "C:\Users\VB-Guru\"
.UseShellExecute = True
.Arguments = "/C Ping google.com > PingResults.txt"
End With
oProcess = System.Diagnostics.Process.Start(oPSI)
oProcess.WaitForExit()
System.Diagnostics.Process.Start(oPSI.WorkingDirectory & "PingResults.txt")
End Sub
Re: [2005] Using the system console to run a command
I can't say I'm exactly sure what you mean, but I had a look at the linked thread and now the inside of my 'with' bit looks like this:
Code:
.FileName = "tracert.exe"
.WorkingDirectory = "C:\"
.Arguments = "72.14.207.99 > test.txt"
.CreateNoWindow = True
.UseShellExecute = False
Which seems to yield the same problems.
Re: [2005] Using the system console to run a command
It works just fine for me using your provided ip address, tracert.exe and the code I posted.
Quote:
Tracing route to eh-in-f99.google.com [72.14.207.99]
over a maximum of 30 hops:
1 1 ms <1 ms <1 ms 192.168.1.1
2 25 ms 24 ms 24 ms netblock-66-159-208-1.dslextreme.com [66.159.208.1]
3 25 ms 24 ms 25 ms LAX1.CR1.Gig9-0-3.dslextreme.com [66.51.203.33]
4 24 ms 25 ms 24 ms ge-5-1-115.ipcolo1.LosAngeles1.Level3.net [63.209.70.133]
5 35 ms 35 ms 35 ms ae-31-53.ebr1.LosAngeles1.Level3.net [4.68.102.94]
6 38 ms 36 ms 35 ms ae-2.ebr1.SanJose1.Level3.net [4.69.132.9]
7 36 ms 35 ms 36 ms ae-1-100.ebr2.SanJose1.Level3.net [4.69.132.2]
8 67 ms 73 ms 70 ms ae-3.ebr1.Denver1.Level3.net [4.69.132.58]
9 70 ms 72 ms 71 ms ae-1-100.ebr2.Denver1.Level3.net [4.69.132.38]
10 90 ms * * ae-3.ebr1.Chicago1.Level3.net [4.69.132.62]
11 116 ms 110 ms 111 ms ae-2.ebr2.NewYork1.Level3.net [4.69.132.66]
12 101 ms 101 ms 100 ms ae-21-54.car1.NewYork1.Level3.net [4.68.97.116]
13 105 ms 107 ms 106 ms GOOGLE-INC.car1.NewYork1.Level3.net [4.71.172.86]
14 106 ms 106 ms 106 ms 72.14.236.213
15 116 ms 116 ms 118 ms 72.14.233.115
16 120 ms 116 ms 118 ms 66.249.94.90
17 119 ms 118 ms 120 ms 66.249.94.50
18 117 ms 116 ms 119 ms eh-in-f99.google.com [72.14.207.99]
Trace complete.
Re: [2005] Using the system console to run a command
Exactly as I tested it. Did you also add the cmd.exe as shown in my previous code post too?
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oPSI As New System.Diagnostics.ProcessStartInfo
Dim oProcess As System.Diagnostics.Process
With oPSI
.FileName = "C:\Windows\System32\cmd.exe"
.CreateNoWindow = True
.WindowStyle = ProcessWindowStyle.Hidden
.WorkingDirectory = "C:\Users\VB-Guru\"
.UseShellExecute = True
.Arguments = "/C tracert.exe 72.14.207.99 > PingResults.txt"
End With
oProcess = System.Diagnostics.Process.Start(oPSI)
oProcess.WaitForExit()
System.Diagnostics.Process.Start(oPSI.WorkingDirectory & "PingResults.txt")
End Sub
Re: [2005] Using the system console to run a command
I hadn't added that bit. I have now, and it seems to be working perfectly.
Thanks very much for your assistance :bigyello:
Re: [2005] Using the system console to run a command
No prob. Glad to help.
Ps, dont forget to Resolve your thread so other members will know its solved. ;)