c# - 从 C# 将相关命令执行到 SSH
问题描述
我试图从 C# 在 SSH 上执行连续命令我有三个命令 subcommand1、subcommand2 和 subcommand3。尽管成功连接到目标服务器,但我无法通过 C# 从下面获得结果,这在我在 putty 上运行命令时有效。我的主要目标是将文件从 oracle 服务器 140.X.XX.3 下载到 IP 上的本地 lynux 服务器,如下面的 MYIPADDRESS 所示。我做错了什么?
'using System;
using Renci.SshNet;
using Renci.SshNet.Common;
using System.Diagnostics;
namespace SFTP_SHH
{
class Program
{
static void Main(string[] args)
{
//Connection information
string user = "username";
string pass = "password";
string host = "MYIPADDRESS ";
string gainAcces = "sftp -o IdentityFile=/export/home/oracle/.ssh/rsps_rsa user@140.XX.XX.X;";
//Set up the SSH connection
SshClient sshclient = new SshClient(host, user, pass);
sshclient.Connect();
string subcommand1 = gainAcces;
string subcommand2 = "cd download";
string subcommand3 = "get 2020-07-22_RESPONSYS_CRM_OUTGOING_CALLS2.csv.zip";
SshCommand sc1 = sshclient.CreateCommand(subcommand1 && subcommand2 && subcommand3);
sc1.Execute();
}
}
}
'
解决方案
您可以使用;
将命令分成三个,
SshCommand sc1 = sshclient.CreateCommand(subcommand1 + ";" + subcommand2 + ";" + subcommand3);
您正在做的是&&
在三个字符串之间使用 (AND) 运算符...这与您的想法不太一样。您可以将 && 运算符用作三个命令的一部分,方法是将其添加为字符串...而不是 c# 运算符(可能不适用于所有 linux 系统),
SshCommand sc1 = sshclient.CreateCommand(subcommand1 + "&&" + subcommand2 + "&&" + subcommand3);
推荐阅读
- xml - 工作表中的 XML 文件“导入的内容为空”错误
- javascript - 如何在 socket.io 中获取所有连接的客户端
- .net - HttpWebRequest.ProtocolVersion - 如何将其设置为尝试 1.0,然后是 1.1(反之亦然)?
- visual-c++ - CTab_Ctrl 类是否需要附加属性才能在其“窗口”中绘制?
- python - 你可以在pygame中重复打印一个矩形的整个长度的图像吗?
- c++ - 阅读其他帖子后未定义对 vtable 问题的引用
- javascript - Gatsby GDPR Cookie Banner 如何实现多个 cookie + 禁用跟踪?
- android - Android Studios 应用登录失败,应用崩溃
- ios - 在核心数据中如何保存字典或数组,如字符串数据?
- data-warehouse - 作为事实表子集的维度有什么用?