Arthas的基础命令

系列


开篇

arthas本身提供一些基础的linux命令,熟悉linux命令行的应该都比较熟悉,因为这趴内容也不是特别难,所以就一次性多讲解几个命令。

  • cat——打印文件内容,和linux里的cat命令类似
  • base64——base64编码转换,和linux里的base64命令类似
  • pwd——返回当前的工作目录,和linux命令类似


命令解析

public class CatCommand extends AnnotatedCommand {

    @Override
    public void process(CommandProcess process) {

        for (String file : files) {
            File f = new File(file);
            try {
                String fileToString = FileUtils.readFileToString(f,
                        encoding == null ? Charset.defaultCharset() : Charset.forName(encoding));
                process.appendResult(new CatModel(file, fileToString));
            } catch (IOException e) {
                logger.error("cat read file error. name: " + file, e);
                process.end(1, "cat read file error: " + e.getMessage());
                return;
            }
        }

        process.end();
    }

    public static String readFileToString(File file, Charset encoding) throws IOException {
        FileInputStream stream = new FileInputStream(file);
        try {
            Reader reader = new BufferedReader(new InputStreamReader(stream, encoding));
            StringBuilder builder = new StringBuilder();
            char[] buffer = new char[8192];
            int read;
            while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
                builder.append(buffer, 0, read);
            }
            return builder.toString();
        } finally {
            stream.close();
        }
    }
}
  • grep命令实现的原理是通过BufferedReader进行读取返回。


public class Base64Command extends AnnotatedCommand {

    @Override
    public void process(CommandProcess process) {
       
        InputStream input = null;

        try {
            input = new FileInputStream(f);
            byte[] bytes = IOUtils.getBytes(input);

            ByteBuf convertResult = null;
            // 使用Netty的Unpooled转换成ByteBuf对象
            // 使用Netty的Base64进行加密和解密
            if (this.decode) {
                convertResult = Base64.decode(Unpooled.wrappedBuffer(bytes));
            } else {
                convertResult = Base64.encode(Unpooled.wrappedBuffer(bytes));
            }

            if (this.output != null) {
                // 通过Netty的ByteBuf对象进行操作并进行输出
                int readableBytes = convertResult.readableBytes();
                OutputStream out = new FileOutputStream(this.output);
                convertResult.readBytes(out, readableBytes);
                process.appendResult(new Base64Model(null));
            } else {
                String base64Str = convertResult.toString(CharsetUtil.UTF_8);
                process.appendResult(new Base64Model(base64Str));
            }
        } catch (IOException e) {

        } finally {
            IOUtils.close(input);
        }

        process.end();
    }

    public static byte[] getBytes(InputStream input) throws IOException {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        copy(input, result);
        result.close();
        return result.toByteArray();
    }
}
  • Base64Command的实现是结了java的文件操作和Netty提供的工具包结合实现的。
  • 通过Netty的Unpooled返回ByteBuf对象,通过Netty提供的Base64包进行加密和解密。


public class PwdCommand extends AnnotatedCommand {
    @Override
    public void process(CommandProcess process) {
        String path = new File("").getAbsolutePath();
        process.appendResult(new PwdModel(path));
        process.end();
    }
}
  • pwd命令直接通过在当前目录创建文件并返回绝对路径进行返回。


唠嗑

  • 坚持写博客只是一种热爱,也希望能通过一种方式能够帮到别人。
  • 喜欢探究原理背后,但是探究本质性的东西讲究深度,我只是努力将我的深度拓展。
  • 如果只肯相信你认知范围的事情,那么抱歉咱们就没法交流了,因为你无法享受新内容扑面而来的快乐。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容