0

I want to transfer file from windows to unix(linux) box using Java, then change to super user. I tried using the JSCH library, that is SFTP with java.

I am stuck at the step to change to super user.

The steps i followed are similar to how to transfer a file through SFTP in java?

I know similar questions have been asked, but i am not able to change user using this approach.

Request to please do not mark this as duplicate.

Can anyone help me with the steps, or any other approach? An example would be helpful for the same. Thanks in advance.

Edit-1 - sample snippet from the link how to transfer a file through SFTP in java?, which is used as a reference posted

 public  void send (String fileName) {
    String SFTPHOST = "host:IP";
    int SFTPPORT = 22;
    String SFTPUSER = "username";
    String SFTPPASS = "password";
    String SFTPWORKINGDIR = "file/to/transfer";

    Session session = null;
    Channel channel = null;
    ChannelSftp channelSftp = null;
    System.out.println("preparing the host information for sftp.");
    try {
        JSch jsch = new JSch();
        session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
        session.setPassword(SFTPPASS);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        System.out.println("Host connected.");
        channel = session.openChannel("sftp");
        channel.connect();
        System.out.println("sftp channel opened and connected.");
        channelSftp = (ChannelSftp) channel;
        channelSftp.cd(SFTPWORKINGDIR);
        File f = new File(fileName);
        channelSftp.put(new FileInputStream(f), f.getName());
        log.info("File transfered successfully to host.");
    } catch (Exception ex) {
         System.out.println("Exception found while tranfer the response.");
    }
    finally{

        channelSftp.exit();
        System.out.println("sftp Channel exited.");
        channel.disconnect();
        System.out.println("Channel disconnected.");
        session.disconnect();
        System.out.println("Host Session disconnected.");
    }
}   

With this, i am able to connect to the remote system, but cannot change the user.

Edit-2 : On doing some more research, i could find that using SFTP with JSCH api can help me transfer the file, but not change user. If i change the Channel to 'exec', i can change the user, but both do not work in same session. So both do not work simultaneously.

Is there any another way (SSH, SCP transfer perhaps?)

So the question remains unsolved - Want to transfer file and change user through Java.

8
  • You'll need to show us what you've tried in order for us to help! Have you tried this Commented Sep 30, 2018 at 17:34
  • Thanks Nicolas. Will try out the link you have posted. I have already posted the link, that i have tried out. As the code is similar, i avoided doing a copy-paste. stackoverflow.com/questions/14830146/… Commented Oct 1, 2018 at 3:24
  • Show us the code you've tried not the link! Commented Oct 1, 2018 at 3:26
  • Have edited my qs. Hope it helps. Thanks Commented Oct 1, 2018 at 3:38
  • Removed my down-vote. What do you want to do after changing to super user? Commented Oct 1, 2018 at 4:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.