00001 using System;
00002 using System.Collections;
00003 using Tamir.SharpSsh.jsch;
00004
00005
00006
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 namespace Tamir.SharpSsh
00036 {
00040 public abstract class SshBase
00041 {
00042 protected string m_host;
00043 protected string m_user;
00044 protected string m_pass;
00045 protected JSch m_jsch;
00046 protected Session m_session;
00047 protected Channel m_channel;
00048
00052 private static int SSH_TCP_PORT = 22;
00053
00060 public SshBase(string sftpHost, string user, string password)
00061 {
00062 this.m_host = sftpHost;
00063 this.m_user = user;
00064 this.Password = password;
00065 m_jsch = new JSch();
00066 }
00067
00073 public SshBase(string sftpHost, string user)
00074 : this(sftpHost, user, null)
00075 {
00076 }
00077
00082 public virtual void AddIdentityFile(string privateKeyFile)
00083 {
00084 m_jsch.addIdentity(privateKeyFile);
00085 }
00086
00092 public virtual void AddIdentityFile(string privateKeyFile, string passphrase)
00093 {
00094 m_jsch.addIdentity(privateKeyFile, passphrase);
00095 }
00096
00097 protected abstract string ChannelType{get;}
00098
00102 public virtual void Connect()
00103 {
00104 this.Connect(SSH_TCP_PORT);
00105 }
00106
00111 public virtual void Connect(int tcpPort)
00112 {
00113 this.ConnectSession(tcpPort);
00114 this.ConnectChannel();
00115 }
00116
00117 protected virtual void ConnectSession(int tcpPort)
00118 {
00119 m_session = m_jsch.getSession(m_user, m_host, tcpPort);
00120 if (Password != null)
00121 m_session.setUserInfo(new KeyboardInteractiveUserInfo(Password));
00122 Hashtable config = new Hashtable();
00123 config.Add("StrictHostKeyChecking", "no");
00124 m_session.setConfig(config);
00125 m_session.connect();
00126 }
00127
00128 protected virtual void ConnectChannel()
00129 {
00130 m_channel = m_session.openChannel(ChannelType);
00131 this.OnChannelReceived();
00132 m_channel.connect();
00133 this.OnConnected();
00134 }
00135
00136 protected virtual void OnConnected()
00137 {
00138 }
00139
00140 protected virtual void OnChannelReceived()
00141 {
00142 }
00143
00147 public virtual void Close()
00148 {
00149 if (m_channel != null)
00150 {
00151 m_channel.disconnect();
00152 m_channel = null;
00153 }
00154 if (m_session != null)
00155 {
00156 m_session.disconnect();
00157 m_session = null;
00158 }
00159 }
00160
00164 public virtual bool Connected
00165 {
00166 get
00167 {
00168 if (m_session != null)
00169 return m_session.isConnected();
00170 return false;
00171 }
00172 }
00173
00177 public string Cipher
00178 {
00179 get
00180 {
00181 CheckConnected();
00182 return m_session.getCipher();
00183 }
00184 }
00185
00189 public string Mac
00190 {
00191 get
00192 {
00193 CheckConnected();
00194 return m_session.getMac();
00195 }
00196 }
00197
00201 public string ServerVersion
00202 {
00203 get
00204 {
00205 CheckConnected();
00206 return m_session.getServerVersion();
00207 }
00208 }
00209
00213 public string ClientVersion
00214 {
00215 get
00216 {
00217 CheckConnected();
00218 return m_session.getClientVersion();
00219 }
00220 }
00221
00222 public string Host
00223 {
00224 get
00225 {
00226 CheckConnected();
00227 return m_session.getHost();
00228 }
00229 }
00230
00231 public HostKey HostKey
00232 {
00233 get
00234 {
00235 CheckConnected();
00236 return m_session.getHostKey();
00237 }
00238 }
00239
00240 public int Port
00241 {
00242 get
00243 {
00244 CheckConnected();
00245 return m_session.getPort();
00246 }
00247 }
00248
00252 public string Password
00253 {
00254 get { return m_pass; }
00255 set { m_pass = value; }
00256 }
00257 public string Username
00258 {
00259 get { return m_user; }
00260 }
00261
00262 public static Version Version
00263 {
00264 get
00265 {
00266 System.Reflection.Assembly asm
00267 = System.Reflection.Assembly.GetAssembly(typeof(Tamir.SharpSsh.SshBase));
00268 return asm.GetName().Version;
00269 }
00270 }
00271
00272 private void CheckConnected()
00273 {
00274 if(!Connected)
00275 {
00276 throw new Exception("SSH session is not connected.");
00277 }
00278 }
00279
00283 protected class KeyboardInteractiveUserInfo : UserInfo, UIKeyboardInteractive
00284 {
00285 string _password;
00286
00287 public KeyboardInteractiveUserInfo(string password)
00288 {
00289 _password = password;
00290 }
00291
00292 #region UIKeyboardInteractive Members
00293
00294 public string[] promptKeyboardInteractive(string destination, string name, string instruction, string[] prompt, bool[] echo)
00295 {
00296 return new string[] { _password };
00297 }
00298
00299 #endregion
00300
00301 #region UserInfo Members
00302
00303 public bool promptYesNo(string message)
00304 {
00305 return true;
00306 }
00307
00308 public bool promptPassword(string message)
00309 {
00310 return true;
00311 }
00312
00313 public string getPassword()
00314 {
00315 return _password;
00316 }
00317
00318 public bool promptPassphrase(string message)
00319 {
00320 return true;
00321 }
00322
00323 public string getPassphrase()
00324 {
00325 return null;
00326 }
00327
00328 public void showMessage(string message)
00329 {
00330 }
00331
00332 #endregion
00333 }
00334 }
00335 }