- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想连接一个 SSH 服务器,在连接时运行一个自定义命令,该命令需要 XML 作为输入(UTF-8 编码,多行,在每个 XML 文档的末尾有一个特殊的行标记它的结束)和输出 XML(UTF-8 编码,多行,末尾有相同的特殊行表示文档结束)。
目前有一种使用 plink.exe(以 PuTTy 闻名)的机制有效(意思是:服务器端确实按规定工作),但我正在消除对外部进程的需求。
我尝试使用 Renci.SshNet 库,但我发现文档不充分,并且找不到我需要的示例。
到目前为止我的尝试:
using System;
using Renci.SshNet;
namespace ServerConnect
{
public class ServerConnection
{
private const string EOC= "*-* COMMAND DELIMITER *-*";
private SshClient scon;
private System.IO.Stream stdin, stdout, stderr;
private System.IO.StreamWriter stdin_writer;
private System.IO.StreamReader stdout_reader;
private Shell shell;
public ServerConnection (string keyFileName, string keyFilePassphrase)
{
ConnectionInfo ci= new ConnectionInfo("my.server.local", 22, "datamgr",
new PrivateKeyAuthenticationMethod("datamgr", new PrivateKeyFile(keyFileName, keyFilePassphrase)));
this.scon= new SshClient(ci);
}
public ServerConnection (string keyFilePassphrase): this("client_id_rsa", keyFilePassphrase) {}
public void Connect() {
this.scon.Connect();
this.stdin= new System.IO.MemoryStream();
this.stdin_writer= new System.IO.StreamWriter(stdin);
this.stdout= new System.IO.MemoryStream();
this.stdout_reader= new System.IO.StreamReader(stdout);
this.stderr= new System.IO.MemoryStream();
this.shell= this.scon.CreateShell(this.stdin, this.stdout, this.stderr);
this.shell.Start();
}
public void Disconnect() {
this.scon.Disconnect();
}
public string Command(string text) {
Console.WriteLine("sending command");
this.stdin_writer.WriteLine(text);
Console.WriteLine("sending EOC");
this.stdin_writer.WriteLine(EOC);
this.stdin_writer.Flush();
for (;;) {
string line;
line= this.stdout_reader.ReadLine();
if (line == null) {
Console.WriteLine("got null");
break;
}
Console.WriteLine("READ");
Console.WriteLine(line);
if (line.StartsWith(EOC)) break;
}
return ""; // yes, this should return the built output
}
}
}
鉴于这是一个程序集,我正处于尝试连接到服务器的阶段(为方便起见,测试通过 booish
运行):
>>> import ServerConnect
>>> a= ServerConnection("passphrase")
ServerConnect.ServerConnection
>>> a.Connect()
>>> a.Command("<i command=\"ping\"/>")
sending command
sending EOC
got null
''
我可以看到(在服务器的 auth.log 中)连接是在 a.Connect()
调用之后建立的。但是,似乎无论我写入 stdin_writer
都不会发送到服务器。
我怎样才能实现这个机制?向服务器发送数据,然后发送分隔符,然后读取数据,直到我读取分隔符?该循环将在一次连接期间重复多次。
请注意,我不是经验丰富的 C# 程序员,因此我可能缺乏对 C# 流模型的基本了解。
PS 我看过this答案,但似乎 CreateShellStream
调用 block ,可能在 PseudoTty
分配中(该过程不需要伪 ttys,只需要输入/输出)。
PS2 我从 codeplex.com 下载并编译了最新版本的 Renci.SshNet,现在我在这一点上(Connect 方法摘录):
this.scon.Connect();
this.stream= this.scon.CreateShellStream("dumb", 80, 24, 800, 600, 1024);
this.stdin_writer= new System.IO.StreamWriter(this.stream);
this.stdout_reader= new System.IO.StreamReader(this.stream);
this.shell= this.scon.CreateShell(this.stream, this.stream, this.stream);
this.shell.Start();
但是,对 stdin_writer 的任何写入(以及随后的刷新)似乎都没有通过。
最佳答案
抱歉,输入此内容后我发现我在发帖...
你可以使用这里给出的解决方案,这是我想做类似你的事情时使用的:
Renci ssh.net - Create console screen from form when connected
您可以放弃使用任何 System.IO 对象并使用 ShellStream 对象操纵 SSH session IO。
ShellStream.Expect 将读取输出,并查找特定的字符串。ShellStream.WriteLine 会将输入发送到 shell。
string reply = string.Empty;
ShellStream ss = scon.CreateShellStream("dumb", 80, 24, 800, 600, 1024);
reply = ss.Expect("Press enter to continue"); //The prompt I get after login
ParseOutput(reply);
ss.WriteLine("./myscript.sh")
reply = ss.Expect("$"); //My prompt, when the script has finished.
ParseOutput(reply);
ParseOutput 是我编写的一个例程,用于在每个换行符处拆分“回复”字符串,并将每一行添加到表单的列表框中。
关于c# - 如何写入标准输入/从 SSH 连接的远程进程的标准输出读取? (Renci.SshNet),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30079526/
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 10年前关闭。 Improve this qu
我错误地设置了 ssh key 的密码。 但是每次使用 key 时都要输入密码很烦人。 我想删除我的 key 的密码: $ ssh-keygen -p -f 但是,我担心 key 会被更改。 由于我
你好!我在连接到远程主机时遇到问题。我忘记了 ssh 密码,但我有 SSH key 的指纹。是否可以仅使用指纹进行连接? 最佳答案 不会。机器的指纹仅用于验证您正在连接您认为正在连接的机器。它是用于避
我有一个 Ubuntu 服务器 example.com 并且我已经通过 ssh 进入了它。但只有在我通过 ssh 进入它之后,我才意识到我打算执行 ssh -L 8000:localhost:9000
通过终端,我可以多次 SSH 连接到服务器: (客户端--->网关--->服务器1---->服务器2---) 但是现在要通过Java的JSch库来实现,怎么办呢?首先尝试了端口转发,但在终端上我没有这
在创建一个新的仓库(本地 + 远程)之后,我正在尝试推送提交。但是我遇到了 SSH key 的问题 在我的 ~/.ssh/config 中,我有一个 gitlab 条目定义为 Host MyRepoJ
我发现 ssh 和 ssh-keygen 有一种奇怪的行为:它们根本没有反应。 Cygwin 以管理员权限启动并正常运行。主机 192.168.1.1 已启动,我可以远程桌面到它: 当我尝试时: $
为什么eval语句返回 Illegal variable name $ eval "$(ssh-agent -s)" Illegal variable name. 最佳答案 请尝试$ eval "ss
我正在尝试通过以下命令从我的本地主机连接到远程主机。但是远程主机中有一个设置,我们登录后不久它会提示输入徽章 ID、密码和登录原因,因为它在 profile 中是这样编码的文件在 remote-hos
有时,我被迫在不稳定的互联网连接上使用 ssh。 ping some.doma.in PING some.doma.in (x.x.x.x): 56 data bytes Request timeou
This thread is a response to the titled question : Why ssh-agent doesn't forward my SSH certificate
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 8年前关闭。 Improve this q
因此,我在10.10.10.x子网中有一堆机器,基本上所有这些机器都以相同的方式配置。我将这些与我的10.10.11.x子网中的计算机区分开,后者具有不同的用途。 我希望能够输入“ssh 10.x”以
我不想在 vagrant box 上创建新的 SSH key 对,而是想重新使用主机上的 key 对,使用 agent forwarding 。我尝试过设置 config.ssh.forward_ag
我有一台服务器,可以通过ssh通过pubkey身份验证通过ssh访问: ssh "Host" -l "my identity" 这很好。但是,我无法在一个命令中使用我的私钥进行端口重定向和标识(这是我
创建一对 key 并通过 ssh-copy-id 将公钥发送到服务器后,我仍然无法在没有密码的情况下登录 ssh -v user@host 的输出 debug1:在/home/pumba/.ssh/k
我被困在 Permission denied (publickey) hell 中,试图将公钥复制到远程服务器,以便 Jenkins 可以在构建期间同步文件。 运行: sudo ssh-copy-id
我正在尝试通过我大学的代理服务器ssh到我们实验室的服务器之一。我们的目标是使用paramiko使其自动化,但我试图首先了解终端机级别中正在发生的事情。 我试过了 ssh -o ProxyComman
我在说明问题上有问题,我想通过SSH切换到我的Cisco交换机,但是我遇到了一些麻烦 我写了两个脚本, 有了这个我没有任何问题,我可以通过运行脚本并输入用户名和密码来更改默认网关: from Exsc
每个人 我目前正在使用一个集群,并且会经常使用“ssh”连接到它。有时我需要打开多个终端。因此,每次需要连接时,我都必须输入“ssh username@cluster.com”,然后输入“密码”(我在
我是一名优秀的程序员,十分优秀!