Reading and Writing Linux Files Using JSCH (Java)


JSCH (Java Secure Channels)

JSCH allows us to connect to sshd server and use file transafer, port forwarding, X11 forwarding and we can use its functionality in our java applications. I have created an example which shows us how to use JSCH in java application to read and write linux files. JSch is in pure Java, but it depends on JavaTM Cryptography Extension (JCE)

Below is class which consist two methods: 1st one is used to read files from linux and 2nd one is used to write files to linux filesystem using JSCH. We need to download JSCH library from JCRAFT and add JSCH library to our java application.

import com.jcraft.jsch.*;
import java.io.*;
import java.util.*;

public class JSCHcl
{
  public JSCHcl()
  {}

  public String ReadFileFromLinux(String str_Host, String str_Username, String str_Password, int int_Port, String str_FileDirectory, String str_FileName)
  {
    JSch obj_JSch = new JSch();
    Session obj_Session = null;
    StringBuilder obj_StringBuilder = new StringBuilder();
    try
    {
      obj_Session = obj_JSch.getSession(str_Username, str_Host);
      obj_Session.setPort(int_Port);
      obj_Session.setPassword(str_Password);
      Properties obj_Properties = new Properties();
      obj_Properties.put("StrictHostKeyChecking", "no");
      obj_Session.setConfig(obj_Properties);
      obj_Session.connect();
      Channel obj_Channel = obj_Session.openChannel("sftp");
      obj_Channel.connect();
      ChannelSftp obj_SFTPChannel = (ChannelSftp) obj_Channel;
      obj_SFTPChannel.cd(str_FileDirectory);
      InputStream obj_InputStream = obj_SFTPChannel.get(str_FileName);
      char[] ch_Buffer = new char[0x10000];
      Reader obj_Reader = new InputStreamReader(obj_InputStream, "UTF-8");
      int int_Line = 0;
      do
      {
        int_Line = obj_Reader.read(ch_Buffer, 0, ch_Buffer.length);
        if (int_Line > 0)
        { obj_StringBuilder.append(ch_Buffer, 0, int_Line);}
      }
      while (int_Line >= 0);
      obj_Reader.close();
      obj_InputStream.close();
      obj_SFTPChannel.exit();
      obj_Channel.disconnect();
      obj_Session.disconnect();
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
    return obj_StringBuilder.toString();
  }

  public void WriteFileToLinux(String str_Host, String str_Username, String str_Password, int int_Port, String str_Content, String str_FileDirectory, String str_FileName)
  {
    JSch obj_JSch = new JSch();
    Session obj_Session = null;
    try
    {
      obj_Session = obj_JSch.getSession(str_Username, str_Host);
      obj_Session.setPort(int_Port);
      obj_Session.setPassword(str_Password);
      Properties obj_Properties = new Properties();
      obj_Properties.put("StrictHostKeyChecking", "no");
      obj_Session.setConfig(obj_Properties);
      obj_Session.connect();
      Channel obj_Channel = obj_Session.openChannel("sftp");
      obj_Channel.connect();
      ChannelSftp obj_SFTPChannel = (ChannelSftp) obj_Channel;
      obj_SFTPChannel.cd(str_FileDirectory);
      InputStream obj_InputStream = new ByteArrayInputStream(str_Content.getBytes());
      obj_SFTPChannel.put(obj_InputStream, str_FileDirectory + str_FileName );
      obj_SFTPChannel.exit();
      obj_InputStream.close();
      obj_Channel.disconnect();
      obj_Session.disconnect();
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }
  }
}

Using ReadFileFromLinux method in a JSP page or Java Form to read a file:

int int_SSHPort = 22; //By default SSH port of linux system is 22. Please check yours if it is different.
String str_Directory = "/var/";
String str_Host = "192.168.1.108";
String str_Username = "root";
String str_Password = "password";
String str_File = "file_to_read.txt";
JSCHcl obj_JSHcl = new JSCHcl();
String str_FileContent = "";

str_FileContent = obj_JSHcl.ReadFileFromLinux(str_Host, str_Username, str_Password, int_SSHPort, str_Directory, str_File);

obj_JSHcl = null;

Using WriteFileToLinux method to write a file in linux filesystem:

String str_Content = "Write this content to linux file";

  int int_SSHPort = 22; //By default SSH port of linux system is 22. Please check yours if it is different.
  String str_Directory = "/var/";
  String str_Host = "192.168.1.108";
  String str_Username = "root";
  String str_Password = "password";
  String str_File = "file_to_write.txt";
  JSCHcl obj_JSCHcl = new JSCHcl();

  obj_JSCHcl.WriteFileToLinux(str_Host, str_Username, str_Password, int_SSHPort, str_Content, str_Directory, str_File);
  obj_JSCHcl = null;

 Download: jsch-0.1.48.jar

Tags: , , , , , , , ,

Comments & Responses

2 Responses so far.

  1. Tony says:

    on the onvocation of the ReadFileFromLinux method there is a typo i.e. it should be passing str_Directory and instead passed str_Username twice

    str_FileContent = obj_JSHcl.ReadFileFromLinux(str_Host, str_Username, str_Password, int_SSHPort, str_Username, str_File);

Leave a Reply

Your email address will not be published. Required fields are marked *

*


2 × = 10

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>