c# - 单声道 Linux Shell 命令

标签 c# linux shell mono snmp

我在 Linux 中的 Mono 上执行了这个命令:

System.Diagnostics.Process.Start("snmpwalk", " -v 1 -c public " + Ip + " 1.3.6.1.2.1.25.1.1.0 > /home/dana/Desktop/test.txt")

它应该在桌面上的 .txt 文件中获取 SystemUpTime,我在应用程序输出中得到了答案,但它没有创建文件。

可以使用此命令在 .txt 文件中获取答案吗?提前致谢。

最佳答案

我不认为你可以通过这种方式获得输出重定向。

但是你可以这样做:

System.Diagnostics.Process proc = new System.Diagnostics.Process();

proc.Start("snmpwalk", " -v 1 -c public " + Ip + " 1.3.6.1.2.1.25.1.1.0")

//Then use the StandardOutput property of the process to read its output
string procOutput = proc.StandardOutput.ReadToEnd();

//Write the output to your file
System.IO.File.WriteAllText("/home/dana/Desktop/test.txt", output);

上面的例子需要一些修改

在阅读 Dana 评论后,我扩展了上面的示例。

System.Diagnostics.Process proc = new System.Diagnostics.Process();

//this should tell not to start a console
proc.StartInfo.UseShellExecute = False

//this should tell that you are redirecting process output
proc.StartInfo.RedirectStandardOutput = True

proc.StartInfo.FileName = "snmpwalk"
proc.StartInfo.Arguments = "-v 1 -c public " + Ip + " 1.3.6.1.2.1.25.1.1.0"

proc.Start()

//Then use the StandardOutput property of the process to read its output
string procOutput = proc.StandardOutput.ReadToEnd();

//Write the output to your file
System.IO.File.WriteAllText("/home/dana/Desktop/test.txt", output);

我试过将一个简单的 ping 输出重定向到一个文本文件并且成功了。

关于c# - 单声道 Linux Shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31135266/

相关文章:

java - jni4net - System.Runtime.Remoting.RemotingException

linux - 在 Windows 上托管 docker 虚拟机时,是否可以配置 docker-nvidia 以与 tensorflow-serving 一起使用?

linux -/proc/pid/smaps (linux) 中的 "rwxp"部分是什么

linux - 在 Shell 脚本中格式化 Diff 输出

c# - 从正则表达式中提取数据库提供程序的分隔符

c# - 使用 C#.NET 将 MS Word 2007 中的粗体文本替换为 <b>文本</b>

c# - 清除客户端所有文本框

linux - 创建一个包含目录中文件(相对路径)列表的文本文件?

linux - shell脚本终止程序但不终止脚本本身

linux - 如何根据上一个命令比较得到下一个输入命令[bash-script]