using Renci.SshNet; using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Metadata.Ecma335; using System.Reflection.PortableExecutable; using System.Text; using System.Threading.Tasks; namespace NetPanel.Bl { public class SshBl { private string _username = ""; private string _pwd = ""; private string _host = ""; /// /// 客户端 /// private SshClient _client = null; /// /// sell /// private ShellStream _shellStream = null; /// /// 读取流 /// private StreamReader _reader = null; /// /// 写入流 /// private StreamWriter _writer = null; /// /// 是否连接完成 /// private bool _isConnected = false; /// /// ssh连接 /// /// ip /// 登录用户 /// 登录密码 /// 单元格 /// 行 /// 宽 /// 高 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(); if (_client.IsConnected) { _shellStream = _client.CreateShellStream("xterm", columns, rows, width, height, 1024); _reader = new StreamReader(_shellStream); _writer = new StreamWriter(_shellStream); } } /// /// 单条读取,可循环读取 /// /// public string ReadLine() { return _reader.ReadLine(); } /// /// 写入命令 /// /// public void WriteLine(string line) { _writer.WriteLine(line); } /// /// 全部读取,可循环读取 /// /// public string ReadToEnd() { return _reader.ReadToEnd(); } /// /// Ssh 客户端 /// public SshClient Client { get { return _client; } } /// /// 关闭 /// public void 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(); } } } }