00001 using System;
00002 using System.IO;
00003 using System.Collections;
00004 using Tamir.SharpSsh.jsch;
00005 using Tamir.Streams;
00006 using System.Text;
00007 using System.Text.RegularExpressions;
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 namespace Tamir.SharpSsh
00021 {
00025 public class SshStream : Stream
00026 {
00027 private Stream m_in;
00028 private Stream m_out;
00029 private ChannelShell m_channel;
00030 private string m_host;
00031 private Regex m_prompt;
00032 private string m_escapeCharPattern;
00033 private bool m_removeTerminalChars = false;
00034 private Session m_session;
00035
00042 public SshStream(string host, string username, string password)
00043 {
00044 this.m_host = host;
00045 JSch jsch=new JSch();
00046 m_session=jsch.getSession(username, host, 22);
00047 m_session.setPassword( password );
00048
00049 Hashtable config=new Hashtable();
00050 config.Add("StrictHostKeyChecking", "no");
00051 m_session.setConfig(config);
00052
00053 m_session.connect();
00054 m_channel=(ChannelShell)m_session.openChannel("shell");
00055
00056 m_in = m_channel.getInputStream();
00057 m_out = m_channel.getOutputStream();
00058
00059 m_channel.connect();
00060 m_channel.setPtySize(80, 132, 1024, 768);
00061
00062 Prompt = "\n";
00063 m_escapeCharPattern = "\\[[0-9;?]*[^0-9;]";
00064 }
00065
00073 public override int Read(byte[] buffer, int offset, int count)
00074 {
00075 return m_in.Read(buffer, offset, count);
00076 }
00077
00083 public virtual int Read(byte[] buffer)
00084 {
00085 return Read(buffer, 0, buffer.Length);
00086 }
00087
00092 public override int ReadByte()
00093 {
00094 return m_in.ReadByte();
00095 }
00096
00101 public override void WriteByte(byte value)
00102 {
00103 m_out.WriteByte(value);
00104 }
00105
00112 public override void Write(byte[] buffer, int offset, int count)
00113 {
00114 m_out.Write(buffer, offset, count);
00115 }
00116
00121 public virtual void Write(byte[] buffer)
00122 {
00123 Write(buffer, 0, buffer.Length);
00124 }
00125
00130 public void Write(String data)
00131 {
00132 data += "\r";
00133 Write( Encoding.Default.GetBytes( data ) );
00134 Flush();
00135 }
00136
00140 public override void Close()
00141 {
00142 try
00143 {
00144 base.Close ();
00145 m_in.Close();
00146 m_out.Close();
00147 m_channel.close();
00148 m_channel.disconnect();
00149 m_session.disconnect();
00150 }
00151 catch{}
00152 }
00153
00157 public override bool CanRead
00158 {
00159 get
00160 {
00161 return m_in.CanRead;
00162 }
00163 }
00164
00168 public override bool CanWrite
00169 {
00170 get
00171 {
00172 return m_out.CanWrite;
00173 }
00174 }
00175
00179 public override bool CanSeek
00180 {
00181 get
00182 {
00183 return false;
00184 }
00185 }
00186
00190 public override void Flush()
00191 {
00192 m_out.Flush();
00193 }
00194
00198 public override long Length
00199 {
00200 get
00201 {
00202 return 0;
00203 }
00204 }
00205
00209 public override long Position
00210 {
00211 get
00212 {
00213 return 0;
00214 }
00215 set
00216 {
00217 }
00218 }
00219
00223 public override void SetLength(long value)
00224 {
00225 }
00226
00230 public override long Seek(long offset, SeekOrigin origin)
00231 {
00232 return 0;
00233 }
00234
00238 public string Prompt
00239 {
00240 get{return m_prompt.ToString();}
00241 set{m_prompt = new Regex(value, RegexOptions.Singleline);}
00242 }
00243
00247 public bool RemoveTerminalEmulationCharacters
00248 {
00249 get{return m_removeTerminalChars;}
00250 set{m_removeTerminalChars=value;}
00251 }
00252
00256 public string Cipher
00257 {
00258 get{return m_session.getCipher();}
00259 }
00260
00264 public string Mac
00265 {
00266 get{return m_session.getMac();}
00267 }
00268
00272 public string ServerVersion
00273 {
00274 get{return m_session.getServerVersion();}
00275 }
00276
00280 public string ClientVersion
00281 {
00282 get{return m_session.getClientVersion();}
00283 }
00284
00289 public string ReadResponse()
00290 {
00291 int readCount;
00292 StringBuilder resp = new StringBuilder();
00293 byte[] buff = new byte[1024];
00294 Match match;
00295
00296 do
00297 {
00298 readCount = this.Read(buff);
00299 resp.Append( System.Text.Encoding.Default.GetString( buff), 0, readCount);
00300 string s = resp.ToString();
00301 match = m_prompt.Match( s );
00302 }while(!match.Success);
00303
00304 return HandleTerminalChars( resp.ToString() );
00305 }
00306
00312 private string HandleTerminalChars(string str)
00313 {
00314 if(RemoveTerminalEmulationCharacters)
00315 {
00316 str = str.Replace("(B)0", "");
00317 str = Regex.Replace(str, m_escapeCharPattern, "");
00318 str = str.Replace(((char)15).ToString(), "");
00319 str = Regex.Replace(str, ((char)27).ToString()+"=*", "");
00320
00321 }
00322 return str;
00323 }
00324 }
00325 }