This commit is contained in:
Mn
2023-11-16 18:25:38 +08:00
parent f12a0ff25c
commit 8382129a49
4 changed files with 270 additions and 64 deletions

View File

@@ -16,47 +16,109 @@ namespace NetPanel.Bl
private string _pwd = "";
private string _host = "";
private SshClient client = null;
/// <summary>
/// 客户端
/// </summary>
private SshClient _client = null;
/// <summary>
/// sell
/// </summary>
private ShellStream _shellStream = null;
/// <summary>
/// 读取流
/// </summary>
private StreamReader _reader = null;
/// <summary>
/// 写入流
/// </summary>
private StreamWriter _writer = null;
public SshBl(string host, string username, string pwd)
/// <summary>
/// 是否连接完成
/// </summary>
private bool _isConnected = false;
/// <summary>
/// ssh连接
/// </summary>
/// <param name="host">ip</param>
/// <param name="username">登录用户</param>
/// <param name="pwd">登录密码</param>
/// <param name="columns">单元格</param>
/// <param name="rows">行</param>
/// <param name="width">宽</param>
/// <param name="height">高</param>
public SshBl(string host, string username, string pwd, uint columns, uint rows, uint width, uint height)
{
_username = username;
_pwd = pwd;
_host = host;
client = new SshClient(host, username, pwd);
client.Connect();
_client = new SshClient(host, username, pwd);
_client.Connect();
if (_client.IsConnected)
{
_shellStream = _client.CreateShellStream("xterm", columns, rows, width, height, 1024);
_reader = new StreamReader(_shellStream);
_writer = new StreamWriter(_shellStream);
}
}
/// <summary>
/// 单条读取,可循环读取
/// </summary>
/// <returns></returns>
public StreamReader RunCommand(string command)
public string ReadLine()
{
var sshCommand = client.CreateCommand(command);
var asyncExecute = sshCommand.BeginExecute();
return new StreamReader(sshCommand.OutputStream);
//string outStr;
//while ((outStr = reader.ReadLine()) != null)
//{
//}
return _reader.ReadLine();
}
public string ServerVersion()
/// <summary>
/// 写入命令
/// </summary>
/// <returns></returns>
public void WriteLine(string line)
{
return client.ConnectionInfo.ServerVersion;
_writer.WriteLine(line);
}
/// <summary>
/// 全部读取,可循环读取
/// </summary>
/// <returns></returns>
public string ReadToEnd()
{
return _reader.ReadToEnd();
}
/// <summary>
/// Ssh 客户端
/// </summary>
public SshClient Client { get { return _client; } }
/// <summary>
/// 关闭
/// </summary>
public void Dispose()
{
client.Disconnect();
client.Dispose();
if (_writer != null) _writer.Close();
if (_reader != null) _reader.Close();
//if (_shellStream != null) _shellStream.Close();
if (_shellStream != null) _shellStream.Dispose();
if (_client != null) { _client.Disconnect(); _client.Dispose(); }
}
}
}