2

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
  • 36,391
  • 15
  • 88
  • 148
Noobzilla
  • 97
  • 1
  • 1
  • 6

1 Answers1

6

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

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

Mister Verleg
  • 4,053
  • 5
  • 43
  • 68
  • This answer works for executing "built-in" commands, but doesn't seem to work when running an arbitary executable. – Steve Smith Sep 26 '22 at 13:52