Indirect telnet connection with Java - java

Scenario
I'm in a Java project where we have to communicate with the CLIs of other machines. Unfortunately, we can't connect to these other machines directly and another bad luck is that they only support telnet. So we have the following setup, which is carved in stone (of course):
application <---- telnet or ssh ----> gateway <---- telnet ----> machine_001
(10.0.0.1) (192.168.1.1) (192.168.2.1)
(192.168.2.2)
( ... )
It's possible to connect via SSH or telnet to the gateway manually (e.g. using PuTTY), telnet from this shell to one of the machines and work with its CLI. As we want the communication to happen automatically, the application must be able to talk to the machines by itself; so I need a programmatic solution.
What I've tried so far
After some research on the internet I've found a library called JSch which looked promising, but I've encountered an evil problem. When the applications connects to the gateway, the telnet command and therefore the whole CLI of the target machine is one single command from application's viewpoint. So I'd have to struggle with a non-terminating InputStream, unsynchronized OutputStream and Threads if necessary.
The next try was to establish a SSH tunnel from L127.0.0.1:1234 to 192.168.2.1:23 (via the gateway), but with this configuration it's not possible to telnet to 127.0.0.1:1234 (neither programmatically nor manually).
The actual question
How can I get my application to talk to the machines via the gateway using telnet?

Related

Java SSH Client like PuTTy

So, I want to create my own SSH Client, not just sending commands but a full on ssh client for me to use like you would use PuTTy or mobaxterm, can someone here point me in the right direction.
Things I tried / thought of:
- Using Jsch to send and execute commands (If its possible I would love to know if this is able to be done without using Jsch).
SSH is a protocol that is defined to run on top of TCP, and you can make TCP connections in java using the java.net.Socket class.
So, yes, it can be done.
You'd have to implement the protocol yourself (or use JSch to do it, but given that all these tools already exist, and it's security, where in general relying on widely used tools is orders of magnitude more intelligent than relying on hand-written stuff, given that a security bug is very hard to test for – I assume this is some sort of exercise in sheer dogged arrogance, or, more likely, a learning exercise, which'd mean that you wouldn't want to use jsch here).
ssh works by sending any number of streams across the network, encrypted. By default, there is one stream (a two-way stream), with the user's shell being on one end (so, the /bin/bash executable, for example), and a terminal (where what you type forms the input, and any output is echoed to the terminal window) on the other.
ssh does NOT wait for you type a command then run it; that's /bin/bash doing this. This is basic linux info and has no direct bearing on ssh (the protocol), that's just what you're used to, because sshd (the linux executable that forms an ssh server) by default hooks your terminal up to the target server's shell executable. Usually bash.
Given that you know nothing of this, this sounds like a project that's a bit too far fetched as a starter experiment, but I'm going by incomplete information to make this assessment.
I'd start by making a trivial telnet client and server. telnet is at this point extremely outdated but it is basically ssh without the multiplexing of streams nor the encryption. If you can't write a telnet client, you can't write an ssh client either. And you can still start telnet servers on linux machines for testing purposes.
Once you've got that down, start figuring out the crypto and the multiplexing*.
*) With ssh you can, whilst having a shell open, also port forward and such; check out the -R and -L and -D options in standard ssh. Each such option allows for another stream to be sent along. (whilst you're sending shell commands and seeing their results, at the same time, over the same ssh connection, you are, say, sending a print job to a printer in the network of the other machine). Sending multiple streams through a single connection is called 'multiplexing', and it is part of the ssh specification.

what could I use to wrapp a SSH server for a Telnet client

In my case, the server has been secured by passing all console access to SSH,
but the client console must remain with Telnet because it is integrated in an application that can't be modified.
Due to incompatibility of protocols I imagine a "proxy" service in order to manage the SSH protocol including authentication and encryption decoding,
that would redirect the shell dialog in plain-text to a local socket (no file transfer or other feature needed).
Any advice about solution based on Windows utility (putty, netcat ...) or JEE service dev will be welcome.
I can only install something on the Windows client system.
Thx

How to establish ssh connection to postgresql via Java? [duplicate]

We are implementing an university project: a car-pooling service in Java.
We need to solve a problem linked "how to manage a postgres server":
the PostgreSQL Database is configured in a lab server called "golem" (130.136.4.sth) reachable only through terminals in the same subnet (130.136.4.0).
We have four account (ours) through we can establish a ssh connection to an host.
Is it possible to make SQL queries through SSH towards Postgres DB in JAVA?
Thank you :)
Davide
If this is just for development, you can use ssh port forwarding to access the database as if it was installed locally. How port forwarding is enabled depends on the client software you use, openssh for example has a command line switch for it (-L):
ssh user#host -L localport:remotehost:remoteport
This command would make the remoteport on remotehost, though accessible only through host, available on localport on your computer.
Take a look at the other suggested answers as they seem easier to accomplish what you need.
However, if you really need to implement the command submission with Java for your lab assignment, you can take a look at the JSch (Java Secure Channel) library found here: http://www.jcraft.com/jsch/ Examples are here http://www.jcraft.com/jsch/examples/
With it you can submit ssh commands and perform any kind of operation via a Java API
If you run "ssh" followed by any command that command gets executed on the remote host. So you should be able to run pre-baked queries in batch mode via ssh.
Consider doing key-gen and key exchanges to enable passwordless ssh execution.
Example (this just dumps a directory listing to your terminal):
ssh me#mybox ls

Correct way to create an ICMP request on Windows platforms

I'm trying to execute an ICMP request in Java on Windows. Initially I planned on using InetAddress.isReachable(). In the documentation it says:
A typical implementation will use ICMP ECHO REQUESTs if the privilege can be obtained, otherwise it will try to establish a TCP connection on port 7 (Echo) of the destination host.
I know in Linux, you have to have root privileges to open a raw socket, therefore you need root privileges to ping. But is that the case in Windows?
I did some investigation and realized that maybe I should just use runtime.exex(ping_command), but then I found some tools like shortPasta
or Verax that are supposed to better implement ICMP requests in Java.
I noticed that both execute the ping binary like I wanted to do, but only in Linux; in Windows they used JNI.
I was wondering, is there some principal limitation for not executing the ping binary in Windows?

Communicating with an SSH Server for file access using pure Java

I'm writing a program in Java using the sockets to communicate with a Telnet server which allows the users to access the file directory in an UNIX OS.
When using Putty to communicate with this server, it prompts me for my username and password, but using my sockets there is nothing from the server except for a string which states that it uses SSH 2.0 - I think.
I'm sure that this has to do with the Telnet protocol, but how do I get the server to ask me for my username and password. What set of commands would I need to give the server in order to access the file directory in an UNIX environment?
Correction:
I figured that it's actually using SSH on port 22. It can be accessed using Putty or Microsoft Windows' Telnet program, but it doesn't actually use the Telnet protocol but the SSH protocol.
SSH isn't telnet. SSH is a protocol that is a lot like telnet, but is encrypted and has a slew of other features. So it looks like youre expecting a plain-text exchange, but what you're getting is the ssh protocol trying to do a handshake.
Telnet runs on port 23, SSH on 22. I imagine you want to use 23. Note: Telnet is old and unencrypetd and dangerous to use over the internet (unless youre going over a VPN or something that encrypts the session).
There is really nothing to the Telnet protocol for most uses...see this page for details. If the server on the other end is trying to negotiate a SSL connection, which is by far the most likely thing these days, try using a java.net.ssl.SSLSocket instead of a bare TCP socket.
Once you negotiate the connection (see the docs linked above) you should essentially print UNIX CLI commands to the socket and read (& parse) the results. If you just want to access files, maybe use FTP instead. Most modern servers are going to support SFTP.
Edit
With a little poking I found that using SSLSocket directly to connect to a SSH server is cumbersome at best because SSH has its own protocol. You probably don't want to reinvent the wheel on that one. Check out the answers to this question for some pure Java SSH client libraries. You can probably use at least one of these to solve your problem more directly than sending text commands over SSH.
Would it be a telnet server, it would be simple.
But what you have is a ssh server, and that is good as it is. telnet is heavily deprecated, as it is not encrypted.
You now have two options: Either use a ssh library or access the ssh command line client (or under Windows: the plink program) via its stdio.

Categories

Resources