00001 using System.IO;
00002 using System.Collections;
00003 using Tamir.SharpSsh.jsch;
00004 using Tamir.Streams;
00005 using System.Text;
00006
00007 namespace Tamir
00008 {
00009 public class SshHelper
00010 {
00011
00012 private StreamReader reader;
00013 private PipedOutputStream writer_po;
00014 private Session session;
00015 private ChannelShell channel;
00016 private string host;
00017
00018 public SshHelper(string host, string username, string password)
00019 {
00020 this.host = host;
00021 JSch jsch=new JSch();
00022 Session session=jsch.getSession(username, host, 22);
00023 session.setPassword( password );
00024
00025 Hashtable config=new Hashtable();
00026 config.Add("StrictHostKeyChecking", "no");
00027 session.setConfig(config);
00028
00029 session.connect();
00030
00031 channel=(ChannelShell)session.openChannel("shell");
00032
00033 writer_po = new PipedOutputStream();
00034 PipedInputStream writer_pi = new PipedInputStream( writer_po );
00035
00036 PipedInputStream reader_pi = new PipedInputStream();
00037 PipedOutputStream reader_po = new PipedOutputStream( reader_pi );
00038 reader = new StreamReader (reader_pi,Encoding.UTF8);
00039
00040
00041 channel.setInputStream( writer_pi );
00042 channel.setOutputStream( reader_po );
00043
00044 channel.connect();
00045 channel.setPtySize(132, 132, 1024, 768);
00046
00047 }
00048
00049 public void Write(string data)
00050 {
00051 data += "\n";
00052 writer_po.write( Util.getBytes(data ));
00053 }
00054
00055 public string WriteAndRead(string data)
00056 {
00057 Write( data );
00058 return ReadResponse();
00059 }
00060
00061 public void SendCtrlC()
00062 {
00063 byte ASCII_CTRL_C = 3;
00064 byte[] ctrlc = { ASCII_CTRL_C };
00065 writer_po.write( ctrlc );
00066 }
00067
00068 public void SendCtrlZ()
00069 {
00070 byte ASCII_CTRL_Z = 26;
00071 byte[] ctrlz = { ASCII_CTRL_Z };
00072 writer_po.write( ctrlz );
00073 }
00074
00075 public string ReadResponse()
00076 {
00077
00078 return ReadTest();
00079 }
00080
00081 public string ReadBuffer(int size)
00082 {
00083 string res = "";
00084 int buff;
00085 try
00086 {
00087 buff = reader.Read();
00088 while( ((char)buff != '#') && (size != 0) )
00089 {
00090 res += (char)buff;
00091 size--;
00092 if (res.EndsWith("More--"))
00093 writer_po.write(Util.getBytes( " ") );
00094 buff = reader.Read();
00095 }
00096 if(buff != '\n')
00097 res += (char)buff;
00098 }
00099 catch
00100 {
00101 }
00102 res = RemoveJunk( res );
00103 return res;
00104 }
00105
00106 public void Close()
00107 {
00108 channel.disconnect();
00109 writer_po.close();
00110 reader.Close();
00111 }
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 private string ReadTest()
00138 {
00139 StringBuilder res = new StringBuilder();
00140 char[] buff = new char[1024];
00141 int count = 0;
00142
00143 try
00144 {
00145 count = reader.Read(buff, 0, buff.Length);
00146
00147 while(!Util.ArrayContains(buff, '#', count))
00148 {
00149
00150 res.Append( buff, 0, count );
00151 if ( (res.ToString().EndsWith("More--"))||(res.ToString().EndsWith("More--[m")))
00152 writer_po.write( Util.getBytes( " ") );
00153 count = reader.Read(buff, 0, buff.Length);
00154 count++;
00155 count--;
00156
00157 }
00158 if(buff[count-1] != '\n')
00159 res.Append( buff, 0, count );
00160 else
00161 res.Append( buff, 0, count-1 );
00162 }
00163 catch
00164 {
00165 }
00166 return RemoveJunk( res ).ToString();
00167 }
00168
00169 private static string RemoveJunk(string str)
00170 {
00171 string[] junk = new string[]{"[132;1H", "[24;1H", "[K", "[7m", "[m", " \b", "--More--", "\r", "\n "};
00172
00173 for( int i=0; i<junk.Length; i++ )
00174 {
00175 string match = junk[i];
00176 if (match=="\n ")
00177 str = Replace(str, match, " ");
00178 else
00179 str = Replace(str, match, "");
00180
00181 }
00182 return str;
00183 }
00184
00185 private static StringBuilder RemoveJunk(StringBuilder str)
00186 {
00187 string[] junk = new string[]{"[132;1H", "[24;1H", "[K", "[7m", "[m", " \b", "--More--", "\n "};
00188
00189 for( int i=0; i<junk.Length; i++ )
00190 {
00191 string match = junk[i];
00192 if (match=="\n ")
00193 str = Replace(str, match, " ");
00194 else
00195 str = Replace(str, match, "");
00196
00197 }
00198 return str;
00199 }
00200
00201 private static string Replace(string str, string toReplace, string replcaeWith)
00202 {
00203 int index = (str.IndexOf( toReplace ));
00204 while (index != -1)
00205 {
00206 string pre = str.Substring(0, index);
00207 string post = str.Substring(index+toReplace.Length, str.Length-index-toReplace.Length);
00208 str = pre + replcaeWith + post;
00209 index = (str.IndexOf( toReplace ));
00210 }
00211 return str;
00212 }
00213 private static StringBuilder Replace(StringBuilder str, string toReplace, string replcaeWith)
00214 {
00215 return str.Replace(toReplace, replcaeWith);
00216 }
00217
00218 }
00219 }
00220