Ganymed SSH2 Jump host - java

I'm using the ganymed lib to access remote hosts in my network. But, in this network has routers and switches that can only be accessed by another host.
To illustrate this, in my network there is a Linux server and connected to it there is a Cisco router. The connection to the Linux server worked, but i try to execute the command:
ssh user#ip
After this i use the session.getStdin.write() for executing another command in the session and make the login in the password prompt. But, after the execution of the first command (ssh), the program still running and doesn't send any output.

Related

Need help in java Port forwarding in socket (AWS CISCO router with AWS Server)

I am having a aws server(10.10.10.1) which will act as a CISCO Router and it will port forward to another aws server(10.10.10.2). Now I am connecting a server in ssh using the following command.
ssh -i server.pem ec2-user#10.10.10.1 -p 2222
Then it will connect to the Server 10.10.10.2.
Now I need to run a socket program there and I need a client(Which will run in local) program to connect that.
I tried in many ways but I am not able to connect that.
I am new in socket programming please help me to sort out.
Below Answer Result:
I have tried to connect through the SSH tunnel and the tunnel is working for the router and it is not going to the aws server
Please see my tunnel command,
ssh -i server.pem -L 9080:10.10.10.2:8090 ec2-user#10.10.10.1
I also ran a Socket server code in 10.10.10.2 and the client code in my local machine.
Please help me to sort it out
This is your layout
DEV ---ssh---> PIVOT ---ssh---> SERVER
There's an SSH feature called port forwarding that allows the user to open a listening (server) socket on one end of the connection (-L for local, -R for remote) and proxy all socket reads and writes to another machine reachable from that end.
Since PIVOT can connect to SERVER you simply need a proxy on your DEV machine:
me#dev$ ssh -L 9005:server:80 pivot
And then when you talk to localhost:9005 on the development machine you are in fact talking to server:80 via the SSH channel.
Note: if host A can't directly connect to the server and you use it to ssh into B that can, you actually have to setup two forwards, one for each SSH bridge.

Command execution in ubuntu

My server is running on OS ubuntu 10.4.
When I run command "skype -callto userid" it makes a call using Skype user interface.
But when I run same command by connecting via SSH or
java code
Runtime.getRuntime().exec(command), its not working.
What could be the reason of such behavior?
I guess your problem is that Skype needs an X server to run properly. So you could enable X11 forwarding.
X11 forwarding needs to be configured on the client and server side:
On the server side enable X11 forwarding by setting X11Forwarding yes in /etc/ssh/sshd_config and restarting the ssh service service sshd restart. You might also need to install xauth on the server side.
On the client side you need to open the ssh connection with the -X parameter to enable X11 forwarding for the session.

Run JConsole from server over SSH

I deployed a Java app to server and need to run Jconsole for profiling. I connected to server over SSH and run jconsole, however I got an error
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
How can I run JConsole over SSH on my client machine?
when connection to the server, enable X forwarding with ssh -X $servername

Java exec or runtime not working with ssh

I'm trying to ssh into ubuntu using exec, but for some reason when I execute from the code I get the error
port 22: Connection refused
In the code I use concat to put the strings together, but I know they're put together properly because I print them out and if I copy and paste them into the command line then it will ssh properly.
My code tries:
p1= Runtime.getRuntime().exec(run1);
p1.waitfor();
where
run1 = "ssh -o StrictHostKeyChecking=no -v -i key " + "ubuntu#"+ DNS + " sudo mke2fs -F -j "+device;
Any ideas?
You are initiating the connection, so for it to be refused it means that the machine you are attempting to ssh into is denying you ssh access.
Log into that machine by whatever means you have and verify that the ssh server is running. If it is, then verify that the firewall is not blocking port 22; because, sometimes the ssh server is running but the firewall won't allow network access to the ssh server due to a port blocking rule.
--- Edited after question in comments ---
Is there a difference between ssh in the command line and using exec? Because I can
connect to the server through the command line, which I assume is still using port 22.
So if I can ssh that way does that mean port 22 is working?
There are a few possibilities. Java comes with a Security Manager which only serves to deny programs access to machine resources. This is why it is possible to safely run applet code, which is downloaded from remote servers, as the Security Manager denies permission to access the hard drive or make connections to other machines. In the applet sandbox, it does allow connections back to the originating web server (to download more code and images).
However, the lack of a security exception directs the suspicion away from the Security Manager. The fact that the message uses the words "Connection refused" is a strong indicator that the SSH server you are connecting to won't accept a connection from you.
Perhaps by operating on the command line, the ssh command is using a different environment or configuration. I would see if the command is aliased, of if the ssh connection makes some assumption about key pairs. If nothing seems to be out of line with the command, I would check that your program is connection with ssh version 2 (version 1 is not allowed by many due to a security hole).
Then I would also hunt around for possible differences in name resolution. You might be resolving the hostname in the command line differently than you are resolving it from the Java program. This could mean that the Java program is attempting connection to a different machine, one which doesn't have a secure shell server running.
Either way, it seems that you'll have to do a bit of debugging to isolate if it is a true coding problem or an environmental issue.
If you are getting Connection refused, the SSHD is not running or you are being blocked by Firewall (or similiar).

SSH tunneling with JDBC on Mac OS X - Problems

I am currently developing a java application that uses JDBC inorder to connect to MySQL on a computer running Mac OS 10.6.7. The server that I connect to suddenly decided to use SSH. I've managed to connect to the database using MySQLWorkbench so there is nothing wrong with the connection.
My next attempt was to start the terminal and type ssh username#sshserver.com -L 3305:databaseserver:3306 and then start Eclipse and run my application. The terminal prompted me for my password but JDBC could not connect. I tried SSH Tunnel Manager with the following settings: http://imageshack.us/photo/my-images/146/sshb.jpg/ and it also prompts me for the password and then gives a green light. Still no success. The application worked fine before when i just connected without SSH. I would be very grateful for any help, I googled and tested every suggestion I found but nothing worked.
Two issues to keep in mind using tunnels:
Remember that when you use a tunnel, your JDBC client should connect to localhost port 3305 (or 3306, whatever you're actually using). It shouldn't continue trying to connect directly to the remote host.
SSH will tunnel the connection to the remote end and try to connect to "databaseserver" port 3306 from there. If ssh on the remote host can't resolve the hostname that you gave it, it won't be able to make the connection. Since you are tunneling to the database server, you could try connecting to "localhost" or "127.0.0.1" instead.

Categories

Resources