c# – SSH: enter directory (cd)

Question:

I created a button on the form and when I clicked, I want the command to be sent to the remote server (starting the game server), but it doesn't work, although everything works fine through putty .

Here is the code:

private void button2_Click(object sender, EventArgs e)
{
    PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo("ip", port, "user", "password");
    using (var client = new SshClient(connectionInfo))
    {
        try
        {
            client.Connect();
            if (client.IsConnected)
            {
                client.RunCommand("cd srv-cr-mp-c3-linux");
                client.RunCommand("nohup ./samp03svr-cr &");
            }
            else
            {
                label1.Text = "Ошибка";
            }
        }
        catch (Exception)
        {
            label1.Text = "Ошибка";
        }
        finally
        {
            client.Disconnect();
        }
    }
}

The connection is successful, but the command to enter the directory does not work, followed by the second command, respectively. Because the second command must be executed in the srv-cr-mp-c3-linux folder. I tried to log into another (created) folder, but it still doesn't work. The rights were set to 777 . Tell me, please, what could be the matter?

Answer:

These methods are executed from the working directory:

// ...
{
    client.RunCommand("cd srv-cr-mp-c3-linux");
    client.RunCommand("nohup ./samp03svr-cr &");
}
// ...

Every time you RunCommand() . You need to specify workingDir for client , or execute cd srv-cr-mp-c3-linux | nohup ./samp03svr-cr & , then first you go to the directory, and there you do what you need.

Linux is not at hand, I could be wrong, try cd srv-cr-mp-c3-linux && nohup ./samp03svr-cr & – this will execute the commands in turn.

Scroll to Top