Search in sources :

Example 6 with SFTPv3Client

use of ch.ethz.ssh2.SFTPv3Client in project pentaho-kettle by pentaho.

the class JobEntrySSH2PUT method execute.

@Override
public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);
    try {
        // Get real variable value
        String realServerName = environmentSubstitute(serverName);
        int realServerPort = Const.toInt(environmentSubstitute(serverPort), 22);
        String realUserName = environmentSubstitute(userName);
        String realServerPassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(password));
        // Proxy Host
        String realProxyHost = environmentSubstitute(httpproxyhost);
        int realProxyPort = Const.toInt(environmentSubstitute(httpproxyport), 22);
        String realproxyUserName = environmentSubstitute(httpproxyusername);
        String realProxyPassword = Encr.decryptPasswordOptionallyEncrypted(environmentSubstitute(httpProxyPassword));
        // Key file
        String realKeyFilename = environmentSubstitute(keyFilename);
        String relKeyFilepass = environmentSubstitute(keyFilePass);
        // Source files
        String realLocalDirectory = environmentSubstitute(localDirectory);
        String realwildcard = environmentSubstitute(wildcard);
        // Remote destination
        String realftpDirectory = environmentSubstitute(ftpDirectory);
        // Destination folder (Move to)
        String realDestinationFolder = environmentSubstitute(destinationfolder);
        try {
            // Remote source
            realftpDirectory = FTPUtils.normalizePath(realftpDirectory);
            // Destination folder (Move to)
            realDestinationFolder = FTPUtils.normalizePath(realDestinationFolder);
        } catch (Exception e) {
            logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.CanNotNormalizePath", e.getMessage()));
            result.setNrErrors(1);
            return result;
        }
        // Check for mandatory fields
        boolean mandatoryok = true;
        if (Utils.isEmpty(realServerName)) {
            mandatoryok = false;
            logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.ServernameMissing"));
        }
        if (usehttpproxy) {
            if (Utils.isEmpty(realProxyHost)) {
                mandatoryok = false;
                logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.HttpProxyhostMissing"));
            }
        }
        if (publicpublickey) {
            if (Utils.isEmpty(realKeyFilename)) {
                mandatoryok = false;
                logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.KeyFileMissing"));
            } else {
                // Let's check if folder exists...
                if (!KettleVFS.fileExists(realKeyFilename, this)) {
                    mandatoryok = false;
                    logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.KeyFileNotExist"));
                }
            }
        }
        if (Utils.isEmpty(realLocalDirectory)) {
            mandatoryok = false;
            logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.LocalFolderMissing"));
        }
        if (afterFtpPut.equals("move_file")) {
            if (Utils.isEmpty(realDestinationFolder)) {
                mandatoryok = false;
                logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.DestinatFolderMissing"));
            } else {
                FileObject folder = null;
                try {
                    folder = KettleVFS.getFileObject(realDestinationFolder, this);
                    // Let's check if folder exists...
                    if (!folder.exists()) {
                        // Do we need to create it?
                        if (createDestinationFolder) {
                            folder.createFolder();
                        } else {
                            logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.DestinatFolderNotExist", realDestinationFolder));
                        }
                    }
                } catch (Exception e) {
                    throw new KettleException(e);
                } finally {
                    if (folder != null) {
                        try {
                            folder.close();
                            folder = null;
                        } catch (Exception e) {
                        /* Ignore */
                        }
                    }
                }
            }
        }
        if (mandatoryok) {
            Connection conn = null;
            SFTPv3Client client = null;
            boolean good = true;
            int nbfilestoput = 0;
            int nbput = 0;
            int nbrerror = 0;
            try {
                // Create a connection instance
                conn = getConnection(realServerName, realServerPort, realProxyHost, realProxyPort, realproxyUserName, realProxyPassword);
                if (timeout > 0) {
                    // Cache Host Key
                    if (cachehostkey) {
                        conn.connect(new SimpleVerifier(database), 0, timeout * 1000);
                    } else {
                        conn.connect(null, 0, timeout * 1000);
                    }
                } else {
                    // Cache Host Key
                    if (cachehostkey) {
                        conn.connect(new SimpleVerifier(database));
                    } else {
                        conn.connect();
                    }
                }
                // Authenticate
                boolean isAuthenticated = false;
                if (publicpublickey) {
                    String keyContent = KettleVFS.getTextFileContent(realKeyFilename, this, Const.XML_ENCODING);
                    isAuthenticated = conn.authenticateWithPublicKey(realUserName, keyContent.toCharArray(), relKeyFilepass);
                } else {
                    isAuthenticated = conn.authenticateWithPassword(realUserName, realServerPassword);
                }
                // LET'S CHECK AUTHENTICATION ...
                if (isAuthenticated == false) {
                    logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.AuthenticationFailed"));
                } else {
                    if (log.isBasic()) {
                        logBasic(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Connected", serverName, userName));
                    }
                    client = new SFTPv3Client(conn);
                    if (log.isDetailed()) {
                        logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.ProtocolVersion", "" + client.getProtocolVersion()));
                    }
                    // Check if remote directory exists
                    if (!Utils.isEmpty(realftpDirectory)) {
                        if (!sshDirectoryExists(client, realftpDirectory)) {
                            good = false;
                            if (createRemoteFolder) {
                                good = CreateRemoteFolder(client, realftpDirectory);
                                if (good) {
                                    logBasic(BaseMessages.getString(PKG, "JobSSH2PUT.Log.RemoteDirectoryCreated"));
                                }
                            } else {
                                logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.RemoteDirectoryNotExist", realftpDirectory));
                            }
                        } else if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.RemoteDirectoryExist", realftpDirectory));
                        }
                    }
                    if (good) {
                        // Get files list from local folder (source)
                        List<FileObject> myFileList = getFiles(realLocalDirectory);
                        // Prepare Pattern for wildcard
                        Pattern pattern = null;
                        if (!Utils.isEmpty(realwildcard)) {
                            pattern = Pattern.compile(realwildcard);
                        }
                        // Get the files in the list
                        for (int i = 0; i < myFileList.size() && !parentJob.isStopped(); i++) {
                            FileObject myFile = myFileList.get(i);
                            String localFilename = myFile.toString();
                            String remoteFilename = myFile.getName().getBaseName();
                            boolean getIt = true;
                            // First see if the file matches the regular expression!
                            if (pattern != null) {
                                Matcher matcher = pattern.matcher(remoteFilename);
                                getIt = matcher.matches();
                            }
                            // do we have a target directory?
                            if (!Utils.isEmpty(realftpDirectory)) {
                                remoteFilename = realftpDirectory + FTPUtils.FILE_SEPARATOR + remoteFilename;
                            }
                            if (onlyGettingNewFiles) {
                                // We get only new files
                                // ie not exist on the remote server
                                getIt = !sshFileExists(client, remoteFilename);
                            }
                            if (getIt) {
                                nbfilestoput++;
                                boolean putok = putFile(myFile, remoteFilename, client);
                                if (!putok) {
                                    nbrerror++;
                                    logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Error.CanNotPutFile", localFilename));
                                } else {
                                    nbput++;
                                }
                                if (putok && !afterFtpPut.equals("do_nothing")) {
                                    deleteOrMoveFiles(myFile, realDestinationFolder);
                                }
                            }
                        }
                        /**
                         ****************************** RESULT *******************
                         */
                        if (log.isDetailed()) {
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Result.JobEntryEnd1"));
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Result.TotalFiles", "" + nbfilestoput));
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Result.TotalFilesPut", "" + nbput));
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Result.TotalFilesError", "" + nbrerror));
                            logDetailed(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Result.JobEntryEnd2"));
                        }
                        if (nbrerror == 0) {
                            result.setResult(true);
                        /**
                         ****************************** RESULT *******************
                         */
                        }
                    }
                }
            } catch (Exception e) {
                result.setNrErrors(nbrerror);
                logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Error.ErrorFTP", e.getMessage()));
            } finally {
                if (conn != null) {
                    conn.close();
                }
                if (client != null) {
                    client.close();
                }
            }
        }
    } catch (Exception e) {
        result.setResult(false);
        result.setNrErrors(1L);
        logError(BaseMessages.getString(PKG, "JobSSH2PUT.Log.Error.UnexpectedError"), e);
    }
    return result;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) Connection(com.trilead.ssh2.Connection) SFTPv3Client(com.trilead.ssh2.SFTPv3Client) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) IOException(java.io.IOException) Result(org.pentaho.di.core.Result) FileObject(org.apache.commons.vfs2.FileObject)

Example 7 with SFTPv3Client

use of ch.ethz.ssh2.SFTPv3Client in project pentaho-kettle by pentaho.

the class JobEntryFTPDelete method SSHConnect.

private void SSHConnect(String realservername, String realserverpassword, int realserverport, String realUsername, String realPassword, String realproxyhost, String realproxyusername, String realproxypassword, int realproxyport, String realkeyFilename, String realkeyPass) throws Exception {
    /* Create a connection instance */
    Connection conn = new Connection(realservername, realserverport);
    /* We want to connect through a HTTP proxy */
    if (useproxy) {
        conn.setProxyData(new HTTPProxyData(realproxyhost, realproxyport));
        // if the proxy requires basic authentication:
        if (!Utils.isEmpty(realproxyusername) || !Utils.isEmpty(realproxypassword)) {
            conn.setProxyData(new HTTPProxyData(realproxyhost, realproxyport, realproxyusername, realproxypassword));
        }
    }
    if (timeout > 0) {
        // Use timeout
        conn.connect(null, 0, timeout * 1000);
    } else {
        // Cache Host Key
        conn.connect();
    }
    // Authenticate
    boolean isAuthenticated = false;
    if (publicpublickey) {
        isAuthenticated = conn.authenticateWithPublicKey(realUsername, new File(realkeyFilename), realkeyPass);
    } else {
        isAuthenticated = conn.authenticateWithPassword(realUsername, realserverpassword);
    }
    if (!isAuthenticated) {
        throw new Exception("Can not connect to ");
    }
    sshclient = new SFTPv3Client(conn);
}
Also used : FTPSConnection(org.pentaho.di.job.entries.ftpsget.FTPSConnection) Connection(com.trilead.ssh2.Connection) SFTPv3Client(com.trilead.ssh2.SFTPv3Client) HTTPProxyData(com.trilead.ssh2.HTTPProxyData) File(java.io.File) FTPException(com.enterprisedt.net.ftp.FTPException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException)

Example 8 with SFTPv3Client

use of ch.ethz.ssh2.SFTPv3Client in project agileway by fangjinuo.

the class Ssh2Connection method openSftpSession.

@Override
public SftpSession openSftpSession() throws SshException {
    try {
        SFTPv3Client sftpClient = new SFTPv3Client(this.delegate);
        Ssh2SftpSession session = new Ssh2SftpSession(sftpClient);
        session.setSshConnection(this);
        return session;
    } catch (Throwable ex) {
        throw new SshException(ex.getMessage(), ex);
    }
}
Also used : Ssh2SftpSession(com.jn.agileway.ssh.client.impl.trileadssh2.sftp.Ssh2SftpSession) SFTPv3Client(com.trilead.ssh2.SFTPv3Client) SshException(com.jn.agileway.ssh.client.SshException)

Aggregations

SFTPv3Client (com.trilead.ssh2.SFTPv3Client)7 Connection (com.trilead.ssh2.Connection)3 MessageBox (org.eclipse.swt.widgets.MessageBox)3 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)3 KettleException (org.pentaho.di.core.exception.KettleException)3 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)3 SshException (com.jn.agileway.ssh.client.SshException)2 File (java.io.File)2 Pattern (java.util.regex.Pattern)2 Result (org.pentaho.di.core.Result)2 SFTPv3Client (ch.ethz.ssh2.SFTPv3Client)1 FTPException (com.enterprisedt.net.ftp.FTPException)1 Ssh2SftpSession (com.jn.agileway.ssh.client.impl.ganymedssh2.sftp.Ssh2SftpSession)1 Ssh2SftpSession (com.jn.agileway.ssh.client.impl.trileadssh2.sftp.Ssh2SftpSession)1 HTTPProxyData (com.trilead.ssh2.HTTPProxyData)1 IOException (java.io.IOException)1 Matcher (java.util.regex.Matcher)1 FileObject (org.apache.commons.vfs2.FileObject)1 KettleFileException (org.pentaho.di.core.exception.KettleFileException)1 FTPSConnection (org.pentaho.di.job.entries.ftpsget.FTPSConnection)1