Connecting and getting Response from Remote Linux machine using SharpSSH(using C sharp)
HI all,
i want to connect to remote Linux machine and get response from those by executing command on them using SSH,please let me know how can i proceed. as of now i am able to connect but unable to get response.
Thanks & Regards, M.Channabasappa
Here i am pasting my code
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());
}
}
}
}
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");
精彩评论