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.

bhargav desai
  • 119
  • 1
  • 3
  • 12
  • You'll need to show us what you've tried in order for us to help! Have you tried [this](https://stackoverflow.com/questions/16343117/java-jsch-changing-user-on-remote-machine-and-execute-command) – Nicholas K Sep 30 '18 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. https://stackoverflow.com/questions/14830146/how-to-transfer-a-file-through-sftp-in-java – bhargav desai Oct 01 '18 at 03:24
  • Show us the code you've tried not the link! – Nicholas K Oct 01 '18 at 03:26
  • Have edited my qs. Hope it helps. Thanks – bhargav desai Oct 01 '18 at 03:38
  • Removed my down-vote. What do you want to do after changing to super user? – Nicholas K Oct 01 '18 at 04:09
  • Thanks. Copy to another directory which has rights for super user. – bhargav desai Oct 01 '18 at 04:21
  • Why not just login as super user? – Nicholas K Oct 01 '18 at 13:04
  • The link you provided in 1st comment did not work out. The issue is not logging in/changing to super user, i need to transfer files as well in the same session, which does not work. Have posted the edits in question as well. Thanks – bhargav desai Oct 02 '18 at 14:23

0 Answers0