如何通过sudo运行命令并通过ssh .net c#输入密码 [英] How to run commands by sudo and enter password by ssh .net c#

查看:607
本文介绍了如何通过sudo运行命令并通过ssh .net c#输入密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行命令并在具有Linux操作系统的远程计算机上获得结果.我正在使用ssh .net库通过C#代码进行连接.我可以连接并运行一些不需要使用"sudo" 的命令.但是我不知道如何运行需要由sudo运行的命令,因为在运行-例如-"sudo iptables -L -n"之后,我应该输入密码,而执行此操作后我也不知道如何输入密码命令.

I want to run command and get the result on remote computer which has Linux operating system. I am using ssh .net library to connect by C# code. I can connect and run some commands that it doesn't need to use "sudo" before. but I don't know How to run commands which need to be run by sudo, because after run -for instance- "sudo iptables -L -n" I should enter password and I don't know How to enter password after execute this command.

这是我的代码:

private void connectingToLinuxHost(string ip, string username, string password, int port)
{
    SshCommandLineRunner ssh = new SshCommandLineRunner(ip, username, password, port);
    ssh.Connect();
    string output1 = ssh.ExecuteCommand("ls");

    ssh.ExecuteCommand("sudo iptables -L -n");
    Thread.Sleep(1000);
    string output2 = ssh.ExecuteCommand(password);

    ssh.ExecuteCommand("sudo iptables -L -n \n");
    Thread.Sleep(1000);
    string output3 = ssh.ExecuteCommand(password);
}

该函数运行时,我可以成功连接到远程PC,输出1的值正确,但是输出2和输出3为空.实际上很明显,在sudo命令之后需要输入密码. 我已阅读有关ssh .net的大多数问题.一些类似的问题仍悬而未决.

when this function run, I can connect to remote pc successfully, output1 has correct value, but output2 and output3 are empty. actually it is obvious that after sudo command it is needed to enter password. I have read most question about ssh .net. some similar question has retain unanswered.

SSH .NET:冻结sudo su-user2 (他或她不知道密码,但我知道我不知道如何通过代码输入密码)

SSH .NET : freeze upon sudo su - user2 ( he or she didnt know the password but I know I dont know how to enter it via code)

如何使用SSH.Net进行安装? (我使用了create shell方法,但是我不明白它的用途,并且像这个问题一样,我得到了"Last login:..."的输出.)

How to su with SSH.Net? (I used create shell method but i couldn't understand what is for and like this question I got "Last login: ..." output.)

推荐答案

最安全的方法是使用CreateShellStream并在运行sudo命令后直接将密码直接写入流中.就像您使用交互式终端一样,将发送密码.但是,如果您不希望被无休止的流用于其余的工作,那么这可能不是一个方便的解决方案.示例:

The most secure way to do it, as already mentioned by a comment, is to use CreateShellStream and just write the password directly to the stream after running the sudo command. The password gets sent just as if you had been using an interactive terminal. This might not be a convenient solution, though, if you don't want to be locked into using an endless stream for the rest of what you want to do. Example:

var promptRegex = new Regex(@"\][#$>]"); // regular expression for matching terminal prompt
var modes = new Dictionary<Renci.SshNet.Common.TerminalModes, uint>();
using (var stream = ssh.CreateShellStream("xterm", 255, 50, 800, 600, 1024, modes))
{
    stream.Write("sudo iptables -L -n\n");
    stream.Expect("password");
    stream.Write("mypassword\n");
    var output = stream.Expect(promptRegex);
}

缺点是您的输出将包含您不真正想要的垃圾:控制字符,提示以及通过流发送的所有其他内容.

The downside is that your output will include junk you don't really want: control characters, prompts, and everything else that gets sent over the stream.

如果要避免使用Shell流,则可以(取决于安全性设置)通过stdin提供密码.这是不安全的,因为命令会在不同的地方登录,并且您可能向具有root用户访问权限的其他用户透露密码.如果您是唯一的用户,或者您不关心其他人都可以看到您的密码,那么这可能对您来说更方便.

If you want to avoid using a shell stream then you may be able to (depending on security settings) provide a password via stdin. THIS IS INSECURE because commands get logged in various places and you might be revealing your password to other users with root access. If you're the only user, or if you don't care that everybody else can see your password, then this might be more convenient for you.

示例:

using (var cmd = ssh.RunCommand("echo -e 'mypassword\n' | sudo -S iptables -L -n"))
{
    if (cmd.ExitStatus == 0)
        Console.WriteLine(cmd.Result);
    else
        Console.WriteLine(cmd.Error);
}

最后,也可以使用脚本将您的密码打印到stdin.这样,您的密码将不会与其余命令行一起记录;但这仍然不是更安全,因为具有root用户访问权限的任何人都可以阅读该脚本并查看密码:

Finally, it's also possible to have a script print your password to stdin. This way your password won't get logged along with the rest of the command line; but this still isn't much more secure since anyone with root access could potentially read the script and see the password:

using (var cmd = ssh.RunCommand("~/printpasswd.sh | sudo -S iptables -L -n"))
{
    if (cmd.ExitStatus == 0)
        Console.WriteLine(cmd.Result);
    else
        Console.WriteLine(cmd.Error);
}

并在printpasswd.sh内:

and inside printpasswd.sh:

#!/bin/bash
echo -e 'mypassword\n'

这篇关于如何通过sudo运行命令并通过ssh .net c#输入密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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