1

I have 3 computers in the same network. I want to write a java program in order to run some scripts on the other machines. My machine runs Windows and the others run Linux and Windows respectively.

Any ideas about it? I show some solutions about remote machines but I hope that there will be an easier way because my pc are in the same network.

2
  • Do you mean you want to trigger those scripts using a Java program? Is this trigger going to be a manual one? I mean, do you intend to set up a cron or scheduler to trigger it? Commented Aug 17, 2011 at 11:07
  • its a manual one... I want to run the java program, click go and everything will begin Commented Aug 17, 2011 at 11:25

5 Answers 5

2

For linux you can use ssh to execute remote command

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

2 Comments

Can you give me an example? Do i need to install anything to the remote machine?
@Nidis - the remote machine needs to have SSH software installed. It is available for windows and linux. In the linux case, it is almost certainly installed already.
1

In addition to @corsair's reply: you can use SSH for Linux, Telnet for both Linux and windows. There are several pure java libraries that implement SSH and Telnet. Take a look on http://www.jcraft.com/jsch/ and http://commons.apache.org/net/

2 Comments

I have a problem with Windows because they refuse the ssh and the telnet connection.
Windows has alternative - PowerShell. Maybe this can help Running Remote Commands
1
  1. Create a serverSocket or RMI / XMLRPC server on each of the machines.

    ServerSocket serverSocket = new ServerSocket(1234);

    while (true) {

    try {
        Socket socket = serverSocket.accept();
        BufferedReader reader = new BufferedReader(socket.getInputStream());
        // exec a command sent by the client
        Runtime.getRuntime().exec(reader.readLine());
        // or 
        // a static command
    } catch (Exception ex) {
    }
    

    }

  2. On client side

    Socket socket = new Socket("serverip",1234);

    OutputStream os = socket.getOutputStream();

    os.write("echo hello");

    os.flush();

    os.close();

    socket.close();

  3. Where you have linux machines you could do

    try {

    Socket socket = new Socket("serverip",21); // connect to telnet port
    
    OutputStream os = socket.getOutputStream();
    
    // wait for server prompt
    
    Thread.sleep(1000);
    
    os.write("username\n");
    
    // wait for server prompt
    
    Thread.sleep(1000);
    
    os.write("password\n");
    
    Thread.sleep(1000);
    
    os.write("~/xyz/run.sh");
    
    os.close();
    
    socket.close();
    
    } catch(Exception ex) {
    

    }

3 Comments

This means that I need to run a script on the server side as well. Right?
sorry i dont understand your question. You can run the server - socket listener on each of the machines where you want to execute the scripts. From client side you can invoke the scripts. Obviously you would have to keep the bat files or whatever on the path of your server app. Hope this answer is useful... With Linus you could telnet at and do the same so when you connect at port 21 on your linux machines you would get a prompt and you can write the bit to log in in your client and run the scripts. With windows you would need the server part - there was a tftp but not sure if it exists now.
so in your client side for linux server/machines you could connect to telnet, do - os.write("<your username>\n"); Thread.sleep(1000); os.write("<your pwd>\n"); Thread.sleep(1000); Runtime.getRuntime().exec("echo hello world or ~/xyz/run.sh &");
0

Java is not the best tool for this. Yet, if you want to do with Java, you need to setup a server in each of the remote machines. A server is basically a process that always is running and listens to a port; from that port it will receive the message to run the scripts.

If the scripts are safe (if they run at the wrong time no harm is done), you can do it just with ServerSocket. If they are unsafe (you need to make sure that only you are the one able to launch the process, I would advise using a web server (Jetty, Tomcat) to use its security capabilities (SSL/HTTPS, authentication).

Comments

0

Don't use Java for this task, if somehow possible. Use a remote management system, like OpenRSM (http://openrsm.sourceforge.net/).

1 Comment

Unfortunately I have to use Java for this

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.