3

Im recently trying to execute the following line ;

string strCmdText;
strCmdText = "netstat -np TCP | find " + quote + number + quote + "";
System.Diagnostics.Process.Start("netstat.exe", strCmdText);

Logs.Write("LISTEN_TO(" + Registry_val1.Text + ")", strCmdText);

now what this has to do is basicly find all TCP ports that contain '80' in them and show them up in my custom-made log system that will make a logbook in my folder called;

LISTEN_TO(80)-{date_time}.txt inside this .txt it should contain the command issued text, however all i get is a time.

i debugged this command as above, and unfortunately all i know is that the CMDtext is set correctly, and that my logging system works correctly, leaving me with no choice that NETSTAT may be closed as soon as the query is launched?

hopefully i provided anough information, as this is my first post.

Regards,

Co

Due to vague description, here's an other-sort same code i tried to do, however still remain getting only a time.

const string quote = "\"";
Process p = new Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.Arguments = "netstat -np TCP | find " + quote + number + quote + "";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();

String output = p.StandardOutput.ReadToEnd();

Logs.Write("LISTEN_TO(" + Registry_val1.Text + ")", output);

basicly, you could see this as; textbox1.text = output; execpt now the output is being putten to a log file.

codea
  • 1,439
  • 1
  • 17
  • 31
Cotemp
  • 55
  • 1
  • 4
  • I cannot make sense of this. You're not even doing anything with netstat other than starting it and then forgetting about it. – mclaassen Jul 23 '14 at 00:34
  • im starting netstat with its command, and the command (output) is being parsed to Logs.Write(log_filename,log_text(being parsed). – Cotemp Jul 23 '14 at 10:23
  • I think what Cotemp means it that he does not get the output of the command. @Cotem, can you confirm this? – codea Jul 23 '14 at 10:46
  • Exactly, i geuss i have to practice to learn how to write things properly, however i have resolved it by using rene's code below, thank you all for this supportive ways. – Cotemp Jul 23 '14 at 11:32

2 Answers2

10

I don't understand why you use netstat in the first place. The .Net framework has a load of classes that give all kind of data, in this case IPGlobalProperties has the method you need.

var ip =  System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();

foreach(var tcp in ip.GetActiveTcpConnections()) // alternative: ip.GetActiveTcpListeners()
{
        if (tcp.LocalEndPoint.Port == number 
         || tcp.RemoteEndPoint.Port == number)
        {
           Logs.Write(
                String.Format(
                   "{0} : {1}", 
                   tcp.LocalEndPoint.Address, 
                   tcp.RemoteEndPoint.Address));
        }
}

The benefit of using the build-in classes is the ease of shaping and selecting whatever you need and most important: you spare yourself and your user an out-of-process call and parsing of output.

rene
  • 41,474
  • 78
  • 114
  • 152
  • 2
    One correction to the accepted answer: instead of looking into ´GetActiveTcpConnections()´ you should be looking at active listeners by using ´GetActiveTcpListeners()´ – Stritof Nov 03 '19 at 01:15
1

You may try this:

strCmdText = "cmd /c \"netstat -np TCP | find " + quote + number + quote + "\"";

if this does not work, try first to use the command in a cmd prompt to make sure it returns data.

cmd /c "netstat -an | find "80"

codea
  • 1,439
  • 1
  • 17
  • 31