Java code to copy files from one linux machine to another linux machine

49,376

Solution 1

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.

Solution 2

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.

Share:
49,376

Related videos on Youtube

Thushi
Author by

Thushi

Updated on September 18, 2022

Comments

  • Thushi
    Thushi almost 2 years

    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.

    • Admin
      Admin about 10 years
      You could just use public key authentication for your SSH connections. See for example stackoverflow.com/questions/7260/… .
    • Thushi
      Thushi about 10 years
      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
      Thushi about 10 years
      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. :)
    • Admin
      Admin almost 10 years
      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?
  • kosgeinsky
    kosgeinsky about 5 years
    Amazingly simple, thanks and to add, if you want to copy the other way(remote to local), you use the channel.get(...) version.