问题
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