0

大家好,

我想连接到远程 Linux 机器并通过使用 SSH 在它们上执行命令来获得响应,请让我知道如何继续。截至目前,我能够连接但无法得到响应。

谢谢和问候, M.Channabasappa

我在这里粘贴我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tamir.SharpSsh;
using Tamir.SharpSsh.jsch;
using Tamir.Streams;
using Org.Mentalis.Security.Cryptography;

namespace ConsoleApplication7
{
    class SSHUtility
    {
        static void Main(string[] args)

            {
             SSHUtility sSHU = new SSHUtility();
            string command ="ls";
            try
            {
                JSch jsch = new JSch();

                jsch.setKnownHosts("a.z.y.x");
                Session session = jsch.getSession("robad", "d.z.y.x", 22);
                session.setPassword("robad");
                System.Collections.Hashtable hashConfig = new System.Collections.Hashtable();
                hashConfig.Add("StrictHostKeyChecking", "No");
                session.setConfig(hashConfig);
                session.getHostKey();


                //Channel channel = session.openChannel("ssh");
                //channel.connect();
                //ChannelSftp c = (ChannelSftp)channel;

                //c.exit();
                //new SSHUtility();
                //SshStream ssh = new SshStream("a.z.y.x", "robadaaa", "robadaaa");
                //ssh.Prompt = "ls";            //Remove terminal emulation characters            
                //ssh.RemoveTerminalEmulationCharacters = true;           

                //string response = ssh.ReadResponse();            
                //Console.Write(response);


                SshShell ssh = new SshShell("a.z.y.x", "robadaaaa", "robadaaaa");

                Console.WriteLine("Connecting.......");
                ssh.Connect();
                Console.WriteLine("OK");
                //Channel channel = session.openChannel("ssh");
                //channel.connect();
                ssh.Expect("the initial server prompt");
                ssh.WriteLine("ls");
                string resp = ssh.Expect("shell prompt");








                //ssh.Prompt = "#";
                //ssh.RemoveTerminalEmulationCharacters = true;
                //ssh.Write(command);
                //string response = ssh.ReadResponse();
                //Console.WriteLine("The response is : " + response);
            }
            catch (Exception ex)
            {
                Console.WriteLine("The Reason for Not Connecting is "+ex.ToString());
            }
            }

        }


        }
4

1 回答 1

1
SshExec exec = new SshExec("host","eri_sn_admin");
            exec.Password = "pass";
            //if (input.IdentityFile != null) exec.AddIdentityFile(input.IdentityFile);

            Console.Write("Connecting...");
            exec.Connect();
            Console.WriteLine("OK");
            while (true)
            {
                Console.Write("Enter a command to execute ['Enter' to cancel]: ");
                string command = "ls";
                if (command == "") break;
                string output = exec.RunCommand(command);
                Console.WriteLine(output);
            }
            Console.Write("Disconnecting...");
            exec.Close();
            Console.WriteLine("OK");
于 2011-07-20T09:25:26.827 回答