How can I execute Linux commands on a remote machi

2019-01-18 14:44发布

问题:

I have two secured linux servers. In one machine my Java application is running. I need to run Linux commands on second machine from first machine in Java. How might I do this?

回答1:

Jsch (here) allows you to connect to a remote server using SSH and executes shell commands easily (and lot of other things like SCP, SFTP...). There is not a lot of documentation, but you have a few really helpful implementation examples here (and even an example of what you want to do here).

You can also combine Jsch with Expect4j and this way have a better control on the commands you want to execute (nice example here).



回答2:

Essentially, you need to open an ssh connection to the other server from within your Java application. The OpenSSH site has some useful information on libraries that will give you ssh support in Java.

It looks like Ganymed SSH-2 for Java is the nicest of the pick there, but I haven't used any of them so you will need to look at what you need.

Once you have an ssh connection, you will be able to run commands just as if you logged in using any other ssh client.



回答3:

You can do it a number of ways; however, nearly every way involves a network connection.

You could write a client-server pair of Java programs, with the client connection to a server and submitting the command.

You could write your Java to use an existing server, like sshd, telnetd, rsh, ftpd, or a pre-existing other server which allows commands at the remote end.

You could leverage an architecture which handles certain aspects of establishing a client-server pair, like RMI, SOAP, CORBA, etc.

In the end Java supports tons of networking options, so you have more ways of doing this than you think. Just make sure you don't do it in a web browser, as those JVMs are launched sandboxed, and you can't get out of the sandbox without some assistance.



回答4:

It might be easier to check out Sockets, as you can do what you're trying to do without having to get any external libraries set up.

On the host machine, you want to set up a ServerSocket object, and from the client machine you open a Socket. I don't have time to type up a whole example, but check this out for a simple way to set up a server-host connection over the Internet in Java.

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

Once you get that set up, you want to input your shell command from the ServerSocket on the computer that should execute the command, and do something around the lines of

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 setting up a realiable host-client connection with the Sockets, but if you're doing something simple you should be fine with the example from the link above.



标签: java linux ssh