//using System; //using Tamir.SharpSsh.jsch; //using System.IO; //using System.Windows.Forms; //using System.Text; //using System.Collections; // ///* // * Scp.cs // * // * THIS SOURCE CODE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY // * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE // * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR // * PURPOSE. // * // * Copyright (C) 2005 Tamir Gal, tamirgal@myrealbox.com. // */ // //namespace Tamir.SharpSsh //{ // /// // /// Class for handling SCP file transfers over SSH connection. // /// // public class Scp // { // /// // /// Triggered when this SCP object starts connecting to the remote server. // /// // public event FileTansferEvent OnConnecting; // /// // /// Triggered when this SCP object starts the file transfer process. // /// // public event FileTansferEvent OnStart; // /// // /// Triggered when this SCP object ends the file transfer process. // /// // public event FileTansferEvent OnEnd; // /// // /// Triggered on every interval with the transfer progress iformation. // /// // public event FileTansferEvent OnProgress; // // /// // /// The default value of the progress update interval. // /// // private int m_interval = 250; // // /// // /// Copies a file from local machine to a remote SSH machine. // /// // /// The local file path. // /// The remote machine's hostname or IP address // /// The path of the remote file. // /// The username for the connection. // /// The password for the connection. // public void To(string localFile, string remoteHost,string remoteFile, string user, string pass) // { // Channel channel=null; // int filesize=0; // int copied=0; // try // { // double progress=0; // SendConnectingMessage("Connecting to "+remoteHost+"..."); // // JSch jsch=new JSch(); // Session session=jsch.getSession(user, remoteHost, 22); // session.setPassword( pass ); // // Hashtable config=new Hashtable(); // config.Add("StrictHostKeyChecking", "no"); // session.setConfig(config); // // session.connect(); // // // exec 'scp -t rfile' remotely // String command="scp -p -t \""+remoteFile+"\""; // channel=session.openChannel("exec"); // ((ChannelExec)channel).setCommand(command); // // // get I/O streams for remote scp // Stream outs=channel.getOutputStream(); // Stream ins=channel.getInputStream(); // // channel.connect(); // // SendStartMessage("Connected, starting transfer."); // // byte[] tmp=new byte[1]; // // if(checkAck(ins)!=0) // { // throw new Exception("Error openning communication channel."); // } // // // send "C0644 filesize filename", where filename should not include '/' // // filesize=(int)(new FileInfo(localFile)).Length; // command="C0644 "+filesize+" "; // if(localFile.LastIndexOf('/')>0) // { // command+=localFile.Substring(localFile.LastIndexOf('/')+1); // } // else // { // command+=localFile; // } // command+="\n"; // byte[] buff = Util.getBytes(command); // outs.Write(buff, 0, buff.Length); outs.Flush(); // // if(checkAck(ins)!=0) // { // throw new Exception("Error openning communication channel."); // } // // // send a content of lfile // SendProgressMessage(0, filesize, "Transferring..."); // FileStream fis=File.OpenRead(localFile); // byte[] buf=new byte[1024]; // copied = 0; // while(true) // { // int len=fis.Read(buf, 0, buf.Length); // if(len<=0) break; // outs.Write(buf, 0, len); outs.Flush(); // copied += len; // progress = (copied*100.0/filesize); // SendProgressMessage(copied, filesize, "Transferring..."); // } // fis.Close(); // // // send '\0' // buf[0]=0; outs.Write(buf, 0, 1); outs.Flush(); // // SendProgressMessage(copied, filesize, "Verifying transfer..."); // if(checkAck(ins)!=0) // { // throw new Exception("Unknow error during file transfer."); // } // SendEndMessage(copied, filesize, "Transfer completed successfuly ("+copied+" bytes)."); // try{channel.close();} // catch{} // } // catch(Exception e) // { // SendEndMessage(copied,filesize, "Transfer ended with an error."); // try{channel.close();} // catch{} // throw e; // } // } // // /// // /// Copies a file from a remote SSH machine to the local machine. // /// // /// The remote machine's hosname or IP address. // /// The remote file path. // /// The username or the connection. // /// The password for the connection. // /// The local file path. // public void From(string remoteHost,string remoteFile, string user, string pass, string localFile) // { // Channel channel=null; // int filesize=0; // int copied=0; // try // { // String prefix=null; // if(Directory.Exists(localFile)) // { // prefix=localFile+Path.DirectorySeparatorChar; // } // // double progress=0; // SendConnectingMessage("Connecting to "+remoteHost+"..."); // // JSch jsch=new JSch(); // Session session=jsch.getSession(user, remoteHost, 22); // session.setPassword( pass ); // // Hashtable config=new Hashtable(); // config.Add("StrictHostKeyChecking", "no"); // session.setConfig(config); // // session.connect(); // // // exec 'scp -f rfile' remotely // String command="scp -f \""+remoteFile + "\""; // channel=session.openChannel("exec"); // ((ChannelExec)channel).setCommand(command); // // // get I/O streams for remote scp // Stream outs=channel.getOutputStream(); // Stream ins=channel.getInputStream(); // // channel.connect(); // // SendStartMessage("Connected, starting transfer."); // // byte[] buf=new byte[1024]; // // // send '\0' // buf[0]=0; outs.Write(buf, 0, 1); outs.Flush(); // int c=checkAck(ins); // if(c!='C') // { // throw new Exception("Error openning communication channel."); // } // // // read '0644 ' // ins.Read(buf, 0, 5); // // filesize=0; // while(true) // { // ins.Read(buf, 0, 1); // if(buf[0]==' ')break; // filesize=filesize*10+(buf[0]-'0'); // } // // String file=null; // for(int i=0;;i++) // { // ins.Read(buf, i, 1); // if(buf[i]==(byte)0x0a) // { // file=Util.getString(buf, 0, i); // break; // } // } // // // send '\0' // buf[0]=0; outs.Write(buf, 0, 1); outs.Flush(); // // // read a content of lfile // FileStream fos=File.OpenWrite(prefix==null ? // localFile : // prefix+file); // int foo; // int size=filesize; // copied=0; // while(true) // { // if(buf.LengthProgressUpdateInterval) // { // OnProgress(transferredBytes,totalBytes, msg); // lastUpdate=DateTime.Now; // } // } // } // // /// // /// Gets or sets the progress update interval in milliseconds // /// // public int ProgressUpdateInterval // { // get{return m_interval;} // set{m_interval=value;} // } // } // // public delegate void FileTansferEvent(int transferredBytes, int totalBytes, string message); //}