Linux 如何在不运行 dhcpd 的情况下检查 dhcpd.conf 是否存在语法错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13878706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:03:08  来源:igfitidea点击:

How can I check dhcpd.conf against syntax error without running dhcpd?

linuxbashdhcp

提问by ibrahim

I need to make sure there are no syntax errors on dhcpd.conf. If there are errors, I want to get what they are.

我需要确保 dhcpd.conf 上没有语法错误。如果有错误,我想知道它们是什么。

I can check for syntax errors with this command:

我可以使用以下命令检查语法错误:

dhcpd -cf /path/to/dhcpd.conf

but that prints a lot of information in addition to the error I got. Another thing is that I don't want to run dhcpd, even there is no syntax error. I only want to check for syntax errors and see what they are.

但是除了我得到的错误之外,它还打印了很多信息。另一件事是我不想运行dhcpd,即使没有语法错误。我只想检查语法错误,看看它们是什么。



Unfortunately, running dhcpd -tf /path/to/dhcpd.confalso didn't solve my problem.

不幸的是,跑步dhcpd -tf /path/to/dhcpd.conf也没有解决我的问题。

采纳答案by John

The syntax you are looking for is

您正在寻找的语法是

dhcpd -t -cf /path/to/dhcpd.conf

The -toption will do a config check:

-t选项将进行配置检查:

If the -t flag is specified, the server will simply test the configuration file for correct syntax, but will not attempt to perform any network operations. This can be used to test the new configuration file automatically before installing it.

如果指定了 -t 标志,服务器将简单地测试配置文件的语法是否正确,但不会尝试执行任何网络操作。这可用于在安装前自动测试新配置文件。

You do not need to use -cfif you are using the default config file path.

-cf如果您使用的是默认配置文件路径,则不需要使用。

/usr/sbin/dhcpd -t

The one you tried with -tf /path/to/...is quite different and relates to tracing.

您尝试使用的那个-tf /path/to/...完全不同,并且与跟踪有关。

回答by Michael Firth

One thing not on the manual page, and not covered here yet, is that the '/usr/sbin/dhcpd -t' command uses the return value to indicate whether the configuration is correct or not.

手册页上没有的一件事,这里也没有涉及,'/usr/sbin/dhcpd -t' 命令使用返回值来指示配置是否正确。

If there are no errors, it will return zero. if there are syntax errors it will return non zero (1 for the test I did)

如果没有错误,它将返回零。如果存在语法错误,它将返回非零(我所做的测试为 1)

So you can use something like:

所以你可以使用类似的东西:

/usr/sbin/dhcpd -t
if [ $? -ne 0 ]; then
  echo "Configuration has errors, aborting"
fi
/bin/systemctl restart isc-dhcp-server

To check if changes made to the configuration are valid before trying to restart the server with the new version.

在尝试使用新版本重新启动服务器之前检查对配置所做的更改是否有效。

Unfortunately I don't think there is any option to just display the errors. It would be possible to use a text parsing tool (awk, python etc) to remove the header lines (for the version I have, everything up to a line beginning with "For info"), and trailer lines (for the version I have, everything after a line saying "Configuration file errors encountered -- exiting") which would leave just the syntax error and location

不幸的是,我认为没有任何选项可以只显示错误。可以使用文本解析工具(awk、python 等)删除标题行(对于我拥有的版本,所有内容都以“For info”开头)和尾行(对于我拥有的版本) , 一行说“遇到配置文件错误——退出”之后的所有内容,这将只留下语法错误和位置