简体   繁体   中英

Mono Linux Shell command

I executed this command on Mono in Linux:

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")

and it should get the SystemUpTime inside the .txt file on Desktop, I've got the answer on the Application Output, but it didn't create the file.

It is possible to get the answer inside a .txt file using this command? Thanks in advance.

I do not think you can get output redirection this way.

However you can do something like this:

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);

Above example needs some reworking

After reading Dana comment I have expanded the above example.

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);

I have tried redirecting a simple ping output to a text file and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM