使用 SSH.NET 在命令输出期间发送输入

2024-04-21

With PuTTY, I connect to an SSH server and then I execute a command that constantly output logs (multiple lines per second). During this process I can send some keystrokes which are commands, ie. putting 123 or Esc which stops the process. If I put a valid keystroke I can see it on output device, so I always know if keystroke was sent or not.

我想以编程方式执行此操作 - 连接到 SSH 服务器,执行主命令,然后将一些击键作为命令发送给它,同时监视日志,在其中我可以看到对命令的响应。

所以我想用 SSH.NET 来做这件事——它是一个简洁的 SSH 工具,它输出可靠,而且使用起来很舒服。但是我有一个问题 - 我可以连接到该服务器,执行命令,但是当它转储日志时我无法向它发送任何击键。

我尝试过使用的方法CreateShellStream:

  • shellStream.WriteLine('keystroke in ascii or just string')
  • sshClient.CreateCommand('keystroke in ascii or just string')进而sshClient.Execute();
  • using StreamWriter(shellstream)并写信给它

我尝试过 ascii 代码、UTF-16 代码、字符串,不幸的是没有任何效果。

使用 Plink(PuTTY 命令行工具)完成的相同操作工作得很好,但它更加混乱,我更喜欢使用 SSH.NET。您知道使用 SSH.NET 和 Plink 发送击键有什么区别吗?为什么它可以与 Plink 完美配合?我尝试过使用 Wireshark 来调试它,不幸的是 SSH 使得它很难做到这一点。

对于 Plink,我基本上使用这样的东西:

ProcessStartInfo psi = new ProcessStartInfo("plink.exe", arguments);
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.UseShellExecute = false;
plink = Process.Start(psi);
StreamWriter input = plink.StandardInput;
StreamReader output = plink.StandardOutput;

input.WriteLine("startCommand"); //Starts the process responsible for logs

input.Write("950"); //It correctly sends the keystrokes

// Some code regarding reading logs from output, some other commands sent to ssh server...

Console.WriteLine("Exiting...");
input.Write('\u001b');  //ESC in UTF16

我认为 SSH.NET 在发送其他任何内容之前会等待最后一个命令结束,但我不知道如何更改它。


我尝试使用 SSH.NET:

using (var client = new SshClient(host, user, new PrivateKeyFile(key)))
{
    int timoutCounter = 0;
    do
    {
        timoutCounter++;
        try { client.Connect(); }
        catch (Exception e)
        {
            Console.WriteLine("Connection error: " + e.Message);
        }
    } while (!client.IsConnected);

    ShellStream stream = client.CreateShellStream("shell", 200, 24, 1920, 1080, 4096);

    // it starts the process on remote ssh server that 
    stream.WriteLine("startCommand"); 
    Console.Write("Waiting for any key...");
    Console.ReadKey();

    for (int i = 0; i < 30; i++)
    {
        // Just some quick way for me to know that the device fully loaded
        stream.Expect("LogKeyWord");
    }
    Console.WriteLine("Device started");
    Console.ReadKey();

    // Different ways of entering commands during log output that I've tried

    stream.Write("5"); 
    Console.Write("Waiting for any key...");
    Console.ReadKey();

    stream.WriteLine("6");
    Console.Write("Waiting for any key...");
    Console.ReadKey();

    stream.Write("\u001b"); 
    Console.Write("Waiting for any key...");
    Console.ReadKey();

    var test97 = new StreamWriter(stream);
    test97.Write("7");
    Console.Write("Waiting for any key...");
    Console.ReadKey();

    test97.WriteLine("8");
    Console.Write("Waiting for any key...");
    Console.ReadKey();

    client.RunCommand("11");
    Console.Write("Waiting for any key...");
    Console.ReadKey();

    var test = client.CreateCommand("31");
    test.Execute();
    Console.Write("Waiting for any key...");
    Console.ReadKey();

    client.Disconnect();
}

只是一个猜测,但我会尝试刷新流。

Update:

写了一个小实验,我无法重现这个问题。看看这里:https://github.com/scottstephens-repros/SSHNetInterleavedIORepro https://github.com/scottstephens-repros/SSHNetInterleavedIORepro

因此,这可能是您的代码、您尝试远程运行的程序或远程计算机的操作系统、sshd 或配置特有的问题。我的远程测试机器是 CentOS 7.8,带有 OpenSSH 7.4p1。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 SSH.NET 在命令输出期间发送输入 的相关文章

随机推荐

  • WPF在触发器中设置边框背景

    我需要创建一个触发器 当 MouseEnter 发生时 它将更改 Border 背景属性 我做了以下事情
  • 如何使用 Powershell 3 增加并行进程数?

    我正在尝试并行运行 20 个进程 我如下更改了会话 但没有运气 我每个会话最多只能有 5 个并行进程 wo New PSWorkflowExecutionOption MaxSessionsPerWorkflow 50 MaxDisconn
  • cURL下载文件问题

    当我给出 URL 时 http 192 168 150 41 8080 filereport 31779 json 在浏览器中 它会自动下载文件为31779 report json 现在使用我正在尝试使用下载文件curl但我收到以下错误 c
  • 静态资源中的 Spring Boot 2.6 和 Angular

    我的问题非常类似于this https stackoverflow com questions 40769200 configure spring boot for spa frontend已经提出并回答过的问题 但不是 100 最新的 我
  • 声明两个同名变量

    是否可以调用在函数外部设置的同名变量 var a window width I want to call this variable if isFunction p var a window height Not this one aler
  • HTML.EditorFor 添加类不起作用

    我在将 css 类添加到 HTML EditorFor 时遇到问题 我在网上搜索是否有人遇到同样的问题 但我找不到任何东西 你能帮我么 所以 这不是关于未定义类的 css 问题 而是问题 但简单地说 它没有添加到输入字段中 这是视图中的代码
  • 同一网络中的 Fabric-Kafka 和 Fabric-odor 容器的用例是什么

    我可以看到有 3 种类型的排序者 当我部署结构网络时 最多使用两种类型 https hub docker com r hyperledger fabric orderer https hub docker com r hyperledger
  • 不明白objective-c中的多个参数声明

    有人可以帮我澄清一下吗 当有多个参数时 参数在方法名称中的冒号后面声明 参数在声明中将名称分开 就像在消息中一样 例如 void setWidth float width height float height 所以在上面 方法是实例变量
  • 是否可以使用容器管理的身份验证和密码加盐?

    我知道如何设置使用表单身份验证并使用摘要密码 例如 SHA 256 的普通容器管理安全性 像这样的东西 web xml
  • 如何在iPhone中的sqlite数据库中插入图像

    如何在数据库中插入图像 我尝试过但没有插入 查询的问题出在哪里 这是我的模型类 m 文件 此文件中的代码之后sqlite3 prepare v2我的痕迹没有插入sqlite3 bind blob之后就直接出去了sqlite3 prepare
  • 使用 Retrofit2 在 Android Studio 上的位置 0 处出现意外的 JSON 标记 # 错误

    我有一个问题 JSON 中位置 0 处出现意外的标记 我想做的是使用 Firebase 上的 Cloud Function 删除 Firebase 用户 没有返回 但错误显示 存在意外的 json 令牌 这是一些代码 CloudFuncti
  • Twitter API 速率限制

    我想从 Twitter 收集几周内的数据 为此 我使用 RStudio Server 和 crontab 自动运行多个脚本 如下所示 require ROAuth require twitteR require plyr load twit
  • 将通用扩展方法限制为基本类型和字符串

    我想要 XElement XAttribute 的扩展方法 它允许我应用 ValueOrDefault 逻辑 也许具有各种略有不同的实现 ValueOrNull ValueOrDefault NumericValueOrDefault 验证
  • HTML 实体解码为特殊字符

    我想在输出中显示特殊符号 例如 我的文本可能包含实体代码 例如 lt gt ETC 我想将其显示为 lt gt 在我的输出中 我需要在 SQL 中执行此操作 我用谷歌搜索了这个并得到了一个函数 select dbms xmlgen conv
  • 使用依赖注入在工作线程中实例化对象

    我的目标是在并行线程中运行一个永无止境的进程 问题是 我不能只在新线程中实例化我的工作服务 因为我在我的应用程序中使用 DI 根据我对 SO 的研究 我注意到很多人建议需要将抽象工厂注入到线程中 以在并行线程中动态实例化线程安全对象 1 h
  • 输出字符串字符总数

    我的问题是 如果我想获取用户输入的字符总数 应该使用什么方法 在不使用数组的情况下 我尝试使用 length 但它没有返回名字和姓氏中的所有字符 它只返回名字 这是我的代码的示例 请不要笑我在编程方面真的很新 System out prin
  • 在 SVG 路径中对 SVG 进行动画处理

    我已经构建了一个路径动画 但 svg 中的锯片有问题 我想要为 Sawblade 制作动画 360 度旋转
  • 尝试伪造和轮换用户代理

    我正在尝试伪造用户代理并在 Python 中轮换它们 我在网上找到了一个关于如何使用 Scrapy 执行此操作的教程scrapy 用户代理 https github com svetlyak40wt scrapy useragents包裹
  • 如何在 Jekyll 中列出同一类别的帖子?

    我想列出与当前帖子具有相同类别的固定数量的最近帖子 这就是我得出的结论 for category name in page categories limit 1 h2 Other articles in category name h2 u
  • 使用 SSH.NET 在命令输出期间发送输入

    With PuTTY I connect to an SSH server and then I execute a command that constantly output logs multiple lines per second