Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploaded file is empty and with wrong name #9

Open
pandasoft85 opened this issue Sep 29, 2022 · 1 comment
Open

Uploaded file is empty and with wrong name #9

pandasoft85 opened this issue Sep 29, 2022 · 1 comment

Comments

@pandasoft85
Copy link

pandasoft85 commented Sep 29, 2022

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;
}
@jsiegel-intel
Copy link

Unlike earlier version of Ganymed, the SCPClient.put() method now returns an SCPOutputStream object. You are responsible for copying the file contents yourself. Something like this:

out=client.put(myFile, new File(myFile).length(), "/tmp", "0777");
in = new  FileInputStream(new File(src))
IOUtils.copy(in, out)
in.close()
out.close()

See this: https://stackoverflow.com/questions/24165554/how-do-i-scp-a-file-up-to-the-server-using-the-latest-ganymed-ssh2-scp-implmenta

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants