示例#1
0
        static void KillSSHAgent()
        {
            Process existingProcess = null;

            try
            {
                existingProcess = Process.GetProcessById(Convert.ToInt32(AgentPID));
            }
            catch (Exception) { }
            if (String.IsNullOrEmpty(AgentPID) || existingProcess == null || existingProcess.Id < 1)
            {
                Console.Error.WriteLine("Either the environment is currently not configured for ssh-agent or it " +
                                        "has already been killed.");
                Environment.Exit(1);
            }

            string  SSHAgentPath = FindProgram("ssh-agent.exe");
            Process SSHAgent     = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName               = SSHAgentPath,
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError  = true,
                    CreateNoWindow         = true,
                    Arguments              = "-k",
                }
            };

            SSHAgent.StartInfo.EnvironmentVariables[SSH_AGENT_PID] = AgentPID;

            try
            {
                SSHAgent.Start();

                Environment.SetEnvironmentVariable(SSH_AGENT_PID, null, EnvironmentVariableTarget.User);
                Environment.SetEnvironmentVariable(SSH_AUTH_SOCK, null, EnvironmentVariableTarget.User);

                Process parent = FindParent.ParentProcess(Process.GetCurrentProcess());

                if (!SSHAgent.StandardError.EndOfStream)
                {
                    while (!SSHAgent.StandardError.EndOfStream)
                    {
                        Console.Error.WriteLine(SSHAgent.StandardError.ReadLine());
                    }
                }

                if (parent.ProcessName == "powershell")
                {
                    Console.WriteLine("Remove-Item env:" + SSH_AUTH_SOCK);
                    Console.WriteLine("Remove-Item env:" + SSH_AGENT_PID);

                    Console.WriteLine("# ssh-agent has been killed and your environment has been configured. " +
                                      "Run these commands to configure current terminal or open a new one.");
                }
                else if (parent.ProcessName == "cmd")
                {
                    Console.WriteLine("set " + SSH_AUTH_SOCK + "=");
                    Console.WriteLine("set " + SSH_AGENT_PID + "=");

                    Console.WriteLine("rem ssh-agent has been killed and your environment has been configured. " +
                                      "Run these commands to configure current terminal or open a new one.");
                }
                else
                {
                    while (!SSHAgent.StandardOutput.EndOfStream)
                    {
                        Console.WriteLine(SSHAgent.StandardOutput.ReadLine());
                    }
                    {
                        Console.WriteLine("# ssh-agent has been killed and your environment has been configured. " +
                                          "Run these commands to configure current terminal or open a new one.");
                    }
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.Message);
                Environment.Exit(1);
            }
        }
示例#2
0
        static void RunSSHAgent()
        {
            try
            {
                Process existingProcess = Process.GetProcessById(Convert.ToInt32(AgentPID));

                if (!existingProcess.Responding || Convert.ToInt32(AgentPID) < 1)
                {
                    throw new Exception("There is no process running or the previous ssh-agent is not responding.");
                }

                Console.Error.WriteLine("Another ssh-agent (PID: " + AgentPID + ") is already running healthily");
            } catch (Exception)
            {
                string  SSHAgentPath = FindProgram("ssh-agent.exe");
                Process SSHAgent     = new Process
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName               = SSHAgentPath,
                        UseShellExecute        = false,
                        RedirectStandardOutput = true,
                        RedirectStandardError  = true,
                        CreateNoWindow         = true
                    }
                };

                try
                {
                    SSHAgent.Start();

                    Process parent = FindParent.ParentProcess(Process.GetCurrentProcess());

                    if (!SSHAgent.StandardError.EndOfStream)
                    {
                        while (!SSHAgent.StandardError.EndOfStream)
                        {
                            Console.Error.WriteLine(SSHAgent.StandardError.ReadLine());
                        }
                    }

                    while (!SSHAgent.StandardOutput.EndOfStream)
                    {
                        var line = SSHAgent.StandardOutput.ReadLine();

                        string[] splits  = line.Split(';');
                        string[] command = splits[0].Split('=');

                        if (command[0] == SSH_AUTH_SOCK && command.Length > 1)
                        {
                            AgentSock = command[1];
                            Environment.SetEnvironmentVariable(SSH_AUTH_SOCK, command[1], EnvironmentVariableTarget.User);
                        }
                        else if (command[0] == SSH_AGENT_PID && command.Length > 1)
                        {
                            AgentPID = command[1];
                            Environment.SetEnvironmentVariable(SSH_AGENT_PID, command[1], EnvironmentVariableTarget.User);
                        }

                        if (parent.ProcessName != "cmd" && parent.ProcessName != "powershell")
                        {
                            Console.WriteLine(line);
                        }
                    }

                    if (parent.ProcessName == "powershell")
                    {
                        Console.WriteLine("$env:" + SSH_AUTH_SOCK + "=\"" + AgentSock + "\"");
                        Console.WriteLine("$env:" + SSH_AGENT_PID + "=\"" + AgentPID + "\"");

                        Console.WriteLine("# Your environment has been configured. " +
                                          "Run these commands to configure current terminal or open a new one.");
                    }
                    else if (parent.ProcessName == "cmd")
                    {
                        Console.WriteLine("set " + SSH_AUTH_SOCK + "=" + AgentSock);
                        Console.WriteLine("set " + SSH_AGENT_PID + "=" + AgentPID);

                        Console.WriteLine("rem Your environment has been configured. " +
                                          "Run these commands to configure current terminal or open a new one.");
                    }
                    else
                    {
                        Console.WriteLine("# Your environment has been configured. " +
                                          "Run these commands to configure current terminal or open a new one.");
                    }
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e.Message);
                    Environment.Exit(1);
                }
            }
        }