1

I spent a lot of time developing an application that would use JSch and connect to a remote machine thru ssh to perform some command-line operations. However I learned that these operations can be performed at the localhost as well (my app is running on localhost). Now... I am too lazy to rewrite all the code and honestly I feel bad since I got really attached to JSch. Is there a way to trick JSch to connect to localhost instead or tell it in some way to just use localhost even though the code says otherwise? :)

P.S. in case it's not possible, how come the regular Proccess class doesnt support setOutputStream and setErrStream like JSch does, but only getInputStream and getErrorStream ??

1 Answer 1

6

As long as your local machine has an SSH server running (and your application has the necessary login credentials), you can use JSch to connect to your local machine, too - simply indicate localhost (or 127.0.0.1) as the host name for the connection.

This will have some overhead, though, since you are encrypting and decrypting all the data, which is not really necessary to execute some command locally. (On the other hand, this would allow you to run the commands as another user, for which you otherwise would need something like sudo or su, or RunAs under Windows.)

JSch implements the setOutputStream and setErrStream on top of the corresponding get... methods - it uses something similar to a PipedInputStream internally and a separate thread which shovels the data between those streams.

As JSch is open source, you can simply look how this is done (in the Channel class, if I remember right), and copy the relevant methods to your class which does the same things for a Process.

Is there a way to tell JSch not to encrypt the data?

You can use the none cipher, e.g. no encryption. This is by default disabled in all general-purpose clients and servers (as it defeats half of the purpose of SSH), but with the right configuration you can enable it. In JSch you can use

session.setConfig("cipher.s2c", "none,...");  // server to client
session.setConfig("cipher.c2s", "none,...");  // client to server

(This configuration option is the list of all options the client supports - see the documentation of setConfig for all supported values. The server will normally select the first one of this list that it also supports. To force no encryption (or canceling the connection), list only none.)

I don't know how to enable this in the SSH server - read your server's documentation. (And enable it only for localhost, if possible.)

The recommended way of using it is to switch to the none cipher only after authentication (so the authentication is still encrypted), but for localhost this might not be necessary. (You can use session.rekey() to switch the cipher (and key) after changing the configuration.)

6
  • is there a way to tell JSch not to encrypt the data? Aug 18, 2011 at 17:35
  • i realize that ssh was meant for encryption, but still Aug 18, 2011 at 19:45
  • @Martin: I added some stuff about the none cipher. Aug 18, 2011 at 22:44
  • thanks. do you think what I am trying to do is a bad practice though? meaning should I rewrite the code instead of ssh-ing to localhost? Aug 19, 2011 at 2:55
  • It is bad practice only in so far as you are adding a dependency to an SSH server (and some not-so secure settings). On the other hand, it allows you to switch to remote access quite easily. I would try to clone the setOutputStream methods from JSch to Process, to be able to reuse the same code as you used to interact with your JSch channel. Aug 19, 2011 at 14:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.