Search in sources :

Example 11 with SFTPv3Client

use of com.trilead.ssh2.SFTPv3Client in project intellij-community by JetBrains.

the class SFTPv3Client method setstat.

/**
	 *  Modify the attributes of a file. Used for operations such as changing
	 *  the ownership, permissions or access times, as well as for truncating a file.
	 * 
	 * @param path See the {@link SFTPv3Client comment} for the class for more details.
	 * @param attr A SFTPv3FileAttributes object. Specifies the modifications to be
	 *             made to the attributes of the file. Empty fields will be ignored.
	 * @throws IOException
	 */
public void setstat(String path, SFTPv3FileAttributes attr) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    tw.writeBytes(createAttrs(attr));
    if (debug != null) {
        debug.println("Sending SSH_FXP_SETSTAT...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_SETSTAT, req_id, tw.getBytes());
    expectStatusOKMessage(req_id);
}
Also used : TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 12 with SFTPv3Client

use of com.trilead.ssh2.SFTPv3Client in project intellij-community by JetBrains.

the class SFTPv3Client method readLink.

/**
	 * Read the target of a symbolic link.
	 * 
	 * @param path See the {@link SFTPv3Client comment} for the class for more details.
	 * @return The target of the link.
	 * @throws IOException
	 */
public String readLink(String path) throws IOException {
    int req_id = generateNextRequestID();
    TypesWriter tw = new TypesWriter();
    tw.writeString(path, charsetName);
    if (debug != null) {
        debug.println("Sending SSH_FXP_READLINK...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_READLINK, req_id, tw.getBytes());
    byte[] resp = receiveMessage(34000);
    if (debug != null) {
        debug.println("Got REPLY.");
        debug.flush();
    }
    TypesReader tr = new TypesReader(resp);
    int t = tr.readByte();
    int rep_id = tr.readUINT32();
    if (rep_id != req_id)
        throw new IOException("The server sent an invalid id field.");
    if (t == Packet.SSH_FXP_NAME) {
        int count = tr.readUINT32();
        if (count != 1)
            throw new IOException("The server sent an invalid SSH_FXP_NAME packet.");
        return tr.readString(charsetName);
    }
    if (t != Packet.SSH_FXP_STATUS)
        throw new IOException("The SFTP server sent an unexpected packet type (" + t + ")");
    int errorCode = tr.readUINT32();
    throw new SFTPException(tr.readString(), errorCode);
}
Also used : TypesReader(com.trilead.ssh2.packets.TypesReader) TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 13 with SFTPv3Client

use of com.trilead.ssh2.SFTPv3Client in project intellij-community by JetBrains.

the class SFTPv3Client method createSymlink.

/**
	 * Create a symbolic link on the server. Creates a link "src" that points
	 * to "target".
	 * 
	 * @param src See the {@link SFTPv3Client comment} for the class for more details.
	 * @param target See the {@link SFTPv3Client comment} for the class for more details.
	 * @throws IOException
	 */
public void createSymlink(String src, String target) throws IOException {
    int req_id = generateNextRequestID();
    /* Either I am too stupid to understand the SFTP draft
		 * or the OpenSSH guys changed the semantics of src and target.
		 */
    TypesWriter tw = new TypesWriter();
    tw.writeString(target, charsetName);
    tw.writeString(src, charsetName);
    if (debug != null) {
        debug.println("Sending SSH_FXP_SYMLINK...");
        debug.flush();
    }
    sendMessage(Packet.SSH_FXP_SYMLINK, req_id, tw.getBytes());
    expectStatusOKMessage(req_id);
}
Also used : TypesWriter(com.trilead.ssh2.packets.TypesWriter)

Example 14 with SFTPv3Client

use of com.trilead.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 15 with SFTPv3Client

use of com.trilead.ssh2.SFTPv3Client in project pentaho-kettle by pentaho.

the class JobEntryFTPDelete method execute.

/**
 * Needed for the Vector coming from sshclient.ls() *
 */
@SuppressWarnings("unchecked")
public Result execute(Result previousResult, int nr) {
    log.logBasic(BaseMessages.getString(PKG, "JobEntryFTPDelete.Started", serverName));
    RowMetaAndData resultRow = null;
    Result result = previousResult;
    List<RowMetaAndData> rows = result.getRows();
    result.setResult(false);
    NrErrors = 0;
    NrfilesDeleted = 0;
    successConditionBroken = false;
    HashSet<String> list_previous_files = new HashSet<>();
    // Here let's put some controls before stating the job
    String realservername = environmentSubstitute(serverName);
    String realserverpassword = Utils.resolvePassword(this, password);
    String realFtpDirectory = environmentSubstitute(ftpDirectory);
    int realserverport = Const.toInt(environmentSubstitute(port), 0);
    String realUsername = environmentSubstitute(userName);
    String realPassword = Utils.resolvePassword(this, password);
    String realproxyhost = environmentSubstitute(proxyHost);
    String realproxyusername = environmentSubstitute(proxyUsername);
    String realproxypassword = Utils.resolvePassword(this, proxyPassword);
    int realproxyport = Const.toInt(environmentSubstitute(proxyPort), 0);
    String realkeyFilename = environmentSubstitute(keyFilename);
    String realkeyPass = environmentSubstitute(keyFilePass);
    // PDI The following is used to apply a path for SSH because the SFTPv3Client doesn't let us specify/change dirs
    String sourceFolder = "";
    if (isDetailed()) {
        logDetailed(BaseMessages.getString(PKG, "JobEntryFTPDelete.Start"));
    }
    if (copyprevious && rows.size() == 0) {
        if (isDetailed()) {
            logDetailed(BaseMessages.getString(PKG, "JobEntryFTPDelete.ArgsFromPreviousNothing"));
        }
        result.setResult(true);
        return result;
    }
    try {
        // Get all the files in the current directory...
        String[] filelist = null;
        if (protocol.equals(PROTOCOL_FTP)) {
            // If socks proxy server was provided
            if (!Utils.isEmpty(socksProxyHost)) {
                if (!Utils.isEmpty(socksProxyPort)) {
                    FTPClient.initSOCKS(environmentSubstitute(socksProxyPort), environmentSubstitute(socksProxyHost));
                } else {
                    throw new FTPException(BaseMessages.getString(PKG, "JobEntryFTPDelete.SocksProxy.PortMissingException", environmentSubstitute(socksProxyHost), getName()));
                }
                // then if we have authentication information
                if (!Utils.isEmpty(socksProxyUsername) && !Utils.isEmpty(socksProxyPassword)) {
                    FTPClient.initSOCKSAuthentication(environmentSubstitute(socksProxyUsername), Utils.resolvePassword(this, socksProxyPassword));
                } else if (!Utils.isEmpty(socksProxyUsername) && Utils.isEmpty(socksProxyPassword) || Utils.isEmpty(socksProxyUsername) && !Utils.isEmpty(socksProxyPassword)) {
                    // we have a username without a password or vica versa
                    throw new FTPException(BaseMessages.getString(PKG, "JobEntryFTPDelete.SocksProxy.IncompleteCredentials", environmentSubstitute(socksProxyHost), getName()));
                }
            }
            // establish the connection
            FTPConnect(realservername, realUsername, realPassword, realserverport, realFtpDirectory, realproxyhost, realproxyusername, realproxypassword, realproxyport, timeout);
            filelist = ftpclient.dir();
            // CHECK THIS !!!
            if (filelist.length == 1) {
                String translatedWildcard = environmentSubstitute(wildcard);
                if (!Utils.isEmpty(translatedWildcard)) {
                    if (filelist[0].startsWith(translatedWildcard)) {
                        throw new FTPException(filelist[0]);
                    }
                }
            }
        } else if (protocol.equals(PROTOCOL_FTPS)) {
            // establish the secure connection
            FTPSConnect(realservername, realUsername, realserverport, realPassword, realFtpDirectory, timeout);
            // Get all the files in the current directory...
            filelist = ftpsclient.getFileNames();
        } else if (protocol.equals(PROTOCOL_SFTP)) {
            // establish the secure connection
            SFTPConnect(realservername, realUsername, realserverport, realPassword, realFtpDirectory);
            // Get all the files in the current directory...
            filelist = sftpclient.dir();
        } else if (protocol.equals(PROTOCOL_SSH)) {
            // establish the secure connection
            SSHConnect(realservername, realserverpassword, realserverport, realUsername, realPassword, realproxyhost, realproxyusername, realproxypassword, realproxyport, realkeyFilename, realkeyPass);
            sourceFolder = ".";
            if (realFtpDirectory != null) {
                sourceFolder = realFtpDirectory + "/";
            } else {
                sourceFolder = "./";
            }
            // NOTE: Source of the unchecked warning suppression for the declaration of this method.
            Vector<SFTPv3DirectoryEntry> vfilelist = sshclient.ls(sourceFolder);
            if (vfilelist != null) {
                // Make one pass through the vfilelist to get an accurate count
                // Using the two-pass method with arrays is faster than using ArrayList
                int fileCount = 0;
                Iterator<SFTPv3DirectoryEntry> iterator = vfilelist.iterator();
                while (iterator.hasNext()) {
                    SFTPv3DirectoryEntry dirEntry = iterator.next();
                    if (dirEntry != null && !dirEntry.filename.equals(".") && !dirEntry.filename.equals("..") && !isDirectory(sshclient, sourceFolder + dirEntry.filename)) {
                        fileCount++;
                    }
                }
                // Now that we have the correct count, create and fill in the array
                filelist = new String[fileCount];
                iterator = vfilelist.iterator();
                int i = 0;
                while (iterator.hasNext()) {
                    SFTPv3DirectoryEntry dirEntry = iterator.next();
                    if (dirEntry != null && !dirEntry.filename.equals(".") && !dirEntry.filename.equals("..") && !isDirectory(sshclient, sourceFolder + dirEntry.filename)) {
                        filelist[i] = dirEntry.filename;
                        i++;
                    }
                }
            }
        }
        if (isDetailed()) {
            logDetailed("JobEntryFTPDelete.FoundNFiles", String.valueOf(filelist.length));
        }
        int found = filelist == null ? 0 : filelist.length;
        if (found == 0) {
            result.setResult(true);
            return result;
        }
        Pattern pattern = null;
        if (copyprevious) {
            // Copy the input row to the (command line) arguments
            for (int iteration = 0; iteration < rows.size(); iteration++) {
                resultRow = rows.get(iteration);
                // Get file names
                String file_previous = resultRow.getString(0, null);
                if (!Utils.isEmpty(file_previous)) {
                    list_previous_files.add(file_previous);
                }
            }
        } else {
            if (!Utils.isEmpty(wildcard)) {
                String realWildcard = environmentSubstitute(wildcard);
                pattern = Pattern.compile(realWildcard);
            }
        }
        if (!getSuccessCondition().equals(SUCCESS_IF_ALL_FILES_DOWNLOADED)) {
            limitFiles = Const.toInt(environmentSubstitute(getLimitSuccess()), 10);
        }
        // Get the files in the list...
        for (int i = 0; i < filelist.length && !parentJob.isStopped(); i++) {
            if (successConditionBroken) {
                throw new Exception(BaseMessages.getString(PKG, "JobEntryFTPDelete.SuccesConditionBroken"));
            }
            boolean getIt = false;
            if (isDebug()) {
                logDebug(BaseMessages.getString(PKG, "JobEntryFTPDelete.AnalysingFile", filelist[i]));
            }
            try {
                // First see if the file matches the regular expression!
                if (copyprevious) {
                    if (list_previous_files.contains(filelist[i])) {
                        getIt = true;
                    }
                } else {
                    if (pattern != null) {
                        Matcher matcher = pattern.matcher(filelist[i]);
                        getIt = matcher.matches();
                    }
                }
                if (getIt) {
                    // Delete file
                    if (protocol.equals(PROTOCOL_FTP)) {
                        ftpclient.delete(filelist[i]);
                    }
                    if (protocol.equals(PROTOCOL_FTPS)) {
                        // System.out.println( "---------------" + filelist[i] );
                        ftpsclient.deleteFile(filelist[i]);
                    } else if (protocol.equals(PROTOCOL_SFTP)) {
                        sftpclient.delete(filelist[i]);
                    } else if (protocol.equals(PROTOCOL_SSH)) {
                        sshclient.rm(sourceFolder + filelist[i]);
                    }
                    if (isDetailed()) {
                        logDetailed("JobEntryFTPDelete.RemotefileDeleted", filelist[i]);
                    }
                    updateDeletedFiles();
                }
            } catch (Exception e) {
                // Update errors number
                updateErrors();
                logError(BaseMessages.getString(PKG, "JobFTP.UnexpectedError", e.getMessage()));
                if (successConditionBroken) {
                    throw new Exception(BaseMessages.getString(PKG, "JobEntryFTPDelete.SuccesConditionBroken"));
                }
            }
        }
    // end for
    } catch (Exception e) {
        updateErrors();
        logError(BaseMessages.getString(PKG, "JobEntryFTPDelete.ErrorGetting", e.getMessage()));
        logError(Const.getStackTracker(e));
    } finally {
        if (ftpclient != null && ftpclient.connected()) {
            try {
                ftpclient.quit();
                ftpclient = null;
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobEntryFTPDelete.ErrorQuitting", e.getMessage()));
            }
        }
        if (ftpsclient != null) {
            try {
                ftpsclient.disconnect();
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobEntryFTPDelete.ErrorQuitting", e.getMessage()));
            }
        }
        if (sftpclient != null) {
            try {
                sftpclient.disconnect();
                sftpclient = null;
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobEntryFTPDelete.ErrorQuitting", e.getMessage()));
            }
        }
        if (sshclient != null) {
            try {
                sshclient.close();
                sshclient = null;
            } catch (Exception e) {
                logError(BaseMessages.getString(PKG, "JobEntryFTPDelete.ErrorQuitting", e.getMessage()));
            }
        }
        FTPClient.clearSOCKS();
    }
    result.setResult(!successConditionBroken);
    result.setNrFilesRetrieved(NrfilesDeleted);
    result.setNrErrors(NrErrors);
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) 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) Result(org.pentaho.di.core.Result) SFTPv3DirectoryEntry(com.trilead.ssh2.SFTPv3DirectoryEntry) RowMetaAndData(org.pentaho.di.core.RowMetaAndData) Iterator(java.util.Iterator) Vector(java.util.Vector) FTPException(com.enterprisedt.net.ftp.FTPException) HashSet(java.util.HashSet)

Aggregations

TypesWriter (com.trilead.ssh2.packets.TypesWriter)8 SFTPv3Client (com.trilead.ssh2.SFTPv3Client)6 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)6 KettleException (org.pentaho.di.core.exception.KettleException)6 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)6 Connection (com.trilead.ssh2.Connection)3 SFTPv3DirectoryEntry (com.trilead.ssh2.SFTPv3DirectoryEntry)3 File (java.io.File)3 Pattern (java.util.regex.Pattern)3 MessageBox (org.eclipse.swt.widgets.MessageBox)3 Result (org.pentaho.di.core.Result)3 FTPException (com.enterprisedt.net.ftp.FTPException)2 SFTPv3FileHandle (com.trilead.ssh2.SFTPv3FileHandle)2 TypesReader (com.trilead.ssh2.packets.TypesReader)2 IOException (java.io.IOException)2 Matcher (java.util.regex.Matcher)2 KettleFileException (org.pentaho.di.core.exception.KettleFileException)2 HTTPProxyData (com.trilead.ssh2.HTTPProxyData)1 BufferedInputStream (java.io.BufferedInputStream)1 FileOutputStream (java.io.FileOutputStream)1