valentimarco
❔ SSH.Net trying to build a interactive shell
main:
var connection = new SSHConnection(host, username, password);
string text = await connection.ReadStream();
Console.WriteLine(text);
connection.WriteStream("ls");
text = await connection.ReadStream();
Console.WriteLine(text);
Console.ReadLine();
classes:
class SSHConnection : IDisposable{
private string _host;
private string _username;
private string _password;
private StreamWriter _writer;
private StreamReader _reader;
//private MemoryStream _memory = new ();
private String _lastcommand;
private SshClient _client;
private ShellStream _shellStream;
public SSHConnection(string host, string username, string password){
_client = new SshClient(host, username, password);
_client.Connect();
if(!_client.IsConnected) throw new Exception("Client not connected.");
var terminalModes = new Dictionary<TerminalModes, uint>();
terminalModes[TerminalModes.TTY_OP_ISPEED] = 0x00009600;
terminalModes[TerminalModes.TTY_OP_OSPEED] = 0x00009600;
_shellStream = _client.CreateShellStream("xterm", 80, 24, 800, 600, 1024,terminalModes);
_writer = new StreamWriter(_shellStream) {AutoFlush = true};
_reader = new StreamReader(_shellStream);
}
public async Task<string> ReadStream(){
var buffer = new char[4096];
var read = await _reader.ReadAsync(buffer, 0, buffer.Length);
var input = new string(buffer, 0, read);
return input;
}
public void WriteStream(string command){
while (_shellStream.Length == 0)
Thread.Sleep(500);
_writer.WriteLine(command);
_writer.Flush();
_lastcommand = command;
}
public void Dispose(){
_writer.Dispose();
_reader.Dispose();
_client.Disconnect();
_client.Dispose();
}
}
main:
var connection = new SSHConnection(host, username, password);
string text = await connection.ReadStream();
Console.WriteLine(text);
connection.WriteStream("ls");
text = await connection.ReadStream();
Console.WriteLine(text);
Console.ReadLine();
classes:
class SSHConnection : IDisposable{
private string _host;
private string _username;
private string _password;
private StreamWriter _writer;
private StreamReader _reader;
//private MemoryStream _memory = new ();
private String _lastcommand;
private SshClient _client;
private ShellStream _shellStream;
public SSHConnection(string host, string username, string password){
_client = new SshClient(host, username, password);
_client.Connect();
if(!_client.IsConnected) throw new Exception("Client not connected.");
var terminalModes = new Dictionary<TerminalModes, uint>();
terminalModes[TerminalModes.TTY_OP_ISPEED] = 0x00009600;
terminalModes[TerminalModes.TTY_OP_OSPEED] = 0x00009600;
_shellStream = _client.CreateShellStream("xterm", 80, 24, 800, 600, 1024,terminalModes);
_writer = new StreamWriter(_shellStream) {AutoFlush = true};
_reader = new StreamReader(_shellStream);
}
public async Task<string> ReadStream(){
var buffer = new char[4096];
var read = await _reader.ReadAsync(buffer, 0, buffer.Length);
var input = new string(buffer, 0, read);
return input;
}
public void WriteStream(string command){
while (_shellStream.Length == 0)
Thread.Sleep(500);
_writer.WriteLine(command);
_writer.Flush();
_lastcommand = command;
}
public void Dispose(){
_writer.Dispose();
_reader.Dispose();
_client.Disconnect();
_client.Dispose();
}
}
10 replies