使用java执行unix命令

使用java执行unix命令,java,unix,Java,Unix,嗨,我需要从java执行vi命令,并需要存储到本地文件中。我正在使用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;

嗨,我需要从java执行vi命令,并需要存储到本地文件中。我正在使用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) {

    }
}
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileWriter;
导入java.io.InputStream;
导入java.io.OutputStream;
导入com.jcraft.jsch.Channel;
导入com.jcraft.jsch.ChannelExec;
导入com.jcraft.jsch.jsch;
导入com.jcraft.jsch.Session;
公营维达{
公共布尔合并日志(字符串主机名、字符串日志文件、字符串用户名、,
字符串(密码){
System.out.println(“in-VIdao”+主机名);
字符串命令=null;
最终整数MAXREAD=131072*100;
试一试{
command=“cd/dr/logs/sonic/dmbain1;view”+日志文件;
JSch JSch=新的JSch();
Session Session=jsch.getSession(用户名、主机名、22);
java.util.Properties config=new java.util.Properties();
配置放置(“检查”、“否”);
session.setConfig(config);
session.setPassword(“Janu$113”);
session.connect();
/*System.out.println(“连接到*******”+主机+“**********”)*/
Channel=session.openChannel(“exec”);
((ChannelExec)channel).setCommand(command);
通道设置向前(真);
channel.connect();
InputStream in=channel.getInputStream();
字节[]tmp=新字节[MAXREAD];
文件目录=新文件(“C:\\Documents and Settings\\”+
System.getProperty(“user.name”)+
“\\Desktop\\LogFiles”);
dir.mkdir();
文件f;
f=新文件(“C:\\Documents and Settings\\”+
System.getProperty(“user.name”)+
“\\桌面\\日志文件\\”+
日志文件+“.txt”);
如果(!f.exists()){
f、 createNewFile();
}
BufferedWriter out=新的BufferedWriter(新的文件写入程序(f));
while(true){
while(in.available()>0){
int i=in.read(tmp,0,MAXREAD);
if(i<0)
打破
字符串strResult=新字符串(tmp,0,i);
out.write(strResult+“\n”);
System.out.println(strResult);
}
if(channel.isClosed()){
in.close();
打破
}
}
系统输出打印项次(“完成”);
通道断开();
session.disconnect();
out.close();
}捕获(例外e){
e、 printStackTrace();
}
返回true;
}
公共静态void main(字符串[]args){
}
}
在这里,我无法阅读文件,只有一些代码行,只有我能阅读,请帮助这一点。 当我使用tail命令而不是vi命令时,它会工作,但处理时间很长。如果我使用vi命令,我只能打印一些行


请对此提供帮助…

视图
通常别名为
vi
,这需要实际的终端或终端模拟器控制台才能工作,因为它在原始模式下使用终端

当遇到非终端输出时,
vi
将打印一条警告,然后开始喷出第一个“满屏”的文件内容,其中夹杂着控制字符-对于大多数自动化处理使用,该输出几乎是无用的


如果要读取远程文件的内容,可能应该使用而不是
vi

使用cat命令读取示例代码吗?我需要读取日志文件。在上面的程序中,我正在读入字符串,有没有最好的方法将文件读入本地文本文件?请提供建议。@shivatatikonda:只需将程序中的
view
替换为
cat
。我需要在上面的程序中读取.gz文件。您能告诉我如何将gz文件读入文本文件吗?我也在使用Jsch ib。正如您所提到的,当我输入vi命令时,我看到一些垃圾字符。不过,从今以后我不能使用任何vi命令。如何使用SshChannel打开vi编辑器并编写新文件?