How can I execute Linux commands on a remote computer using Java?

I have two secure linux servers. My Java application is running on the same computer. I need to run Linux commands on a second machine from the first machine in Java. How can i do this?

+6
source share
4 answers

Jsch ( here ) allows you to connect to a remote server using SSH and easily execute shell commands (and many other things like SCP, SFTP ...). The documentation is not enough, but you have some really useful examples of implementation here (and even an example of what you want to do here ).

You can also combine Jsch with Expect4j and thus have better control over the commands you want to execute (a good example here ).

+8
source

Essentially, you need to open an ssh connection to another server from your Java application. The OpenSSH website has some useful library information that will provide you with ssh support in Java.

Ganymed SSH-2 for Java seems to be the best choice, but I haven't used any of them, so you need to see what you need.

If you have an ssh connection, you can run the commands in the same way as if you were logged in using any other ssh client.

+4
source

You can do this in several ways; however, almost every path is connected to a network connection.

You can write a couple of programs for a Java client with a client connection to the server and send a command.

You can write your Java to use an existing server, such as sshd, telnetd, rsh, ftpd, or a pre-existing other server that allows commands at the remote end.

You can use an architecture that handles certain aspects of creating a client-server pair, such as RMI, SOAP, CORBA, etc.

After all, Java supports many network options, so you have more ways to do this than you think. Just make sure you don't do this in a web browser, as these JVMs run in the sandbox, and you cannot exit the sandbox without any help.

+2
source

It might be easier to check the sockets, as you can do what you are trying to do without creating any external libraries.

On the host machine, you want to configure the ServerSocket object, and from the client machine, you open Socket. I don’t have time to print a whole example, but check this out for an easy way to set up a connection between a server and a host over the Internet in Java.

http://zerioh.tripod.com/ressources/sockets.html

Once you get this setting, you want to enter a shell command from ServerSocket onto the computer that should execute the command, and do something around the lines

String command = "get this from the ObjectInputStream attached to your ServerSocket"; Runtime run = Runtime.getRuntime(); Process pr = run.exec(command) ; pr.waitFor() ; BufferedReader buffer = new BufferedReader( new InputStreamReader( pr.getInputStream() ) ) ; String line; while ( ( line = buffer.readLine() ) != null ) { System.out.println(line); } 

The tricky part is making a reliable host-client connection with sockets, but if you are doing something simple, you should be fine with the example from the link above.

0
source

Source: https://habr.com/ru/post/900403/


All Articles