简体   繁体   中英

Run linux command in remote machine from java

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?

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.

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