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.
Related
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.
I'm working on Android app.
how can i create a new directory in a remote host in java?
I tried this but it doesn't work:
String newFolder = "http://www.mysite.it/public/newfolder";
File outFile = new File(newFolder);
if(!outFile.exists()){
boolean b = outFile.mkdirs();
}
Thank you so much!
It can not be done this way. You need to send a command to your server and create the directory from your server.
Then you can send an acknowledgement back to your client
First of all: The folder is remote.
When using File you are accessing the local file system.
To connection to a remote host, you will need a URLConnection, HTTPUrlConnection or sockets.
So the file constructed using your string won't represent a valid file.
Creating folders on a remote host require the use of protocols such as ssh or ftp.
As an example:
URL url = new URL("ftp://mirror.csclub.uwaterloo.ca/index.html");
URLConnection urlConnection = url.openConnection();
would open a ftp - connection to host mirror.csclub.uwaterloo.ca requesting a file named index.html.
command - line ftp clients can send commands for creating folders to the remote host, e.g.
ftp anonoumos#somehost.com
# mkdir folderName
so you would have to send this command to your ftp host. For ssh it's basically the same procedure.
Both requires a basic client, though.
There are existing solutions around in the Java World, but I don't know of any for Android.
So while uploading a file to a host via ftp is pretty straight forward, (take a look at this example http://www.codejava.net/java-se/networking/ftp/upload-files-to-ftp-server-using-urlconnection-class), sending commands via ftp seems to be a lot more complicated task.
org.apache.commons.net.ftp.FTPClient e.g. supports sending commands via it's doCommand - method, but I don't know if you get it to work on android.
The question us also related to linux but solution is needed for Java. So I have a data directory
/somedir/data
on linux server
servername
I can ssh to the server and do anything I want only from deployment machine (due public/private keys in place). But there's a Java process that should read files from that directory. How can I force it read that files? I was trying to use File("//servername/somedir/data") with no success. Any help would be appreciated.
You must share the file using one of the network file services.
For example:
NFS (check with showmount -e);
Samba (check with smbclient -L);
AFS;
HTTP/FTP (check first if there a HTTP/FTP-server on the host).
You can also access this file using SSH (you say that you have SSH connection to the host, that means that SSH is accessible anyway).
If you want to connect to the SSH server from Java program,
you can use (for example) JSch for that.
Example of JSch usage is here.
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.
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).