大家好,
我想连接到远程 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());
}
}
}
}