1

I have my Java Program which is running on the windows machine. From this Window machine i need to read a file from the Linux server. I have write this code to authenticate with the Linux Server, and its working fine

session = jsch.getSession("root", "ServerIP", 22);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword("Passwrod");
            session.connect();
            System.out.println("Server is connect");

I can see "Server is connect" is printing on my machine that's mean authentication is done with the server. And now there is need to read the file from this server and i have write this code

try
            {
            File file = new File("/var/log//callrec/core1.log");
            LineNumberReader sc = new LineNumberReader(new FileReader(file));
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

But its throwing file not found exception. Can any body guide me how can i solve this.

3
  • What is jsch? And, you still are accessing a local file in your code - just creating a session through some mechanism does not mean that the File class automatically accesses files on the remote machine Commented Jan 15, 2014 at 8:58
  • Its a library in java which is used for the authentication.. Commented Jan 15, 2014 at 9:06
  • You should add a link to the library you are using - my answer below assumes that it is http://www.jcraft.com/jsch/, but this is only a rough guess ... Commented Jan 15, 2014 at 9:08

2 Answers 2

1

According to http://www.jcraft.com/jsch/examples/ScpFrom.java.html, you need to execute a command on the remote side through your session object, then get the input and output streams to communicate with the remote command, and read the file meta data and contents through these channels:

...
String command = "scp -f /var/log/callrec/core1.log";
Channel channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);

OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();

channel.connect();

// Now use in and out to read the file, 
// see http://www.jcraft.com/jsch/examples/ScpFrom.java.html for a complete example
...
Sign up to request clarification or add additional context in comments.

1 Comment

Andreas thank so you much for you reply....I have tried it out your way..I have write this code BufferedReader reader = new BufferedReader(new InputStreamReader(in)); if (in!=null) { String str; while ((str = reader.readLine()) != null) { System.out.print(str); } } else { System.out.print("No line"); } But my program hanged after printing server is connect Can you please help me out
0

Once you're logged in via SSH, your Java code isn't magically transported in the Linux server context. So when you're using Java tools to read files, it searches the file on your local machine.

You have then at least two possibilities :

1/ Copying the file from the Linux server then work on it. You can better use SFTP rather than SSH if it's the only command you send (JSCH example here)

2/ Connecting to the Linux server like you do, then streaming the file back to your machine by lauching a cat myFile command and reading the outputStream of the SSH session

My vote would be for the first method, which is cleaner.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.