0

I've been learning a few things about ASP for the past few days. I wanted to convert this PHP code line into ASP but I'm kinda stuck with it:

$online = exec('netstat -a -n |find "5816" |find "ESTABLISHED" /c') +1;

I have tried creating a variable to store the data but unable to figure out how to check the port 5816 and count the amount of connections. Help is appreciated!

It should be basically a command to be run in cmd to check the port and no. of connections established by it !

StepUp
  • 30,747
  • 12
  • 76
  • 133
Noobzilla
  • 77
  • 1
  • 1
  • 6

1 Answers1

4

Executing a command getting its output

You can use this code to execute the above command:

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = new System.Diagnostics.ProcessStartInfo()
{
    UseShellExecute = false,
    CreateNoWindow = true,
    WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
    FileName = "cmd.exe",
    Arguments = "/C netstat -a -n |find \"5816\" |find \"ESTABLISHED\" /c",
    RedirectStandardError = true,
    RedirectStandardOutput = true
};
process.Start();
// Now read the value, parse to int and add 1 (from the original script)
int online = int.Parse(process.StandardOutput.ReadToEnd()) + 1;
process.WaitForExit();

This code starts the cmd.exe executable. Using the /C argument, you can give it the command you want to execute

A simple search in Stackoverflow gave me hundreds of questions that could help you.

Source: How To: Execute command line in C#, get STD OUT results, Run Command Prompt Commands

Community
  • 1
  • 1