Working with I / O Streams Using Runtime.exec

I am trying to run a command scpin Java. Here is my code

try {
    Process p = Runtime.getRuntime().exec("scp -P" + PORT + " " + FILEPATH + " " + USERNAME + "@" + HOST + ":somefolder/");
    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
    String line;
    while ((line=br.readLine()) != null) {
        System.out.println("LINE: " + line);
        if (line.contains(USERNAME+"@"+HOST+" password:")) {
            bw.write(PASSWORD);
            bw.newLine();
        }
    }
    System.out.println("end of while.");
} catch (IOException e) {
    e.printStackTrace();
}

As you can see, I do not want to type the password blindly on p.getOutputStream()what is offered in almost all similar issues. I want to read the prompt and act accordingly. For example, scp might call something like "The authenticity of the host, SOME HOST" cannot be set ... ", which asks for" yes / no. " Or something else that I can’t imagine right now.

The problem with my code is that it never reads a line, although scp asks for the password that is displayed in cli. Any suggestion?

EDIT: , ProcessBuilder, String [], . :

package scptest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class STest {

    // args => filepath, username, host, port, password
    public static void main(String[] args) {
        try {
            ProcessBuilder pb = new ProcessBuilder("scp", "-P", args[3],
                    args[0], args[1] + "@" + args[2] + ":folder/");
            Process p = pb.start();
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
                    p.getOutputStream()));
            String line;
            bw.write(args[4]);
            bw.newLine();
            bw.flush();
            System.out.println("RETURN: " + p.waitFor());
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

}

, scp . ! , jar commad, " @host password:", . Enter, , - , , Process, My BufferedWriter .

w8- jdk 1.7.0_45, Ubuntu x64 jre 1.7.0_51.

+4
1

, rsa -i scp.

-i, HOST. , , HOST.

, . , -q -o StrictHostKeyChecking = no -o UserKnownHostsFile =/dev/null, - ( !)

0

All Articles