Skip to content

ShellStream copies from output stream back to input stream when Task.Delay is used #1168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Kemsekov opened this issue Sep 2, 2023 · 1 comment
Milestone

Comments

@Kemsekov
Copy link

Kemsekov commented Sep 2, 2023

Problem

Shell copies output stream back to input stream when Task.Delay is called.

How to reproduce

using var client = new Renci.SshNet.SshClient("ip","root","password");

client.Connect();

var file = File.Open("output.txt",FileMode.Append);
using var shellStream = client.CreateShellStream("shell1",0,0,0,0,10000000);
using var shell = client.CreateShell(shellStream,file,null);

shell.Start();

shellStream.WriteLine("echo \"touch very_dangerous_command.txt\" > file.txt");
Task.Delay(1000).Wait();

shellStream.WriteLine("cat file.txt");
Task.Delay(1000).Wait();

shellStream.WriteLine("rm file.txt");
Task.Delay(1000).Wait();

shellStream.WriteLine("history");
Task.Delay(1000).Wait();

shell.Stop();

What effects it have:

It does create file.txt with content touch very_dangerous_command.txt

It prints to output buffer touch very_dangerous_command.txt

It does remove file.txt.

But when history is called it repeats everything back inside of itself and in the end on my remote machine I have both files 'file.txt' and 'very_dangerous_command.txt' which is nuts. =/

interestingly, without Task.Delay() calls it works just fine.

Results

Files in the /root on server before program execution and after.
image

Text file that shows what is happening to shell. There is a lot of repeated/misplaced commands, which leads to chaos.
output.txt

Setup

dotnet 7.0.105
"SSH.NET" Version="2020.0.2

Linux myhostname 6.3.8-arch1-1 #1 SMP PREEMPT_DYNAMIC Wed, 14 Jun 2023 20:10:31 +0000 x86_64 GNU/Linux

@Kemsekov Kemsekov changed the title Shell copies from output stream back to input stream when Task.Delay is used ShellStream copies from output stream back to input stream when Task.Delay is used Sep 2, 2023
@Kemsekov Kemsekov changed the title ShellStream copies from output stream back to input stream when Task.Delay is used ShellStream copies from output stream back to input stream when Task.Delay is used Sep 2, 2023
@Rob-Hague
Copy link
Collaborator

I could be wrong, but I don't think Shell and ShellStream are meant to be used together: they both create a "session" channel and a shell independently. I think you might try passing a standard MemoryStream to the input parameter of CreateShell which you can wrap in a StreamWriter.

Or alternatively, use the ShellStream as you are doing but without the Shell.

As to why these two types both exist, I have no idea.

@WojciechNagorski WojciechNagorski added this to the vNext milestone Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants