使用SSH.NET的Linux:“对不起,你必须有一个tty来运行sudo”

时间:2018-02-09 21:02:56

标签: c# linux ssh ssh.net

我正在使用Renci.SshNet,不知何故,远程Linux服务器知道我没有使用终端模拟器连接。所以:

using Renci.SshNet;

var connectionInfo = new ConnectionInfo("host","username", new PasswordAuthenticationMethod("username", "password"));
using (var client = new SshClient(connectionInfo))
client.Connect();

var test = client.RunCommand("sudo mycommand");

结果

"sudo: sorry, you must have a tty to run sudo"

问题:如何使用SSH.NET模拟终端模拟器?

我在https://refactoragain.com/2016/05/27/ssh-net-echo-sudo-s/上找到的以下代码似乎创建了终端仿真:

SshClient client = new SshClient(server_address, 22, login, password);
client.Connect();

IDictionary<Renci.SshNet.Common.TerminalModes, uint> modes = 
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, modes);
var output = shellStream.Expect(new Regex(@"[$>]")); 

shellStream.WriteLine("sudo sh/home/my_user_name/scripts/install_and_configure.sh"); 
output = shellStream.Expect(new Regex(@"([$#>:])"));
shellStream.WriteLine(password);
client.Disconnect();

但它对我来说是希腊语,因为它需要一个我似乎没有用户名的主目录。为什么我必须断开连接? (代码中的最后一行)

我只想运行Linux命令并在我的代码中获取返回字符串!

无论如何,这是一个愉快的周末。

1 个答案:

答案 0 :(得分:3)

据推测,ionViewDidLoad() { this.slides.lockSwipes(true); } 中有my command

sudo是交互式的,请求密码,sudo无论如何都不提供输入命令。

您需要改为使用RunCommand

ShellStream
相关问题