00001 //using System; 00002 //using Tamir.SharpSsh.jsch; 00003 //using System.IO; 00004 //using System.Windows.Forms; 00005 //using System.Text; 00006 //using System.Collections; 00007 // 00009 // * Scp.cs 00010 // * 00011 // * THIS SOURCE CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 00012 // * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 00013 // * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR 00014 // * PURPOSE. 00015 // * 00016 // * Copyright (C) 2005 Tamir Gal, tamirgal@myrealbox.com. 00017 // */ 00018 // 00019 //namespace Tamir.SharpSsh 00020 //{ 00021 // /// <summary> 00022 // /// Class for handling SCP file transfers over SSH connection. 00023 // /// </summary> 00024 // public class Scp 00025 // { 00026 // /// <summary> 00027 // /// Triggered when this SCP object starts connecting to the remote server. 00028 // /// </summary> 00029 // public event FileTansferEvent OnConnecting; 00030 // /// <summary> 00031 // /// Triggered when this SCP object starts the file transfer process. 00032 // /// </summary> 00033 // public event FileTansferEvent OnStart; 00034 // /// <summary> 00035 // /// Triggered when this SCP object ends the file transfer process. 00036 // /// </summary> 00037 // public event FileTansferEvent OnEnd; 00038 // /// <summary> 00039 // /// Triggered on every interval with the transfer progress iformation. 00040 // /// </summary> 00041 // public event FileTansferEvent OnProgress; 00042 // 00043 // /// <summary> 00044 // /// The default value of the progress update interval. 00045 // /// </summary> 00046 // private int m_interval = 250; 00047 // 00048 // /// <summary> 00049 // /// Copies a file from local machine to a remote SSH machine. 00050 // /// </summary> 00051 // /// <param name="localFile">The local file path.</param> 00052 // /// <param name="remoteHost">The remote machine's hostname or IP address</param> 00053 // /// <param name="remoteFile">The path of the remote file.</param> 00054 // /// <param name="user">The username for the connection.</param> 00055 // /// <param name="pass">The password for the connection.</param> 00056 // public void To(string localFile, string remoteHost,string remoteFile, string user, string pass) 00057 // { 00058 // Channel channel=null; 00059 // int filesize=0; 00060 // int copied=0; 00061 // try 00062 // { 00063 // double progress=0; 00064 // SendConnectingMessage("Connecting to "+remoteHost+"..."); 00065 // 00066 // JSch jsch=new JSch(); 00067 // Session session=jsch.getSession(user, remoteHost, 22); 00068 // session.setPassword( pass ); 00069 // 00070 // Hashtable config=new Hashtable(); 00071 // config.Add("StrictHostKeyChecking", "no"); 00072 // session.setConfig(config); 00073 // 00074 // session.connect(); 00075 // 00076 // // exec 'scp -t rfile' remotely 00077 // String command="scp -p -t \""+remoteFile+"\""; 00078 // channel=session.openChannel("exec"); 00079 // ((ChannelExec)channel).setCommand(command); 00080 // 00081 // // get I/O streams for remote scp 00082 // Stream outs=channel.getOutputStream(); 00083 // Stream ins=channel.getInputStream(); 00084 // 00085 // channel.connect(); 00086 // 00087 // SendStartMessage("Connected, starting transfer."); 00088 // 00089 // byte[] tmp=new byte[1]; 00090 // 00091 // if(checkAck(ins)!=0) 00092 // { 00093 // throw new Exception("Error openning communication channel."); 00094 // } 00095 // 00096 // // send "C0644 filesize filename", where filename should not include '/' 00097 // 00098 // filesize=(int)(new FileInfo(localFile)).Length; 00099 // command="C0644 "+filesize+" "; 00100 // if(localFile.LastIndexOf('/')>0) 00101 // { 00102 // command+=localFile.Substring(localFile.LastIndexOf('/')+1); 00103 // } 00104 // else 00105 // { 00106 // command+=localFile; 00107 // } 00108 // command+="\n"; 00109 // byte[] buff = Util.getBytes(command); 00110 // outs.Write(buff, 0, buff.Length); outs.Flush(); 00111 // 00112 // if(checkAck(ins)!=0) 00113 // { 00114 // throw new Exception("Error openning communication channel."); 00115 // } 00116 // 00117 // // send a content of lfile 00118 // SendProgressMessage(0, filesize, "Transferring..."); 00119 // FileStream fis=File.OpenRead(localFile); 00120 // byte[] buf=new byte[1024]; 00121 // copied = 0; 00122 // while(true) 00123 // { 00124 // int len=fis.Read(buf, 0, buf.Length); 00125 // if(len<=0) break; 00126 // outs.Write(buf, 0, len); outs.Flush(); 00127 // copied += len; 00128 // progress = (copied*100.0/filesize); 00129 // SendProgressMessage(copied, filesize, "Transferring..."); 00130 // } 00131 // fis.Close(); 00132 // 00133 // // send '\0' 00134 // buf[0]=0; outs.Write(buf, 0, 1); outs.Flush(); 00135 // 00136 // SendProgressMessage(copied, filesize, "Verifying transfer..."); 00137 // if(checkAck(ins)!=0) 00138 // { 00139 // throw new Exception("Unknow error during file transfer."); 00140 // } 00141 // SendEndMessage(copied, filesize, "Transfer completed successfuly ("+copied+" bytes)."); 00142 // try{channel.close();} 00143 // catch{} 00144 // } 00145 // catch(Exception e) 00146 // { 00147 // SendEndMessage(copied,filesize, "Transfer ended with an error."); 00148 // try{channel.close();} 00149 // catch{} 00150 // throw e; 00151 // } 00152 // } 00153 // 00154 // /// <summary> 00155 // /// Copies a file from a remote SSH machine to the local machine. 00156 // /// </summary> 00157 // /// <param name="remoteHost">The remote machine's hosname or IP address.</param> 00158 // /// <param name="remoteFile">The remote file path.</param> 00159 // /// <param name="user">The username or the connection.</param> 00160 // /// <param name="pass">The password for the connection.</param> 00161 // /// <param name="localFile">The local file path.</param> 00162 // public void From(string remoteHost,string remoteFile, string user, string pass, string localFile) 00163 // { 00164 // Channel channel=null; 00165 // int filesize=0; 00166 // int copied=0; 00167 // try 00168 // { 00169 // String prefix=null; 00170 // if(Directory.Exists(localFile)) 00171 // { 00172 // prefix=localFile+Path.DirectorySeparatorChar; 00173 // } 00174 // 00175 // double progress=0; 00176 // SendConnectingMessage("Connecting to "+remoteHost+"..."); 00177 // 00178 // JSch jsch=new JSch(); 00179 // Session session=jsch.getSession(user, remoteHost, 22); 00180 // session.setPassword( pass ); 00181 // 00182 // Hashtable config=new Hashtable(); 00183 // config.Add("StrictHostKeyChecking", "no"); 00184 // session.setConfig(config); 00185 // 00186 // session.connect(); 00187 // 00188 // // exec 'scp -f rfile' remotely 00189 // String command="scp -f \""+remoteFile + "\""; 00190 // channel=session.openChannel("exec"); 00191 // ((ChannelExec)channel).setCommand(command); 00192 // 00193 // // get I/O streams for remote scp 00194 // Stream outs=channel.getOutputStream(); 00195 // Stream ins=channel.getInputStream(); 00196 // 00197 // channel.connect(); 00198 // 00199 // SendStartMessage("Connected, starting transfer."); 00200 // 00201 // byte[] buf=new byte[1024]; 00202 // 00203 // // send '\0' 00204 // buf[0]=0; outs.Write(buf, 0, 1); outs.Flush(); 00205 // int c=checkAck(ins); 00206 // if(c!='C') 00207 // { 00208 // throw new Exception("Error openning communication channel."); 00209 // } 00210 // 00211 // // read '0644 ' 00212 // ins.Read(buf, 0, 5); 00213 // 00214 // filesize=0; 00215 // while(true) 00216 // { 00217 // ins.Read(buf, 0, 1); 00218 // if(buf[0]==' ')break; 00219 // filesize=filesize*10+(buf[0]-'0'); 00220 // } 00221 // 00222 // String file=null; 00223 // for(int i=0;;i++) 00224 // { 00225 // ins.Read(buf, i, 1); 00226 // if(buf[i]==(byte)0x0a) 00227 // { 00228 // file=Util.getString(buf, 0, i); 00229 // break; 00230 // } 00231 // } 00232 // 00233 // // send '\0' 00234 // buf[0]=0; outs.Write(buf, 0, 1); outs.Flush(); 00235 // 00236 // // read a content of lfile 00237 // FileStream fos=File.OpenWrite(prefix==null ? 00238 // localFile : 00239 // prefix+file); 00240 // int foo; 00241 // int size=filesize; 00242 // copied=0; 00243 // while(true) 00244 // { 00245 // if(buf.Length<filesize) foo=buf.Length; 00246 // else foo=filesize; 00247 // int len=ins.Read(buf, 0, foo); 00248 // copied += len; 00249 // fos.Write(buf, 0, foo); 00250 // progress += (len*100.0/size); 00251 // SendProgressMessage(copied, size, "Transferring..."); 00252 // filesize-=foo; 00253 // if(filesize==0) break; 00254 // } 00255 // fos.Close(); 00256 // 00257 // byte[] tmp=new byte[1]; 00258 // 00259 // SendProgressMessage(copied, size, "Verifying transfer..."); 00260 // if(checkAck(ins)!=0) 00261 // { 00262 // throw new Exception("Unknow error during file transfer."); 00263 // } 00264 // 00265 // // send '\0' 00266 // buf[0]=0; outs.Write(buf, 0, 1); outs.Flush(); 00267 // SendEndMessage(copied, size, "Transfer completed successfuly ("+copied+" bytes)."); 00268 // } 00269 // catch(Exception e) 00270 // { 00271 // SendEndMessage(copied,filesize, "Transfer ended with an error."); 00272 // try{channel.close();} 00273 // catch{} 00274 // throw e; 00275 // } 00276 // } 00277 // 00278 // private int checkAck(Stream ins) 00279 // { 00280 // int b=ins.ReadByte(); 00281 // // b may be 0 for success, 00282 // // 1 for error, 00283 // // 2 for fatal error, 00284 // // -1 00285 // if(b==0) return b; 00286 // if(b==-1) return b; 00287 // 00288 // if(b==1 || b==2) 00289 // { 00290 // StringBuilder sb=new StringBuilder(); 00291 // int c; 00292 // do 00293 // { 00294 // c=ins.ReadByte(); 00295 // sb.Append((char)c); 00296 // } 00297 // while(c!='\n'); 00298 // if(b==1) 00299 // { // error 00300 // throw new Exception(sb.ToString()); 00301 // } 00302 // if(b==2) 00303 // { // fatal error 00304 // throw new Exception(sb.ToString()); 00305 // } 00306 // } 00307 // return b; 00308 // } 00309 // 00310 // private void SendConnectingMessage(string msg) 00311 // { 00312 // if(OnConnecting != null) 00313 // OnConnecting(-1, 0, msg); 00314 // } 00315 // 00316 // private void SendStartMessage(string msg) 00317 // { 00318 // if(OnStart != null) 00319 // OnStart(-1, 0, msg); 00320 // } 00321 // 00322 // private void SendEndMessage(int transferredBytes, int totalBytes, string msg) 00323 // { 00324 // if(OnEnd != null) 00325 // OnEnd(transferredBytes, totalBytes, msg); 00326 // } 00327 // 00328 // DateTime lastUpdate = DateTime.Now; 00329 // private void SendProgressMessage(int transferredBytes, int totalBytes, string msg) 00330 // { 00331 // if(OnProgress != null) 00332 // { 00333 // TimeSpan diff = DateTime.Now-lastUpdate; 00334 // 00335 // if(diff.Milliseconds>ProgressUpdateInterval) 00336 // { 00337 // OnProgress(transferredBytes,totalBytes, msg); 00338 // lastUpdate=DateTime.Now; 00339 // } 00340 // } 00341 // } 00342 // 00343 // /// <summary> 00344 // /// Gets or sets the progress update interval in milliseconds 00345 // /// </summary> 00346 // public int ProgressUpdateInterval 00347 // { 00348 // get{return m_interval;} 00349 // set{m_interval=value;} 00350 // } 00351 // } 00352 // 00353 // public delegate void FileTansferEvent(int transferredBytes, int totalBytes, string message); 00354 //}
1.5.9