public bool GetFiles() { bool status = true; Sftp sftp = new Sftp( m_sftpSite, m_sftpSiteUserName ); try { // add our private key to connect // put these in the config file sftp.AddIdentityFile(@"d:\apps\RSAKeys\opensshkey"); // connect sftp.Connect(); // get a directory list ArrayList rFiles = sftp.GetFileList( m_sftpSiteRemoteFolder ); foreach ( string file in rFiles ) { if (file.Equals(".") || file.Equals("..")) continue; // get the file and put in the watch folder to be processed sftp.Get(m_sftpSiteRemoteFolder + file, m_moveToProcessFolder + file); // update our database that we have downloaded the file from the server // this.updateDb( file ); // delete the file on the remote server after pulling it over // sftp.Delete(f); } sftp.Close(); status = true; } catch (Tamir.SharpSsh.jsch.SftpException se) { LogMsg("NEXCEPTION:SftpMgr::GetFile():ECaught:" + se.Message); } catch (Tamir.SharpSsh.jsch.JSchException jse) { LogMsg("NEXCEPTION:SftpMgr::GetFile():ECaught:" + jse.Message); } catch (Tamir.SharpSsh.SshTransferException ste) { LogMsg("NEXCEPTION:SftpMgr::GetFile():ECaught:" + ste.Message); } catch (SystemException se) { LogMsg("NEXCEPTION:SftpMgr::GetFile():ECaught:" + se.Message); } return status; }
private bool GetFiles() { bool status = true; Sftp sftp = new Sftp( m_sftpSite, m_sftpSiteUserName ); try { // add our private key to connect // put these in the config file sftp.AddIdentityFile( m_identityFile ); // connect sftp.Connect(); // get a directory list ArrayList rFiles = sftp.GetFileList( m_sftpSiteRemoteFolder ); int indx = 0; foreach ( string file in rFiles ) { indx++; if (file.Equals(".") || file.Equals("..")) continue; if ( this.CheckDb(file) ) continue; try { // get the file and put in the watch folder to be processed sftp.Get(m_sftpSiteRemoteFolder + file, m_moveToProcessFolder + file); // update the database to indicate file has been downloaded this.UpdateDb(file); // delete the file on the remote server after pulling it over // sftp.Delete(f); } catch (SystemException se) { LogMsg("NEXCEPTION:AMSSftpMgr::GetFile():ECaught:TryingToGetFile:" + indx + ":" + file + "::" + se.Message); } } sftp.Close(); status = true; } catch (Tamir.SharpSsh.jsch.SftpException se) { LogMsg("NEXCEPTION:AMSSftpMgr::GetFile():ECaught:" + se.Message); } catch (Tamir.SharpSsh.jsch.JSchException jse) { LogMsg("NEXCEPTION:AMSSftpMgr::GetFile():ECaught:" + jse.Message); } catch (Tamir.SharpSsh.SshTransferException ste) { LogMsg("NEXCEPTION:AMSSftpMgr::GetFile():ECaught:" + ste.Message); } catch (SystemException se) { LogMsg("NEXCEPTION:AMSSftpMgr::GetFile():ECaught:" + se.Message); } return status; }
public static bool downloadSecureFile(Uri serverUri, string filename, string username, string password, bool UseSSL, string destFile) { // The serverUri parameter should use the ftp:// scheme. // It contains the name of the server file that is to be deleted. // Example: ftp://contoso.com/someFile.txt. // if (serverUri.Scheme != Uri.UriSchemeFtp) { return false; } Sftp sftp = new Sftp(serverUri.Host, username, password); if (sftp == null) { //Error connecting to FTP server throw new WebException("ERROR - " + ClassName + ": Error connecting to FTP server", WebExceptionStatus.SecureChannelFailure); } else { try { sftp.OnTransferStart += new FileTransferEvent(sftp_OnTransferStart); sftp.OnTransferProgress += new FileTransferEvent(sftp_OnTransferProgress); sftp.OnTransferEnd += new FileTransferEvent(sftp_OnTransferEnd); sftp.Connect(); sftp.Get(serverUri.LocalPath + filename, destFile); //confirm the file is fully downloaded long remoteSize = sftp.getSize(serverUri.LocalPath + filename); FileInfo localfile = new FileInfo(destFile); long localSize = localfile.Length; if (localSize >= (remoteSize - 100)) { return true; } else { //file was not complete return false; } } catch (Tamir.SharpSsh.jsch.SftpException sfex) { // write to log - note the lowercase m in message - this library uses nonstandard error reporting Trace.TraceInformation("ERROR - " + ClassName + ":" + sfex.message); throw new WebException("ERROR - " + ClassName + ": " + sfex.message); } catch (Exception ex) { // write to log Trace.TraceInformation("ERROR - " + ClassName + ":" + ex.Message); //EventLog.WriteEntry(Application.ProductName, "ERROR - " + ClassName + ":" + ex.Message, EventLogEntryType.Error); throw new WebException("ERROR - " + ClassName + ": " + ex.Message); //return false; } finally { sftp.Close(); } } }
/*public byte[] getZipFile(int jobId, string fileName) { if (fileName.Contains("/")) return null; //fail for securety //if (fileName.Contains(' ')) return null; //fail for securety Sftp sftp = getSftp(); SshStream shell = getShell(); shell.Write(string.Format(@"cd poy_service/{1} ; zip {0}.zip {0}", fileName, jobId )); for(int i = 0 ; i<30 ; i++) { Thread.Sleep(2000); if(sftp.GetFileList(string.Format(@"poy_service/{0}/", jobId)).Contains(fileName+".zip")) i=30; } string vmFileName = string.Format(@"{0}{1}.zip", tempDir, fileName); if(File.Exists(vmFileName)) { File.Delete(vmFileName); } sftp.Get( string.Format(@"poy_service/{1}/{0}.zip", fileName, jobId), vmFileName ); shell.Write(string.Format(@"rm {0}.zip", fileName, jobId )); FileStream fileStream = File.OpenRead(vmFileName); byte[] filedata = new byte[(int)fileStream.Length]; fileStream.Read(filedata, 0, (int)fileStream.Length); //File.Delete(string.Format(@"{0}{1}", tempDir, fileName)); return filedata; }*/ public string getTextFile(int jobId, string fileName) { if (fileName.Contains("/")) return null; //fail for securety //if (fileName.Contains(' ')) return null; //fail for securety Sftp sftp = new Sftp(HostName, UserName, Password); sftp.Connect(); sftp.Get( string.Format(@"poy_service/{1}/{0}", fileName, jobId,DataPath), string.Format(@"{0}{1}",tempDir, fileName) ); string output = File.ReadAllText(string.Format(@"{0}{1}", tempDir, fileName)); File.Delete(string.Format(@"{0}{1}", tempDir, fileName)); return output; }
public byte[] getFile(int jobId, string fileName) { if (fileName.Contains("/")) return null; //fail for securety //if (fileName.Contains(' ')) return null; //fail for securety Sftp sftp = new Sftp(HostName, UserName, Password); sftp.Connect(); sftp.Get( string.Format(@"poy_service/{1}/{0}", fileName, jobId,DataPath), string.Format(@"{0}{1}",tempDir, fileName) ); FileStream fileStream = File.OpenRead(string.Format(@"{0}{1}", tempDir, fileName)); byte[] filedata = new byte[(int)fileStream.Length]; fileStream.Read(filedata, 0, (int)fileStream.Length); File.Delete(string.Format(@"{0}{1}", tempDir, fileName)); return filedata; }
public override DTSExecResult Execute(Connections connections, VariableDispenser variableDispenser, IDTSComponentEvents componentEvents, IDTSLogging log, object transaction) { ConnectionManager cSSH = getConnectionManager(connections, _SSHconnMgrName); List<KeyValuePair<string, string>> connParams = (List<KeyValuePair<string, string>>)cSSH.AcquireConnection(transaction); string host = connParams.Find(t => t.Key == "Host").Value; string username = connParams.Find(t => t.Key == "Username").Value; string password = connParams.Find(t => t.Key == "Password").Value; int port = Convert.ToInt32(connParams.Find(t => t.Key == "Port").Value); if (_operation == SSHFTPOperation.SendFiles) { ConnectionManager cSend = getConnectionManager(connections, _sendFilesSourceConnectionManagerName); string sourceFile = cSend.AcquireConnection(transaction).ToString(); SshTransferProtocolBase sshCp; sshCp = new Sftp(host, username); sshCp.Password = password; string localFile = sourceFile; string remoteFile = getRemotePathAndFile(_sendFilesDestinationDirectory, Path.GetFileName(sourceFile)); try { sshCp.Connect(); sshCp.Put(localFile, remoteFile); } catch { throw; } finally { sshCp.Close(); } } if (_operation == SSHFTPOperation.ReceiveFiles) { ConnectionManager cReceive = getConnectionManager(connections, _receiveFilesDestinationConnectionManagerName); string destinationDirectory = cReceive.AcquireConnection(transaction).ToString(); SshTransferProtocolBase sshCp; sshCp = new Sftp(host, username); sshCp.Password = password; try { sshCp.Connect(); sshCp.Get(_receiveFilesSourceFile, destinationDirectory); } catch { throw; } finally { sshCp.Close(); } } return DTSExecResult.Success; }