C
C#16mo ago
valentimarco

❔ SSH.Net trying to build a interactive shell

I am trying to build a ssh terminal in C#, but i am not able to output text from the streams (the code is a melting pot of pieces, and currently following a similar project): I notice that i need to write something back to able to "refresh" the output stream. CODE in comments
8 Replies
valentimarco
valentimarcoOP16mo ago
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();
}


}
(The environment runs in local network)
valentimarco
valentimarcoOP16mo ago
when i don't write anything in the "input shell" stream
Omnissiah
Omnissiah16mo ago
there is some mess in this code if you are using _reader like that you wouldn't really need it, since _shellStream already has ReadAsync (also im pretty sure vs is giving you some hints on that to use a different overload) then when write you are not using async version and then you are using Thread.Sleep instead of Task.Delay also i would not call CreateShellStresm in a constructor because it's sending data, so it could throw exceptions (it's not great, i know) having said this, i tried to use ssh.net in the beginning and was not satisfied so then i tried to use plink instead then tried ssh.net and it started working for me, tho' i admit it's not the best code i have seen
Atronil
Atronil16mo ago
take a look for this github project https://github.com/AMuhaimin/sshTerminal
GitHub
GitHub - AMuhaimin/sshTerminal: Simple SSH Terminal dari c#
Simple SSH Terminal dari c#. Contribute to AMuhaimin/sshTerminal development by creating an account on GitHub.
Omnissiah
Omnissiah16mo ago
why on earth there is a nupkg committed in the folder
Atronil
Atronil16mo ago
i dont know )
valentimarco
valentimarcoOP16mo ago
i copied some code from others xd
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server