title: Is it possible to execute multiple SSH commands from a single login session with SSH.NET?
body: <p>I'm using C# with SSH.NET and have been able to get a client SSH connection working fine to executing commands across SSH.  I can connect to a Linux install I have on a hypervisor VM with no issues, read back the <code>StdOut</code> and <code>StdErr</code>, etc.</p>
  <p>However, each command is like a completely new session - it always starts from <code>/home/user</code>, even if you finish from another directory with the last command. You <em><strong>can</strong></em> string multiple commands together with semicolons:</p>
  <pre><code>pwd; cd ..; pwd
  </code></pre>
  <blockquote>
  <pre><code>/home/user
  /home
  </code></pre>
  </blockquote>
  <p>but then if I execute:</p>
  <pre><code>pwd
  </code></pre>
  <blockquote>
  <pre><code>/home/user
  </code></pre>
  </blockquote>
  <p>I'm back at the home directory for that user.</p>
  <p>I'm not disconnecting and closing the session between the commands.  Here is some example code for several commands in a row, using changes in path to illustrate the issue:</p>
  <pre><code>public void MultiCommandTest()
  {
      string output1, output2;
  
      var host = Config.SshHostName;
      var port = Config.SshHostPort;
      var user = Config.SshUsername;
      var pass = Config.SshPassword;
      var auth = new PasswordAuthenticationMethod(user, pass);
      var info = new ConnectionInfo(host,port,user,auth);
  
      using (var ssh = new SshClient(info))
      {
          ssh.Connect();
          var cmd1 = ssh.CreateCommand(&quot;pwd; cd ..; pwd&quot;);
          output1 = cmd1.Execute();
          var cmd2 = ssh.CreateCommand(&quot;pwd; cd ..; pwd&quot;);
          output2 = cmd2.Execute();
          ssh.Disconnect();
  
      }
  
      string output = output1 + Environment.NewLine + output2;
      System.Windows.Forms.Clipboard.SetText(output);
      System.Windows.Forms.MessageBox.Show(output);
  }
  </code></pre>
  <p>I realize there is a difference between an interactive and non-interactive session when connecting to Bash, etc., but don't know enough about linux, etc. to know if that could be part of the solution (i.e. maybe it has to be resolved in the login side with <code>.profile</code>, <code>.bashrc</code>, or similar?).</p>
  <h2>Question:</h2>
  <p>So - is there a way to have a shell-like connection with SSH (and SSH.NET in particular)?  Or do I just need to serialize any sets of commands and send them all at once, always starting from scratch for any new command?</p>