Java Web service using ssh (remote linux connection) - java

I am more than novice in Linux. Nevertheless, I need to create a Java Web Service hosting in apache server in a Linux system. The Web service must use ssh to connect to a remote machine, create a txt file, execute a prog.exe (compiled C program in MPI) and then retrieve and return a single output value. The only thing that I do not know is how to connect remotely with the Web Service. In a shell will use something like:
ssh username#remotemachine
and then we will get a prompt for password.
Is it possible to send the password along with the ssh command? I have read that it is possible to connect in one shot with public/private keys but this project is my Master Thesis one and as a result, the machines are these of the uni. So, I do not want to mess with the technicians because most of the times simple do not help at all.
Thanks very much

Most Ssh clients will recognize the following
ssh username#remotemachine -pw'YourPassword'
However, I used GanymedeSSH for Java and it had a method like this:
conn = new Connection(servername, 22);
conn.connect();
conn.authenticateWithPassword(username, password);
session = conn.openSession();
And as long as you keep reference to your session, you will be able to use it to execute commands on the remote machine.

You could use the library Jaramiko to get over the problem (instead of calling ssh externally).

Related

Running Unix command , after connecting through ftp using java

I am not authorized to use ssh/sftp( using private/public key). So ftp is my only choice.
The following piece of code works just fine for me, fetching the file from Unix box, but my motto is to log in to a UNIX box from windows, using java,then from my home directory go to a different directory and use grep, then copy that output back to my windows java program. I was looking for how to execute some Unix command in the box. as we do it in shell/python/ant...
new URL("ftp://user:password#url/sourcefile;type=i");
URLConnection con = url.openConnection();
BufferedInputStream in =
new BufferedInputStream(con.getInputStream());
FileOutputStream out =
new FileOutputStream("Targetfile");
If you have username and password then you can go for Jsch library.
Have a look at this or directly run it !!
http://www.jcraft.com/jsch/examples/Shell.java.html
Similarly you can all shell commands from this.
FTP is a file transfer protocol. It's not a general-purpose remote access protocol. It doesn't have built-in support for a client to run arbitrary commands on the FTP server.
FTP does have a command called SITE which permits running custom commands on the server. To use it, the FTP server's administrator would have to set up a custom command that meets your needs. Then you'd have to use a real FTP client library to invoke the site command on the remote server--calling openConnection() on an FTP URL won't let you invoke site commands.

To connect to remote linux system using Runtime.exec with Putty private key

I wanna programmatically (using Java's Runtime.exec) open Putty on my system and connect to a remote linux system. Is there any syntax to pass Putty private key to the method runtime.exec and connect successfully. I am getting "Access Denied" error on the remote system if I just pass the username and pass-phrase and try to connect.
I am assuming by your comments that you are trying to actually run some commands on a linux server, like
find /some-directory/ -type f -ecec grep -Hnw this-word {} ";"
via bash or other shell as opposed to rcp.
If yes then stop using putty, that is an interactive client for you to login to a remote server and interact with the connected shell.
I would suggest that you would be far better off using something like jssh, assuming it is still a current library.
With that you get fine control of the interaction flow with the remote server and iirc it supports private/public keys.

Execute java code on remote system via ssh

I want to run a java program on my local PC which connects to a remote PC with ssh and executes some methods (java code, not bash scripts!) there. I found for example JSch which enables the ssh connection with and remote execution of code with something like
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, 22);
session.setPassword(passwd);
Channel channel=session.openChannel("shell");
channel.setInputStream(System.in);
(from http://www.jcraft.com/jsch/examples/Shell.java.html)
or ssxcute which can be used with
ConnBean cb = new ConnBean("ip ", "username","password");
ssh = SSHExec.getInstance(cb);
ssh.connect();
CustomTask sampleTask = new ExecCommand("echo 123");
ssh.exec(sampleTask);
(from https://code.google.com/p/sshxcute/)
but what I want to do is something like
...
ssh.connect();
MyExecuteClass execClass = new MyExecuteClass();
ssh.exec(execClass.runLongJob(a, b, c));
MyReturn return = ssh.getReturnValue();
Is this possible? The objects a,b,c would have to be transmitted to the remote system, as well as all global variables, other classes, imports,... And the return value of the method must be returned somehow... If other objects are modified in the runLongjob method those changes have to be send back to the local PC as well.
Is there a solution for this?
What you want to do basically not only requires your objects to be serialized, transmitted and deserialized on the remote system, but also that a Java VM is present and started on the host and has the classes for your transmitted instances in its class path. I doubt you will find an out-of-the-box solution for that. You could use something like RMI to communicate with a running JVM on the remote host, though.
I don't know of any frameworks to do specifically what you describe. The closest thing that comes to mind is to use web services. They allow you to define service running on a remote server which answers requests. The requests and responses can contain complex data structures. There are mature libraries for developing web services in java.
See these:
Introduction to Web Services
Building Web Services with JAX-WS

How to access a directory on a different server than I am working on through java?

I have two servers, one that runs my program written in Java (Server A) and one that stores a graph (Server B) that must be continuously accessed by Server A. To access Server B you must ssh with a username and password using Server B's IP address.
As of now I have failed to find a method to continuously access a directory on a different server and I am wondering if anyone knows a method that lets me do this (or if it is not possible, if there is a workaround).
I have looked into SSH libraries, but they all seem to only give you access to the directory for a brief amount of time. I need continuos access because I write and read from the graph on Server B all the time.
I basically want to make a proxy directory on Server A that actually refers/links to the directory on Server B:
graphDb = new EmbeddedGraphDatabase("/192.168.1.**/media/graphDB");
Any help would be great.
Probably unrelated option:
If client and server are Linux machines, you can use rsync to synchronize files between them. In that way you have a copy of the files on server A. The rsync command could be executed from the Java program or periodically from a cronjob on server A.
You could write your own client/server service, so that the server service provide you with the means to send data over the network to. It tends to be a lot of work though.
You could write your self a "heart beat" service on the client that tests the SSH connection and reestablishes it if it closes
You could "test" the ssh connection before you writing/reading from the connection
You could do as AlperAkture suggests (and mount the directory as a remote drive)

Connect to Unix File System using Java Program

I want to establish a connection with my UNIX file system using java program.. So that I can make some File I/O operations and normally I can connect using Putty.
How can I do the same using java program
I have the Host name, username,password and Port number
Help appreciated :)
You need several things:
A server that takes commands (create directory, list directory, write data to a file, read data from a file) over the network. This server should listen to port1 on localhost
You need to configure putty to forward port2 on your local computer to port1 on the server.
A local client which allows you to connect to port2 on your local computer. Putty will tunnel any data send to port2 to port1 on the remote server and vice versa.
Or you get WinSCP which uses the SSH protocol (just like Putty) and maybe already does what you want.
There's a pure Java implementation of SSH/SCP available: http://www.cleondris.ch/opensource/ssh2/
You can use its SCPClient or SFTPv3Client classes to work on the remote file system.
Documentation is available at http://www.cleondris.ch/opensource/ssh2/javadoc.
If you want to do it from Java, you can use Apache Commons VFS. It provides a common approach to dealing with files on all of the supported file systems. SFTP is one of the supported types which is most likely what you would need if you have been connecting with PuTTY.
You need SSH client. There are various pure java SSH clients. Google "java ssh client" and try any one of them. I used Jsch http://www.jcraft.com/jsch/ and it worked fine for me.

Categories