BOM Byte Oder Marks
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Renci.SshNet;
namespace SSHClient
{
public enum SSHCLIENT_STATUS {Faild,Redy,Connected,Active,Disconnect}
public class SSHClient
{
protected SshClient _sshClient;
protected ShellStream _shellStream;
public string strException { get; protected set;}
public SSHCLIENT_STATUS STATUS { get;protected set;}
public Encoding ClientEncoding { get;protected set;}
public TimeSpan ClientTimeOut { get;protected set;}
public StreamReader ShStrmReader { get; protected set;}
public StreamWriter ShStrmWriter { get; protected set;}
public SSHClient(string host, string Account, string Passwd, int port, string strEncName)
{
ClientEncoding = Encoding.GetEncoding(strEncName);
ClientTimeOut = new TimeSpan(0,0,2);
STATUS = Connect(host,Account,Passwd,port);
if(STATUS == SSHCLIENT_STATUS.Connected){
STATUS = CreatStreams();
if(STATUS == SSHCLIENT_STATUS.Active){
ShStrmWriter.AutoFlush = true;
}
}
}
private SSHCLIENT_STATUS Connect(
string strHost,
string strLoginID,
string strPasswd,
int port)
{
SSHCLIENT_STATUS res = SSHCLIENT_STATUS.Faild;
try{
ConnectionInfo ConnInfo = new ConnectionInfo(
strHost,
port,
strLoginID,
new AuthenticationMethod[]{ new PasswordAuthenticationMethod(strLoginID, strPasswd), });
_sshClient = new SshClient(ConnInfo);
_sshClient.ConnectionInfo.Timeout = ClientTimeOut;
_sshClient.Connect();
if(_sshClient.IsConnected){
_shellStream = _sshClient.CreateShellStream("CiscoTer",
0,0,0,0,0);
res = SSHCLIENT_STATUS.Connected;
}
}catch(Exception ex){
res = SSHCLIENT_STATUS.Disconnect;
strException += ex.ToString();
}
return res;
}
private SSHCLIENT_STATUS CreatStreams()
{
SSHCLIENT_STATUS res = SSHCLIENT_STATUS.Faild;
try{
ShStrmReader = new StreamReader(_shellStream,ClientEncoding);
// UTF BOMs Mark \357\273\277
var enc = new UTF8Encoding(false);
ShStrmWriter = new StreamWriter(_shellStream,enc);
// ShStrmWriter = new StreamWriter(_shellStream,false,ClientEncoding);
res = SSHCLIENT_STATUS.Active;
}catch (Exception ex)
{
res = SSHCLIENT_STATUS.Redy;
strException += ex.ToString();
}
return res;
}
public SSHCLIENT_STATUS Disconnect()
{
SSHCLIENT_STATUS res = SSHCLIENT_STATUS.Faild;
try{
ShStrmReader?.Dispose();
ShStrmWriter?.Dispose();
_shellStream?.Dispose();
_sshClient?.Disconnect();
_sshClient?.Dispose();
ShStrmReader = null;
ShStrmWriter = null;
_shellStream = null;
_sshClient = null;
res = SSHCLIENT_STATUS.Disconnect;
}catch(Exception ex){
res = SSHCLIENT_STATUS.Active;
strException += ex.ToString();
}
return res;
}
public async Task<Boolean> DoCommand(string cmd)
{
Boolean res = true;
try{
await ShStrmWriter.WriteLineAsync(cmd);
}catch(Exception ex){
res = false;
strException += ex.ToString();
}
return res;
}
public async Task<string> DoneResult()
{
string res = "";
try{
for(;;){
var getRes = await ShStrmReader.ReadLineAsync();
if (getRes == null) break;
res += getRes + "\n";
}
}catch(Exception ex){
res = null;
strException += ex.ToString();
}
return res;
}
}
}