簡體   English   中英

從 C# 到 SSH 執行相關命令

[英]Executing Related Commands into SSH from C#

Im trying to execute consecutive commands on SSH from C# i have three commands subcommand1,subcommand2 and subcommand3.Despite successfully connecting to the destination server, i cant get results from the below through C#, this works when i run the commands on 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM