user5102232
user5102232

Reputation:

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");

Upvotes: 0

Views: 928

Answers (2)

Kenster
Kenster

Reputation: 25458

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.

Upvotes: 0

Shubham Chaurasia
Shubham Chaurasia

Reputation: 2632

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.

Upvotes: 1

Related Questions