how to use SharpSSH for Executing linux commands which prompts for additonal input?

徘徊边缘 提交于 2019-12-12 10:12:48

问题


Ok heres what I'm trying to do

I have to setup an SSH session to a linux machine and issue a command like "ls" or cp.

heres the snippet

            SshStream  ssh = new SshStream ("myhost","root","pass");
            ssh.Prompt ="#";
            ssh.RemoveTerminalEmulationCharacters = true;
            byte[] byt = System.Text.Encoding.ASCII.GetBytes("rm vivek");
            Array.Resize (ref byt,byt.Length +3 );
            byt[byt.Length-1]=(byte)'\r';
            byt[byt.Length - 2] = (byte)'y';
            byt[byt.Length - 3] = (byte)'\r';
            ssh.Write(byt);

            Console.WriteLine(ssh.ReadResponse());

The thing is commands like "ls" are simple you type it and it gives the output, but commands like rm

require addtional acknowledement like an "y"

as you can see from the above snippet the code essentially generates a bytes sequence like

rm vivek [enter] y [enter]

But it doesnt work, it is as if it is still wating for the input at

[root@host ~]# rm vivek
rm: remove regular file `vivek'?

What am I doing wrong here?

Hope the question is clear.

P.S I have tried the SSHExec sample again the same result.


回答1:


Why not just do rm vivek -f to avoid the confirmation ?

Otherwise, just expect the question and answer it before reading the response:

 ssh.Write(byt);
 ssh.Expect("rm: remove .*");
 ssh.WriteLine("y");
 Console.WriteLine(ssh.ReadResponse());


来源:https://stackoverflow.com/questions/9857788/how-to-use-sharpssh-for-executing-linux-commands-which-prompts-for-additonal-inp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!