SNMPWalk Error in C# Process init - c#

I'm creating an application at work that uses SNMPWalk to get details on specific IP addresses. Right now it's meant to use Nmap to find all printers in a specified network and then it uses SNMPWalk to get details on each printer.
The problem is that SNMPWalk is giving me the error:
"No log handling enabled - using stderr logging
-v2c: (Sub-id not found: (top) -> -v2c)"
My code is here:
Process p = new Process();
p.StartInfo.FileName = snmploc + "\\snmpwalk.exe";
p.StartInfo.ErrorDialog = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = false;
p.StartInfo.WorkingDirectory = snmploc;
p.StartInfo.Arguments = "–mALL -v2c –cpublic " + printer.IP();
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
Console.WriteLine("Snmpwalk has been fired");
Console.WriteLine("Waiting for Snmp to terminate...");
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
Console.WriteLine(p.ExitCode);
printer is an object of the class Printer. It is used to contain printer details such as ip address, name, model, serial number, and open ports. snmploc is obtained from command line arguments.
The main reason that this is so complicated is that when I try using the exact same command in a command prompt it works perfectly.

Related

How to get ips that a certain process is connecting to in c#

So I am writing a C# program to manage a web server on Windows. I have it so the web server software starts with the users typed in configuration and starts the web server successfully. However, I am struggling with getting the IP addresses that the web server software is connecting to. I am attempting to show incoming and outgoing connection IPs of a certain exe but am only able to show all incoming outgoing connections of the whole computer not just one specific process exe like I am trying to. Here is the code where I am attempting to do this:
private void ipmonitor_Click(object sender, EventArgs e)
{
IPAddress[] addrList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
string IP = addrList[0].ToString();
richTextBox2.Text = IP;
}
}
}
Is there a way to do this in C# code? Thanks.
One option is to run netstat, and parse the result. see a great example here for inspiration.
using (Process p = new Process())
{
ProcessStartInfo ps = new ProcessStartInfo();
ps.Arguments = "-a -n -o";
ps.FileName = "netstat.exe";
ps.UseShellExecute = false;
ps.WindowStyle = ProcessWindowStyle.Hidden;
ps.RedirectStandardInput = true;
ps.RedirectStandardOutput = true;
ps.RedirectStandardError = true;
p.StartInfo = ps;
p.Start();
StreamReader stdOutput = p.StandardOutput;
StreamReader stdError = p.StandardError;
string content = stdOutput.ReadToEnd() + stdError.ReadToEnd();
string exitStatus = p.ExitCode.ToString();
// inpect and parse content and exitStatus for errors
}

Automating interactive command line process from C#

I have an *.exe that has an interactive text command interface which I want to automate from C#.
The (1) is the C # result, and the (2) is the result of directly using *.exe.
(latter miss "connect ok!" & "download successfully!")
(1)
connect ok!
cable detect ok!
download successfully!
(2)
cable detect ok!
I don’t know how to make the result of C # the same as the (2), below is my code:
Process p = new Process();
p.StartInfo.FileName = ex.exe;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("prj_project open " + "...../bin/store//*****Water.ldf");
p.StandardInput.WriteLine("pgr_project open " + "...../bin/store/imp11/imp11.xcf");
p.StandardInput.WriteLine("pgr_program run");
p.StandardInput.WriteLine("exit");
p.WaitForExit();
var str = p.StandardOutput.ReadToEnd();
var str2 = p.StandardError.ReadToEnd();
Console.WriteLine(str);
Console.WriteLine(str2);
p.Close();
For some reason, I want to mask out some places, hope not to cause your confusion.
If you can help me solve this problem, thank you very much.
It drives me crazy...It took me almost fifteen hours to solve...

Why does Kaspersky anti-virus prevent my program from capturing the output of its child process?

I have a C# program that launches a child process and captures its output in a string. This works on most Windows machines (Windows 7 and newer), but when Kaspersky anti-virus is present, Process.StandardOutput.ReadToEnd() returns null. There is no error code or exception. The child process is a trusted console application. The process takes 5 or 6 seconds to run.
The code for launching the child process is as follows:
ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
psi.FileName = "icao.exe";
psi.Arguments = im_path + "image.jpg";
Process p = new Process();
p.StartInfo = psi;
p.Start();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
MessageBox.Show(error);
p.WaitForExit();
int exitCode = p.ExitCode;
MessageBox.Show(exitCode+"");
Why does output end up being null when Kaspersky is present?
My guess is that Kaspersky's heuristics are seeing that your program wants to execute another exe. Because nothing is telling Kaspersky that this is ok, it flags your program as possible malware, because it wants to interface with other programs that are developed by other companies. If you are able to I would try white listing your program with Kaspersky and see if that solves your issue.

Capturing Standard Error

I'm following the guidance of other SO articles on this topic, but I have been unable to find what I need. I have the following codeblock which launches - in this case - a known corrupted .msi (in order to test the retrieval of the standard error message):
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd.exe", "/c " + thirdPartyApp.Item2.InstallString);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
writeEvent(EventLogEntryType.Information, "Attempting to launch upgrade: " + thirdPartyApp.Item2.InstallString, "Third Party Update");
proc.Start();
string stderror = proc.StandardError.ReadToEnd();
proc.WaitForExit();
When manually launched, this .msi throws error code 1620 with message "This installation package could not be opened. Contact the application vendor to verify that this is a valid Windows Installer package."
While debugging, if I look at proc.ExitCode its value is 1620 - so it's capturing that correctly. I added the line string stderror = proc.StandardError.ReadToEnd(); to attempt to capture the error text, but after the process executes and errors out stderror has a value of "".
How can I grab the actual error text into a variable?

Why is auditpol not applying the local policy settings from C#?

I'm setting local auditing policies from a C# .NET program that reads settings from a file then uses Process.Start() with 'cmd' to execute the commands. This way has worked in the past for everything that I've needed it to do (including this exact situation), but recently it's just started to mysteriously fail to set the policies.
Here's the code: (command is of the form "auditpol /set /subcategory:"blah" /success:enable")
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
proc.WaitForExit();
string result = proc.StandardOutput.ReadToEnd();
string error = proc.StandardError.ReadToEnd();
In debug in VS2013 it's applying the policies just fine and even on the same computer in the full on .exe it's applying just fine, but when it gets transferred to another computer it will not set the policies from the auditpol command. Anyone have any ideas what could be happening?

Categories

Resources