125 lines
3.1 KiB
C#
125 lines
3.1 KiB
C#
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 = "";
|
|
|
|
/// <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;
|
|
|
|
/// <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();
|
|
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 string ReadLine()
|
|
{
|
|
return _reader.ReadLine();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 写入命令
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
|
|
public void WriteLine(string line)
|
|
{
|
|
_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()
|
|
{
|
|
|
|
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(); }
|
|
|
|
}
|
|
}
|
|
}
|