【问题标题】:Parse BIND DNS configuration file by Java?用Java解析BIND DNS配置文件?
【发布时间】:2014-03-30 05:50:09
【问题描述】:

我正在尝试逐行解析 BIND 配置,但它不起作用。因为BIND使用了

的结构

keyword1 key_name {

keyword2 key_value ;

};

存储配置。

如何通过Java解析ISC BIND DNS配置文件?

例子:

logging {
  channel "default" {
     file "C:\named\etc\default.log" versions 5 size 5m;
     severity dynamic;
     print-time yes;
     print-severity yes;
     print-category yes;
  };

  channel "general" {
     severity dynamic;
     file "C:\named\etc\general.log" versions 5 size 5m;
     print-time yes;
     print-severity yes;
     print-category yes;
  };

 category "default" {
     "default";
 };
 category "general" {
     "general";
 };

};

这是我的代码:

        import java.io.BufferedReader;
        import java.io.FileReader;
        import java.io.IOException;
        import java.util.StringTokenizer;
        import java.util.regex.Matcher;
        import java.util.regex.Pattern;

        public class ScannerTest {

            public static void main(String[] args) throws IOException {

                FileReader file = new FileReader("C:/named.conf");

                BufferedReader br = new BufferedReader(file);
                String line;
                boolean isChannel = false;
                while ((line = br.readLine()) != null) {

                    if (line.length() <= 0)
                        continue; // skip empty line
                    if (line.charAt(0) == '#' | line.charAt(0) == '/')
                        continue; // skip comment line

                    if (isChannel) {

                        String endChannel = "};"; // end of block
                        String regexEndChannel = endChannel;
                        Pattern patternEndChannel = Pattern.compile(regexEndChannel);
                        Matcher matcherEndChannel = patternEndChannel.matcher(line);

                        if (matcherEndChannel.find() == false) {

                            // get the file name
                            String fileKey = "file";
                            String regex = "\\b" + fileKey + "\\b";
                            Pattern pattern = Pattern.compile(regex);
                            Matcher matcher = pattern.matcher(line);

                            while (matcher.find() == true) {

                                String fileName = null;
                                StringTokenizer channelToken = new StringTokenizer(line);
                                if (channelToken.hasMoreTokens())
                                    fileKey = channelToken.nextToken();
                                if (channelToken.hasMoreTokens())
                                    fileName = channelToken.nextToken();
                                // get the file name
                                System.out.println("file Name: " + fileName);

                            }

                            // get the severity name
                            String severityKey = "severity";
                            // don't know why boundary the word "severity" with the \b
                            // but it still match the word print-severity
                            String regexseverity = "\\b" + severityKey + "\\b";
                            Pattern patternseverity = Pattern.compile(regexseverity);
                            Matcher matcherseverity = patternseverity.matcher(line);

                            while (matcherseverity.find() == true) {

                                String severityName = null;
                                StringTokenizer channelToken = new StringTokenizer(line);
                                if (channelToken.hasMoreTokens())
                                    severityKey = channelToken.nextToken();
                                if (channelToken.hasMoreTokens())
                                    severityName = channelToken.nextToken();
                                // get the file name
                                System.out.println("severity Name: " + severityName);

                            }

                            // get the last things
                            // print-time no;
                            // print-severity yes;
                            // print-category yes;

                        } else {

                            isChannel = false;
                        }
                    }

                    String channelKey = "channel";
                    String regex = "\\b" + channelKey + "\\b";
                    Pattern pattern = Pattern.compile(regex);
                    Matcher matcher = pattern.matcher(line);

                    while (matcher.find() == true) {

                        isChannel = true;
                        String channelName = null;
                        StringTokenizer channelToken = new StringTokenizer(line);
                        if (channelToken.hasMoreTokens())
                            channelKey = channelToken.nextToken();
                        if (channelToken.hasMoreTokens())
                            channelName = channelToken.nextToken();
                        // get the channel name
                        System.out.println("Channel Name: " + channelName);
                    }

                }

                br.close();

            }

        }

【问题讨论】:

  • 我在这里看不到 Java 代码。
  • @EJP 谢谢,我添加了代码。
  • 很好,问题是什么?

标签: java regex dns named


【解决方案1】:

尝试dnsjava 区域文件解析器并查看org.xbill.DNS.Master 类,该类应该解析您需要的内容

【讨论】:

  • 重点是ISC风格的配置文件,不是DNS区域数据文件。
猜你喜欢
  • 2010-10-12
  • 1970-01-01
  • 2012-09-16
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 2021-08-14
  • 2016-03-12
  • 2012-08-31
相关资源
最近更新 更多