Skip to content

Uploaded file is empty and with wrong name #9

@pandasoft85

Description

@pandasoft85

I want to upload file from windows to Unix. File created, but it's empty.

This code creates empty 'C:\Users\myuser\test\mydir\my_test_file.sql' file in /tmp directory insted of my_test_file.sql.
Tried to move "my_test_file.sql" to working dir to use file without full path, but it creates empty "my_test_file.sql" file.
There is no any error message.

Tried for both 262 and 263 versions.

Note: At the end need to use 263 version to include key-exchange algorithms related changes.

public void test() {
    String hostName = "my-IP";
    String userName = "user";
    String password = "pass";

    try {

        Connection conn = getSSHConnection(hostName, userName, password);

        String myFile = "C:\\Users\\myuser\\test\\mydir\\my_test_file.sql";
       
        SCPClient client = conn.createSCPClient();

        client.put(myFile, new File(myFile).length(), "/tmp", "0777");
    } catch (Exception e) {
        System.out.println("ERROR: " + e.getMessage());
    }
}

Connection getSSHConnection(String hostName, String userName, final String password) throws IOException, Exception {
    Connection conn = new Connection(hostName);
    conn.connect();

    String[] strArray;
    try {
        strArray = conn.getRemainingAuthMethods(userName);
    } catch (IOException var7) {
        throw new Exception("Getting Remaining AuthMethods failed with IOException: " + var7.getMessage());
    }

    if (strArray == null) {
        System.out.println("conn.getRemainingAuthMethods returns null");

        try {
            conn.authenticateWithPassword(userName, password);
        } catch (Exception var8) {
            String warning = "";
            if (password.equals("")) {
                warning = warning + " : Warning: Implementation of this package does not allow empty passwords for authentication";
            }

            throw new Exception("Authentication with password failed: " + var8.getMessage() + warning);
        }
    } else {
        List<String> authMethods = Arrays.asList(strArray);
        if (authMethods.contains("password")) {
            if (!conn.authenticateWithPassword(userName, password)) {
                throw new Exception("Password based authentication failed.");
            }
        } else {
            if (!authMethods.contains("keyboard-interactive")) {
                throw new Exception("SSH Server doesnt support password or keyboard-interactive logins");
            }

            InteractiveCallback cb = new InteractiveCallback() {
                public String[] replyToChallenge(String name, String instruction, int numPrompts, String[] prompt, boolean[] echo) throws Exception {
                    String[] response = new String[numPrompts];

                    for (int i = 0; i < response.length; ++i) {
                        response[i] = password;
                    }

                    return response;
                }
            };
            if (!conn.authenticateWithKeyboardInteractive(userName, cb)) {
                throw new Exception("Keyboard-interactive based authentication failed.");
            }
        }
    }
    return conn;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions