如何使用SharpSSH执行提示输入额外的linux命令? [英] how to use SharpSSH for Executing linux commands which prompts for additonal input?

查看:385
本文介绍了如何使用SharpSSH执行提示输入额外的linux命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Ok heres我想做什么



我必须建立一个SSH会话到一个linux机器,并发出一个命令,如ls或cp。 / p>

继承代码片段

  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());

事情就像ls它提供输出,但像 rm



这样的命令需要像y这样的附加确认



从上面的代码片段可以看出,代码本质上生成一个字节序列,如



rm vivek [enter] y [enter]



但它不工作,它似乎仍然在输入



  [root @ host〜]#rm vivek 
rm:删除常规文件`vivek'?

我在这里做错了什么?



希望这个问题很清楚。



PS我已经尝试过SSHExec示例, p>

解决方案

为什么不能仅仅通过 rm vivek -f 来避免确认? / p>

否则,在读取回应之前,请预先回答问题:

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


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.

解决方案

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());

这篇关于如何使用SharpSSH执行提示输入额外的linux命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆