Search code examples
c#gitcmd

Executing Commands in CMD and accessing a git on-premises server


I have configured password less auth for my git on-premises. Now I want to first go to the ssh dir in local machine and then access the git server from there all using cmd. I am doing this whole thing using C# code. Below is my code.

        ProcessStartInfo processStartInfo = null;
        Process process = null;
        Process main = null;
        ProcessStartInfo processMainInfo = null;


        processStartInfo = new ProcessStartInfo("cmd.exe","ssh [email protected]");
        processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        processStartInfo.WorkingDirectory = @"C:\Users\xyz\.ssh";

        process = new Process();
        process.StartInfo = processStartInfo;
        process.StartInfo.RedirectStandardError = true;
        process.StartInfo.RedirectStandardOutput = true;
        process.StartInfo.RedirectStandardInput = true;
        process.StartInfo.UseShellExecute = false;
        process.Start();
        process.StandardInput.WriteLine("/c cd git_home && git log");


        string returnValue = process.StandardOutput.ReadToEnd();

I know that /c first executes the and then terminates. I tried to use && for the whole command like this :

> ssh [email protected] && cd git_home && git log

but in this case only ssh [email protected] this part is getting executed and the other cd git_home && git log are not.

Can anyone help me here?

My main goal here is to execute two commands one after another 1st :

> ssh [email protected] 

this should get executed it will give me access to the server form the cmd and after that

> cd git_home && git log

which will get me to the git repo and give me all the logs as output in string returnValue.

or if there is some more efficient way pls suggest.

Edit

I tried to run the commands one after another below is the code:

            Process p = new Process();
        ProcessStartInfo info = new ProcessStartInfo();
        info.FileName = "cmd.exe";
        info.RedirectStandardInput = true;
        info.RedirectStandardOutput = true;
        info.UseShellExecute = false;

        p.StartInfo = info;
        p.Start();

        using (StreamWriter sw = p.StandardInput)
        {
            if (sw.BaseStream.CanWrite)
            {
                sw.WriteLine(" C:");
                sw.WriteLine(@"cd C:\Users\xyz\.ssh");
                sw.WriteLine(" ssh [email protected]");
                sw.WriteLine("cd git_home");
                sw.WriteLine("git log");

            }
            string aaa = p.StandardOutput.ReadToEnd();
        }

But the string aaa returns :

    Microsoft Windows [Version 10.0.19042.1023]
(c) Microsoft Corporation. All rights reserved.

D:\Kovair Projects\Adapters\KOVAIR_Git_KovairGitAdapter_2.20 - EventService-BAK\Source\TestAPP\bin> C:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE>cd C:\Users\xyz\.ssh

C:\Users\xyz\.ssh> ssh [email protected]

C:\Users\xyz\.ssh>cd git_home

C:\Users\xyz\.ssh>git log

C:\Users\xyz\.ssh>

what can I do so that I can execute the last 2 commands in the linux server?


Solution

  • [email protected] "cd git_home ; git log" execute this command this might help.