Search code examples
c#nmapprocess.start

C# Process.Start() with multiple arguments do not work (nmap)


Description

At the moment I am trying to call "nmap" through C# Process.Start(). But I can't get it work with my desired input arguments. At Process.Start() it seems to make a false parsing of the arguments. Do you know how I have to write the ArgumentList that my command is running right? In the commandline the same command works like charm...

Thank you!

Example

My simple example: (Edit: Suggestion of Lei Yang)

using System.Diagnostics;


namespace MyApp // Note: actual namespace depends on the project name.
{
    public class Program
    {
        public static void Main(string[] args)
        {
            using var myprocess = Process.Start
            (
                new ProcessStartInfo
                {
                    FileName = "/bin/zsh",
                    ArgumentList = {"-c nmap -p 8000-9000 -oG - 192.168.178.37/24 | awk -F'[/ ]' '{h=$2; for(i=1;i<=NF;i++){if($i==\"open\"){print h,$(i-1)}}}'" }

                }
            );

            myprocess.WaitForExit();
        }
    }
}

Output of this command:

zsh: bad option string: '-c nmap -p 8000-9000 -oG - 192.168.178.37/24 | awk -F'[/ ]' '{h=$2; for(i=1;i<=NF;i++){if($i=="open"){print h,$(i-1)}}}''

Desired output:

192.168.178.1 8089
192.168.178.1 8181
192.168.178.1 8182
192.168.178.1 8183
192.168.178.1 8184
192.168.178.1 8185
192.168.178.1 8186
192.168.178.43 8080

Solution

  • With the help of you I could solve the problem. Thank you!

    Solution for someone with the same problem:

    Variant 1: Direct way:

    using System.Diagnostics;
    
    
    namespace MyApp // Note: actual namespace depends on the project name.
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                using var myprocess = Process.Start
                (
                    new ProcessStartInfo
                    {
                        FileName = "/bin/zsh",
                        ArgumentList = {  "-c",  "nmap -p 8000-9000 -oG - 192.168.178.37/24 | awk -F'[/ ]' '{h=$2; for(i=1;i<=NF;i++){if($i==\"open\"){print h,$(i-1)}}}'" }
    
                    }
                );
    
                myprocess.WaitForExit();
            }
        }
    }
    

    Variant 2: Detour: Shell script (Maybe the easier way) C#:

    using System.Diagnostics;
    
    
    namespace MyApp // Note: actual namespace depends on the project name.
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                using var myprocess = Process.Start
                (
                    new ProcessStartInfo
                    {
                        FileName = "/bin/zsh",
                        ArgumentList = {"/home/alexander/tst.sh"}
                    }
                );
    
                myprocess.WaitForExit();
            }
        }
    }
    

    Shell script:

    #!/bin/zsh
    nmap -p 8000-9000 -oG - 192.168.178.37/24 | awk -F'[/ ]' '{h=$2; for(i=1;i<=NF;i++){if($i=="open"){print h,$(i-1)}}}'
    

    Output of both variants:

    192.168.178.1 8089
    192.168.178.1 8181
    192.168.178.1 8182
    192.168.178.1 8183
    192.168.178.1 8184
    192.168.178.1 8185
    192.168.178.1 8186
    192.168.178.43 8080