I have embedded a JSCH SSH Java applet in a web page and need to know if it's possible to run a script (of any language like PHP) to automate logging in and running commands. I have heard of expect4j and java robot but cannot see any way to implement it. Keep in mind, I'm not great with Java so I don't know everything about it. Any help is appreciated.
-
Do you really want the applet to log in into your (or someone else's) server and execute commands each time someone visits your web page? And the output then should be shown to the visiting user? Or what do you actually want to do? – Paŭlo Ebermann Feb 17 '12 at 20:23
-
It needs to log in with a set password automatically via ssh. All this is on the same LAN. It's not going outside anywhere. First, I validate the IP address, then I need the applet to log in to that IP address. – Rodney Wilson Feb 17 '12 at 21:02
-
If I can do it by another method, I'm open for that as well. I'm not stuck to JSCH. I just need it to go through a webpage. – Rodney Wilson Feb 17 '12 at 21:03
-
Do you need the terminal user interface from the applet, or only some output shown on the web site? Is the web page delivered on the same server as where you want to log in? – Paŭlo Ebermann Feb 17 '12 at 21:13
-
I don't need a UI per se. I just thought it would be easier to show an applet and make sure everything was done. The login and commands can be done in the background, if possible. And then the output shown to the end user. No, the webpage won't be on the same machine. It'll be on a static website. – Rodney Wilson Feb 17 '12 at 21:17
-
I thought about using JTA terminal since you can edit the .conf file to log in automatically, but never could get it to work. Is there a way to make a Java class and run it in the background (or in some other language) and output the results to the webpage? Thanks for the help btw. – Rodney Wilson Feb 17 '12 at 21:20
1 Answers
JSch is an SSH client library, and by itself only allows programmatically steered connections to another server. The user interaction has to be build around it by users of the library.
The JCTerm applet provided on the website also contains a terminal emulator in form of a Java GUI. If you only want to automatically execute some command (and maybe show its output in the web page), you could do everything on the server side, and don't need the applet with its terminal emulator. (You would need either some PHP-Java bridge on the server side or some Java-enabled webserver with a Servlet or similar, though.)
(If the web server would be the same machine as the server you'll run the command on you wouldn't even need the SSH connection, but could execute the stuff directly.)
If the server can't do anything (i.e. a "static server"), an applet is the way to go, yes. You can either modify JCTerm or create a new applet from scratch (using JCTerm's connection code as an example on how to connect to to the server).
If you don't have to fear any malicious users in your LAN (i.e. between web server and user, the SSH server doesn't matter), you can embedd the password (or preferably a private key for public-key authentication) into the applet's jar file, and pass it to the library for connection. (You should also include the server's public key for easier checking.)
Provide the command(s) to a ChannelExec
(instead of a ChannelShell
), this makes it easier to provide input (if necessary) and capture the output. Pipe the output in a text area, or simply use a green/red label saying if the command was successfully executed.
(I might have a look at this in the next days and try to do it. No promise, though.)

- 73,284
- 20
- 146
- 210
-
So modify the current applet's jar file with an embedded user name and password? – Rodney Wilson Feb 17 '12 at 21:41
-
Tell me if I have the steps correct for adding code. Like I said, I'm not great with Java. Add lines to the class file in the jar files. Recompile it back into jar and add it to the applet? Is that the way to go? – Rodney Wilson Feb 17 '12 at 21:43