00001 using System;
00002 using System.IO;
00003 using Tamir.SharpSsh.jsch;
00004 using Tamir.Streams;
00005 using System.Text;
00006 using System.Text.RegularExpressions;
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 namespace Tamir.SharpSsh
00039 {
00043 public class SshShell : SshBase
00044 {
00045 private Stream m_sshIO = null;
00046 private Regex m_expectPattern;
00047 private bool m_removeTerminalChars = false;
00048 private bool m_redirectToConsole = false;
00049 private static string escapeCharsPattern = "\\[[0-9;?]*[^0-9;]";
00050
00051 public SshShell(string host, string user, string password)
00052 : base(host, user, password)
00053 {
00054 Init();
00055 }
00056
00057 public SshShell(string host, string user)
00058 : base(host, user)
00059 {
00060 Init();
00061 }
00062
00063 protected void Init()
00064 {
00065 ExpectPattern = "";
00066 m_removeTerminalChars = false;
00067 }
00068
00069 protected override void OnChannelReceived()
00070 {
00071 base.OnChannelReceived ();
00072 if(m_redirectToConsole)
00073 {
00074 SetStream(Console.OpenStandardInput(), Console.OpenStandardOutput());
00075 }
00076 else
00077 {
00078 m_sshIO = GetStream();
00079 }
00080 }
00081
00082
00083 protected override string ChannelType
00084 {
00085 get { return "shell"; }
00086 }
00087
00088 public Stream IO
00089 {
00090 get
00091 {
00092
00093
00094
00095
00096 return m_sshIO;
00097 }
00098 }
00099
00100 public void WriteLine(string data)
00101 {
00102 Write( data+"\r" );
00103 }
00104
00105 public void Write(string data)
00106 {
00107 Write( Encoding.Default.GetBytes( data ) );
00108 }
00109
00114 public virtual void Write(byte[] buffer)
00115 {
00116 Write(buffer, 0, buffer.Length);
00117 }
00118
00119
00126 public virtual void Write(byte[] buffer, int offset, int count)
00127 {
00128 IO.Write(buffer, offset, count);
00129 IO.Flush();
00130 }
00131
00135 public Stream GetStream()
00136 {
00137 return new CombinedStream(m_channel.getInputStream(), m_channel.getOutputStream());;
00138 }
00139
00140 public void SetStream(Stream inputStream, Stream outputStream)
00141 {
00142 m_channel.setInputStream(inputStream);
00143 m_channel.setOutputStream(outputStream);
00144 }
00145
00146 public void SetStream(Stream s)
00147 {
00148 SetStream(s, s);
00149 }
00150
00151 public void RedirectToConsole()
00152 {
00153 m_redirectToConsole = true;
00154 }
00155
00156 public virtual bool ShellOpened
00157 {
00158 get
00159 {
00160 if(m_channel != null)
00161 {
00162 return !m_channel.isClosed();
00163 }
00164 return false;
00165 }
00166 }
00167
00168 public virtual bool ShellConnected
00169 {
00170 get
00171 {
00172 if(m_channel != null)
00173 {
00174 return m_channel.isConnected();
00175 }
00176 return false;
00177 }
00178 }
00179
00183 public bool RemoveTerminalEmulationCharacters
00184 {
00185 get{return m_removeTerminalChars;}
00186 set{m_removeTerminalChars=value;}
00187 }
00188
00192 public string ExpectPattern
00193 {
00194 get{return m_expectPattern.ToString();}
00195 set{m_expectPattern = new Regex(value, RegexOptions.Singleline);}
00196 }
00197
00202 public string Expect()
00203 {
00204 return Expect(m_expectPattern);
00205 }
00206
00211 public string Expect(string pattern)
00212 {
00213 return Expect( new Regex( pattern, RegexOptions.Singleline ));
00214 }
00215
00220 public string Expect(Regex pattern)
00221 {
00222 int readCount;
00223 StringBuilder resp = new StringBuilder();
00224 byte[] buff = new byte[1024];
00225 Match match;
00226
00227 do
00228 {
00229 readCount = IO.Read(buff, 0, buff.Length);
00230 if(readCount == -1) break;
00231 string tmp = System.Text.Encoding.Default.GetString(buff, 0, readCount);
00232 resp.Append( tmp, 0, readCount);
00233 string s = resp.ToString();
00234 match = pattern.Match( s );
00235 }while(!match.Success);
00236
00237 string result = resp.ToString();
00238 if(RemoveTerminalEmulationCharacters)
00239 result = HandleTerminalChars(result);
00240 return result;
00241 }
00242
00246 public static string HandleTerminalChars(string str)
00247 {
00248 str = str.Replace("(B)0", "");
00249 str = Regex.Replace(str, escapeCharsPattern, "");
00250 str = str.Replace(((char)15).ToString(), "");
00251 str = Regex.Replace(str, ((char)27)+"=*", "");
00252
00253 return str;
00254 }
00255 }
00256 }