7

I'm looking for java code to copy files to a remote linux system. I have tried Runtime.getRuntime().exec() function by passing an scp command, but each time I run the program it is asking for the remote system password. I'd like to avoid that.

I looked at the Jsch library -- using this I can login to a remote system -- but I can't copy the files to the remote system. Once I login I can do scp to my host but again it requires the host system username and password. However, I only have the remote system's information.

  • 3
    You could just use public key authentication for your SSH connections. See for example stackoverflow.com/questions/7260/… . – user48669 Jun 9 '14 at 6:52
  • No,I can't do that.Bcos I would like to automate it.And I need some way to copy the public key to remote system without user intervention,I mean through java code.Asking the password for very first time is fine.But it should not ask every time I run the code. – Thushi Jun 9 '14 at 10:47
  • Yeah Thank you.I implemented this using Jsch library with sftp channel.And for the very first time I'll be having the credentials of the remote system so I don't have any issues in login.And then as suggested by @SamiLaine I copied my public key and my actual files(the files which I actually wanted to copy) to remote system.So for the next time while copying it didn't asked for any password. :) – Thushi Jun 9 '14 at 12:31
  • i need to transfer .csv files from linux machine to windows machine.i used jsch i am able to read the data in my console.i want the complete file to be in my local machine. is there an API or how would i do it? – user81547 Aug 21 '14 at 14:28
3

Copying a file from one host to another requires a daemon on the remote host, implementing some application-level file transmission protocol. This is a requirement no matter from which language you are going to talk to that remote daemon.

Your options for Linux systems are:

  • SSH. This requires a SSH daemon (say openssh-server) on the remote side. Because ssh is designed for security you will have to configure the remote host to authenticate you with either a password or a private key. Actually copying the file can be done via the scp utility or ssh client library (jsch would be an example of such).
  • NFS. The remote host installs a daemon (for example samba) and shares some files. Your local computer (cifs-utils package is capable of that) can then mount a remote location on the local file system. This way you can copy a file to the remote host by just copying the file locally. Authentication is optional, files are sent in plain over the network.
  • FTP. An ftp server is installed on remote side and configured to permit access to certain locations for certain users. You can then use any ftp client or some ftp client library (commons-net library from the Apache project, for instance) to connect to the remote ftp server and copy the files. Authentication is optional, files are sent in plain over the network.

All of this seems like a lot of work, and in deed it is, because there is not a single widely-adopted and standardized protocol that would be implemented and configured out-of-the-box on most systems.

  • 1
    Yeah Thank you.I implemented this using Jsch library with sftp channel.And for the very first time I'll be having the credentials of the remote system so I don't have any issues in login.And then as suggested by @SamiLaine I copied my public key and my actual files(the files which I actually wanted to copy) to remote system.So for the next time while copying it didn't asked for any password. :) – Thushi Jun 9 '14 at 12:32
7

You can use this code snippet to copy files to another linux machine.

JSch jsch = new JSch();
Session session = null;
session = jsch.getSession("username","hostname",22);
session.setPassword("password");
session.setConfig("StrictHostKeyChecking", "no");
    session.connect();
ChannelSftp channel = null;
channel = (ChannelSftp)session.openChannel("sftp");
channel.connect();
    File localFile = new File("localfilepath");
    //If you want you can change the directory using the following line.
    channel.cd(RemoteDirectoryPath)
channel.put(new FileInputStream(localFile),localFile.getName());
    channel.disconnect();
session.disconnect();

with that I have added my public key to remote system,generated using ssh-keygen.So it won't ask for password every time you run the program.

  • 1
    Amazingly simple, thanks and to add, if you want to copy the other way(remote to local), you use the channel.get(...) version. – kosgeinsky Jun 17 '19 at 12:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.