简体   繁体   中英

File transfer between remote servers using Jsch

I have a requirement wherein I need to login to a unix server, perform switch user, execute some commands and then scp the files created from these commands to a different server. I'm able to connect to the server, perform sudo login and execute commands however I'm unable to scp files directly to another remote server.

I'm using Jsch jar for this. Below is my code.

public void executeChannel(Session session, String command, String pwd, List file) throws Exception{

    logger.info("************************Start of executeChannel() method*****************************");
    Channel channel = session.openChannel("exec");

    // below line avoids "sudo: no tty present and no askpass program" error

    ((ChannelExec) channel).setPty(true);

    // this line ensures password prompt is not displayed after sudo user
    /**
    * -S  The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device.
    * -p  The -p (prompt) option allows you to override the default password prompt and use a custom one.
    */

    String filename = file.toString().replace("[", "").replace("]", "").replace(",", "");   
    System.out.println("sudo -S -p '' "+command+" "+filename+" server2:/dir1/dir2/dir3/");

    ((ChannelExec)channel).setCommand("sudo -S -p '' "+command+" "+filename+" server2:/dir1/dir2/dir3/");

    channel.setInputStream(null);            
    ((ChannelExec)channel).setErrStream(System.err);

    InputStream in = channel.getInputStream();
    OutputStream out=channel.getOutputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    channel.connect();
    out.write((pwd+"\n").getBytes());


    byte[] tmp=new byte[1024];
    while(true){
        logger.info("start of while(true) method, is channel connected? "+channel.isConnected());

        br.readLine();
        while(br.readLine() != null){
            System.out.println("channel.isConnected() "+channel.isConnected());
            int i = in.read(tmp, 0, 1024);

            if(i<0) break;

            System.out.println("printing putty console: \n"+new String(tmp,0,i));
        }
        if (channel.isClosed()){
            System.out.println("Exit Status:- "+channel.getExitStatus());
            break;
        }

        try{
            Thread.sleep(1000);
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    out.flush();
    br.close();
    System.out.println("DONE");

}

command = sudo su -pentaho; cd /dir1/dir2/; ls -lrt; scp On executing above code, commands until ls -lrt are executed properly and I can see the output. However after this the code hangs. There is no exception thrown by the code, hence I'm at a loss as to whats happening.

Any help would be highly appreciated.

Turns out I wasn't printing the output of putty console properly. I figured out the issue. Solution was to change the command.

Removing sudo -S -p '' from the command and replacing original command with sudo su - user -c "my commands" did the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM