sharpssh远程linux监控系统,连接和获取使用SharpSSH远程Linux机器响应(用C锐利)

该博客内容涉及尝试通过C#代码使用SSH连接到远程Linux服务器并执行`ls`命令,但遇到了无法获取响应的问题。代码中使用了Tamir.SharpSsh库进行连接和交互,包括设置会话、打开通道、配置SSH选项等步骤,但最终未能成功读取命令的响应。
摘要由CSDN通过智能技术生成

我想连接到远程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());

}

}

}

}

+0

你可以张贴一些代码? –

2011-03-23 09:04:40

+0

这是我的代码:: –

2011-03-23 09:08:15

+0

你的代码? –

2011-03-23 10:25:00

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值