0

I need to connect a remote computer via ssh and then make a telnet connection to its port

(telnet localhost #port)

with JAVA

I tried some libraries (sshd, ganymed) and succesfully login to remote computer but cannot login to telnet. The problem is that the libraries open a ssh connection and run "one" command at a time. However telnet login is interactive. It asks for username then i'm writing it to stream (with EOF) but hangs there forever

I cannot find a proper way for implementation. Any advice?

(using linux)

hgoz
  • 641
  • 6
  • 18

3 Answers3

1

You could try to tunnel telnet over SSH, then you could open the telnet session on the original host running the java program. Not sure if/how you could do that in Java though.

In putty you can configure the tunnel in connection/ssh/tunnels, there you can enter a remote port (for example 23) and a local unused port (lets say 2323). Then you can open a telnet connection on the original host to 2323 which gets forwarded to the remote host port 23 via ssh.

styx
  • 421
  • 8
  • 15
  • need a java solution :( I tried that way in java but can not login telnet. It asks for username then i'm writing it to stream (with EOF) but hangs there forever – hgoz Nov 04 '11 at 09:07
  • but if the telnet server is local the java program can communicate with it, right? i.e. the added ssh-layer causes your problem. or does the telnet also not work locally? – styx Nov 04 '11 at 09:25
  • it can also work locally but the problem is not SSH layer.Also problem is not about communication. java program can not make stream read-write properly during telnet login. – hgoz Nov 04 '11 at 09:50
  • @hgoz, you shouldn't send an EOF but a CR or CR/LF (a.k.a. \n in java), as the telnet server expects a user to press the enter key. This might well cause all the problems you're having. – AVee Nov 04 '11 at 14:16
0

Can't you just call Runtime.exec("ssh telnet "), which should drop you straight into the telnet session you need. Try it on the shell. Runtime.exec() returns a Process object which you can use to read from and write to the resulting Process object.

Also, if you are indeed sending EOF after the username that likely is the cause of your issues. You should send CR or CR/LF after the username.

AVee
  • 3,348
  • 17
  • 17
0

I made a Ganymede SSH-2 plugin for the JTA - Java Terminal Adapter. Very simple. You could run it up yourself, or if you contact me off list I can provide it to you. It's been running in production for a year or so, very stable.

user207421
  • 305,947
  • 44
  • 307
  • 483