gpt4 book ai didi

c# - C# SSH .Net 库中的 Sudo 命令

转载 作者:太空狗 更新时间:2023-10-30 00:31:17 24 4
gpt4 key购买 nike

这是我能找到的 SSH .Net 库的最简单的 sudo 实现。但是,我无法让它工作。

    using (var ssh = new SshClient("hostname", "username", "password"))
{
ssh.Connect();
var input = new MemoryStream();
var sr = new StreamWriter(input);
var output = Console.OpenStandardOutput();

var shell = ssh.CreateShell(input, output, output);
shell.Stopped += delegate
{
Console.WriteLine("\nDisconnected...");
};

shell.Start();
sr.WriteLine("sudo ls");
sr.Flush();
Thread.Sleep(1000 * 1);
sr.WriteLine("password");
sr.Flush();
Thread.Sleep(1000 * 100);
shell.Stop();
}

我每次都会收到以下错误

上次登录:2015 年 1 月 14 日星期三 15:51:46 来 self 的电脑


公司资料


SshNet.Logging Verbose:1:从服务器接收消息:“ChannelDataMessage”:“SSH_MSG_CHANNEL_DATA:#0”。SshNet.Logging Verbose:1:从服务器接收消息:“ChannelDataMessage”:“SSH_MSG_CHANNEL_DATA:#0”。[1;36m这是 BASH [1;31m4.1[1;36m- 显示在 [1;31m:0.0[m

2015 年 1 月 14 日星期三 15:55:50 CSTSshNet.Logging Verbose:1:从服务器接收消息:“ChannelDataMessage”:“SSH_MSG_CHANNEL_DATA:#0”。SshNet.Logging Verbose:1:从服务器接收消息:“ChannelDataMessage”:“SSH_MSG_CHANNEL_DATA:#0”。-bash: And,: 命令未找到[0;34musername@host [0;31m[15:55:50]>[0m

最佳答案

        public void ExpectSSH (string address, string login, string password, string command)
{
try
{
SshClient sshClient = new SshClient(address, 22, login, password);

sshClient.Connect();
IDictionary<Renci.SshNet.Common.TerminalModes, uint> termkvp = new Dictionary<Renci.SshNet.Common.TerminalModes, uint>();
termkvp.Add(Renci.SshNet.Common.TerminalModes.ECHO, 53);

ShellStream shellStream = sshClient.CreateShellStream("xterm", 80,24, 800, 600, 1024, termkvp);


//Get logged in
string rep = shellStream.Expect(new Regex(@"[$>]")); //expect user prompt
this.writeOutput(results, rep);

//send command
shellStream.WriteLine(commandTxt.Text);
rep = shellStream.Expect(new Regex(@"([$#>:])")); //expect password or user prompt
this.writeOutput(results, rep);

//check to send password
if (rep.Contains(":"))
{
//send password
shellStream.WriteLine(password);
rep = shellStream.Expect(new Regex(@"[$#>]")); //expect user or root prompt
this.writeOutput(results, rep);
}

sshClient.Disconnect();
}//try to open connection
catch (Exception ex)
{
System.Console.WriteLine(ex.ToString());
throw ex;
}

}

关于c# - C# SSH .Net 库中的 Sudo 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27953227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com