Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSH in Java App with 'expect' like functionality [closed]

Tags:

java

ssh

expect

With 'expect', one can execute SSH commands and parse the output of those commands to alter program flow. I'd like to do this with Java. That is, I want my Java app to launch a SSH session, execute a command on the remote server, and depend on the output of that command execute the next command, without needing to start a new SSH session.

Is this possible in Java?

Thanks

like image 894
jtnire Avatar asked Jun 18 '11 08:06

jtnire


People also ask

How do I SSH into Java code?

Implementation. As we can see in the code, we first create a client session and configure it for connection to our SSH server. Then, we create a client channel used to communicate with the SSH server where we provide a channel type – in this case, exec, which means that we'll be passing shell commands to the server.

How do you use expect in Java?

Expect for Java is a pure Java implementation of the Unix Expect tool. The target is robust, easy-to-maintain code, with only one class file. In order to use this tool, you need to know Java regular expressions and have some basic ideas about the original Expect tool.

How do I run a Java program on a remote machine?

In the Host field, enter the IP address or domain name of the host where the Java program runs. If the program runs on the same machine as the workbench, enter localhost . In the Port field, enter the port where the remote VM accepts connections. Generally, this port is specified when the remote VM is launched.


1 Answers

Self-promoting my project: after looking at other Java-based Expect libraries, I decided to write my own... hope you find it useful. Please open issues in GitHub if you find any problem.

https://github.com/ronniedong/Expect-for-Java

For SSH, I suggest using JSch, as the example in the readme file.

like image 150
Ronnie Avatar answered Nov 16 '22 06:11

Ronnie