1

I´m working with application in java. I can execute linux command (bash) on my machine host, but i want to execute this command in a remote machine like ssh. I´m ussing this code

Process process = Runtime.getRuntime().exec(script);
    process = Runtime.getRuntime().exec(script);

    BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
    String line;
    while ((line = in.readLine()) != null) {  
            System.out.println(line);
    } 

How i can execute linux shell in remote machine with java code?

3
  • 3
    Have you looked at an ssh library for Java like the one in this answer: stackoverflow.com/a/996415/4687135. Also, perhaps your script can do the ssh part for you? Commented May 18, 2015 at 14:59
  • This code it's running in a windows machine and i need execute bash script in a Linux remote machine. I'll try that library Thank you!! Commented May 18, 2015 at 15:06
  • someone knows a method for remote connection? I can't connect yet Commented May 19, 2015 at 8:29

1 Answer 1

1

Luis: as Eric suggested one possible solution is to run a local script that performs an SSH itself on the remote server.

For instance if you have a Linux->Linux environment, your script you could have something like:

ssh remoteuser@remotehost 'bash -s' < localscripttoexecuteremotely.sh

In a Windows->Linux scenario you could do:

plink remoteuser@remotehost -m localscripttoexecuteremotely.sh

Take a look at this thread for additional information.

Sign up to request clarification or add additional context in comments.

1 Comment

That´s right Execute a remote script it's the best way

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.