2

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.

Vivek Bernard
  • 2,063
  • 3
  • 26
  • 43

1 Answers1

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());
Diego
  • 18,035
  • 5
  • 62
  • 66
  • Brilliant Idea! +1 But it only solves the problem with "rm" what if I'm configuring a router and I have to talk to an IOS where commands doesnt always have these options... thats my fundamental problem..Please dont get me wrong,I'm just explaining my problem – Vivek Bernard Mar 25 '12 at 04:00
  • You have to think of it as a dialog. You send something, you expect something, you send something else, etc. – Diego Mar 25 '12 at 04:03