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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With