我正在使用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-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命令并在我的代码中获取返回字符串!
无论如何,一个周末愉快。
大概 my command
中有 sudo
?
sudo
是互动的,请求密码,而RunCommand
无论如何都不会提供输入到命令。
您需要改用ShellStream
。
var sh = client.CreateShellStream("", 0, 0, 0, 0);
sh.WriteLine("my command");
sh.WriteLine("sudo password");