0

Hi I need to execute vi command from java and need to store into a local file . I am using jcraft.jsch

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.OutputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class ViDAO {

    public boolean mergeLogs(String hostName, String logFile, String userName,
            String password) {
        System.out.println("in VIdao" + hostName);
        String command = null;
        final int MAXREAD = 131072 * 100;

        try {

            command = "cd /dr/logs/sonic/dmbain1;view " + logFile;
            JSch jsch = new JSch();
            Session session = jsch.getSession(userName, hostName, 22);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.setPassword("Janu$113");

            session.connect();
            /* System.out.println("Connected to******* " + host+"*********");*/
            Channel channel = session.openChannel("exec");
            ((ChannelExec) channel).setCommand(command);

            channel.setXForwarding(true);
            channel.connect();


            InputStream in = channel.getInputStream();

            byte[] tmp = new byte[MAXREAD];
            File dir = new File("C:\\Documents and Settings\\" + 
                        System.getProperty("user.name") + 
                        "\\Desktop\\LogFiles");
            dir.mkdir();

            File f;
              f=new File("C:\\Documents and Settings\\" + 
                           System.getProperty("user.name") +
                           "\\Desktop\\LogFiles\\" +
                           logFile + ".txt");

              if(!f.exists()){

              f.createNewFile();
              }

              BufferedWriter out = new BufferedWriter(new FileWriter(f));



            while (true) {
                while (in.available() > 0) {

                    int i = in.read(tmp, 0, MAXREAD);

                    if (i < 0)
                        break;
                    String strResult = new String(tmp, 0, i);

                    out.write(strResult+"\n");
                    System.out.println(strResult);

                }
                if (channel.isClosed()) {
                    in.close();

                    break;
                }

            }

            System.out.println("completed");

            channel.disconnect();
            session.disconnect();
            out.close();


        } catch (Exception e) {
            e.printStackTrace();
        }

return true;
    }

    public static void main(String[] args) {

    }
}

Here i am unable to read the file only some lines of code only i am able to read please help on this. when i use tail command instead of vi its working but processing long time.If i use vi command only some lines iam able to print.

please help on this...

0

1 Answer 1

1

view is usually aliased to vi which requires an actual terminal or terminal emulator console to work, since it uses the terminal in raw mode.

When confronted with a non-terminal output, vi will print a warning and then start spewing the first "screenfull" of file contents intermixed with control characters - for most automated processing uses that output is almost useless.

If you want to read in the contents of a remote file, you should probably use cat instead of vi.

Sign up to request clarification or add additional context in comments.

4 Comments

Do you have any sample code to read using cat command? I need to read log files. Here in the above program I am reading into String is there any best way to read the file into local text file? Kindly provide suggestions.
@shivatatikonda: just replace view with cat in your program.
I need to read .gz file in the above program Can you please tell me how to read gz file into textfile?
I am also using Jsch ib. When I enter vi command, as you mentioned, I see some garbage characters in all. I can not use any of the vi command henceforth though. How can I open vi editor using SshChannel and write new file?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.