How to run python tool Command on Linux Server from dot net console App (C#)?

NALB 71 Reputation points
2023-02-20T14:57:04.0466667+00:00

Hello ,

I have a python command that runs python tool on Linux server but how I can run it from C# dotnet ?

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-02-21T16:58:13.56+00:00

    (extra post to force display)

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-02-20T16:41:58.5466667+00:00

    assuming the C# program is on the linux box, you use the process class to run a process

    https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=net-7.0

    note: on linux Process.Start() will do a fork().

    0 comments No comments

  2. NALB 71 Reputation points
    2023-02-21T15:19:43.8566667+00:00

    Thank you for responding , but can you show me what my code should look like as I have tried to use the code that was provided inside the mentioned link but id did not work ?

    This is my command that I use on my shell in Linux : amass enum -d test.com> ~/FolderXTest/file.txt

    The problem my command not fille script it is tool ....

    How can i translate and call it in vs on Linux Server ?

    0 comments No comments

  3. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-02-21T16:55:48.5666667+00:00

    you need to capture the output and write to the file yourself.

    // start process
    using var process = Process.Start(new ProcessStartInfo
    {
        UseShellExecute = false
        FileName = "amass",
        RedirectStandardOutput = true,
        ArgumentList = { "enum", "-d", "test.com" }
    });
    // read std out
    var output = process.StandardOutput.ReadToEnd();  
    // wait for exit
    process.WaitForExit();
    // write out out
    File.WriteAllText("~/FolderXTest/file.txt", output);
    

    note: if the the output is large, then use streams. also you could create a shell script with the redirect and just execute the script. be sure to set the execute bit

    using var process = Process.Start(new ProcessStartInfo
    {
        FileName = "myscript",
    });
    process.WaitForExit();
    
    0 comments No comments

  4. NALB 71 Reputation points
    2023-02-27T08:26:31.6533333+00:00

    Hello ,

    Thank you so much for responding this worked for me ... but how can i execute this kind of commands

    (cat Test.txt | ToolName > Output.txt) as with previous code it did not work ...

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.