ssh—是否有任何方法可以打开linux/unix系统上可用的文件,并使用java将所有异常复制到文本文件中

p1tboqfb  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(143)

关闭。这个问题需要更加突出重点。它目前不接受答案。
**想改进这个问题吗?**通过编辑这篇文章更新这个问题,使它只关注一个问题。

上个月关门了。
改进这个问题
我正在尝试创建一个程序,它将打开linux/unix系统中可用的日志文件,并将所有异常跟踪复制到另一个文件中。以下是我完成的命令/步骤。
登录linux/unix
打开日志文件目录
打开日志文件
以下步骤挂起
搜索异常并将跟踪复制到另一个文件中
示例代码:

public static void main(String[] args) {
        String host="hostname";
        String user="sshuser";
        String password="sshpwd";
        String command1="cd /usr/logs; view myLogs.log; ";
        try{

            java.util.Properties config = new java.util.Properties(); 
            config.put("StrictHostKeyChecking", "no");
            JSch jsch = new JSch();
            Session session=jsch.getSession(user, host, 22);
            session.setPassword(password);
            session.setConfig(config);
            session.connect();
            System.out.println("Connected");

            Channel channel=session.openChannel("exec");
            ((ChannelExec)channel).setCommand(command1);
            channel.setInputStream(null);
            ((ChannelExec)channel).setErrStream(System.err);

            InputStream in=channel.getInputStream();
            channel.connect();
            byte[] tmp=new byte[1024];
            while(true){
              while(in.available()>0){
                int i=in.read(tmp, 0, 1024);
                if(i<0)break;
                System.out.print(new String(tmp, 0, i));
              }
              if(channel.isClosed()){
                System.out.println("exit-status: "+channel.getExitStatus());
                break;
              }
              try{Thread.sleep(1000);}catch(Exception ee){}
            }
            channel.disconnect();
            session.disconnect();
            System.out.println("Execution DONE");
        }catch(Exception e){
            e.printStackTrace();
        }

}

谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题