c#中的新进程tcp转储
Posted
技术标签:
【中文标题】c#中的新进程tcp转储【英文标题】:new process tcp dump in c# 【发布时间】:2012-08-26 10:37:19 【问题描述】:我正在尝试创建一个进程来在我的程序中运行 tcpdump..
这里是代码
Process tcpdump = new Process();
tcpdump.StartInfo.FileName= @"/usr/local/bin/tcpdump";
tcpdump.StartInfo.CreateNoWindow = true;
tcpdump.StartInfo.Arguments = " -i en1 -c 10 -nn tcp and src host " + ip + " and port " + ports[i];
tcpdump.Start();
tcpdump.WaitForExit();
但是我得到了错误并且进程没有正确启动 这是我收到的错误消息
open: invalid option -- i
Usage: open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b <bundle identifier>] [-a <application>] [filenames] [--args arguments]
Help: Open opens files from a shell.
By default, opens each file using the default application for that file.
If the file is in the form of a URL, the file will be opened as a URL.
Options:
-a Opens with the specified application.
-b Opens with the specified application bundle identifier.
-e Opens with TextEdit.
-t Opens with default text editor.
-f Reads input from standard input and opens with TextEdit.
-F --fresh Launches the app fresh, that is, without restoring windows. Saved persistent state is lost, excluding Untitled documents.
-R, --reveal Selects in the Finder instead of opening.
-W, --wait-apps Blocks until the used applications are closed (even if they were already running).
--args All remaining arguments are passed in argv to the application's main() function instead of opened.
-n, --new Open a new instance of the application even if one is already running.
-j, --hide Launches the app hidden.
-g, --background Does not bring the application to the foreground.
-h, --header Searches header file locations for headers matching the given filenames, and opens them.
【问题讨论】:
好吧,我的错,我踢出了我的答案...... 【参考方案1】:问题在于将参数传递给 open 命令的方式。
查看 SO 中 similar question 的解决方案。
【讨论】:
好吧,我试过了,但我得到了另一个错误 System.ComponentModel.Win32Exception has been throwed ApplicationName='/usr/local/bin/tcpdump', CommandLine=' -i en1 -c 10 -nn tcp和 src 主机 192.168.1.4 和端口 80', CurrentDirectory='' 你能在命令行上使用相同的参数运行 tcddump 吗?我建议,您可以尝试使用另一个简单的命令来确保 Process.Start(..) 正常工作。 OOPPS tcpdump 不在那个文件夹中..这就是为什么它不起作用..我的错 :S ...现在一切都很好:) 我很抱歉,但现在一切正常,但当我添加这部分时 + " > tcpdump.txt";将输出重定向到一个文件..我得到tcp语法错误..虽然当我在shell窗口中使用完全相同的命令时我没有得到任何错误......任何解决方案??我认为 char > ..... 有什么帮助吗? 嗯,我相信一定有办法绕过 > char... 另一种方法是使用,tcpDump.StartInfo.RedirectStandardOutput = true; //启动进程后... string tcptxt = tcpDump.StandardOutput.ReadToEnd(); StreamWriter lFile = new StreamWriter("tcpdumpOutput.txt" ); lFile.Write(tcptxt.ToString()); lFile.Close(); tcpDump.WaitForExit();以上是关于c#中的新进程tcp转储的主要内容,如果未能解决你的问题,请参考以下文章