如何在系统日志消息中欺骗主机名?

发布于 2024-12-15 15:28:21 字数 533 浏览 1 评论 0原文

我在 perl 中记录这样的消息 -

syslog ("LOG_INFO", "this is info");
syslog ("LOG_WARNING", "this is warning");

当我看到这些消息时,我得到这个 -

Nov 15 20:20:47 ubuntu tag-0.0.2[13399]: this is info
Nov 15 20:20:47 ubuntu tag-0.0.2[13399]: this is warning

syslog 消息中的单词“ubuntu”恰好是本地主机的主机名。

有没有办法可以在本地登录并指定主机名?

我的应用程序处理来自其他主机的数据并记录有关它们的信息。如果我可以在记录消息时指定主机名,那就太好了,这样我就可以轻松使用第三方工具,因为它们可以轻松地根据主机名过滤掉日志。

顺便说一句,如果我可以添加其他问题 - 为什么日志不显示消息级别?我不应该期望在我正在记录的信息系统日志消息中看到“信息”吗?

I am logging messages like this in perl -

syslog ("LOG_INFO", "this is info");
syslog ("LOG_WARNING", "this is warning");

when I see these messages, I get this-

Nov 15 20:20:47 ubuntu tag-0.0.2[13399]: this is info
Nov 15 20:20:47 ubuntu tag-0.0.2[13399]: this is warning

The word "ubuntu" in syslog message happens to be host name of the local host.

Is there a way I can log locally and but specify a hostname?

My app processes data from other hosts and logs information about them. It will be great if I can specify the host name when I log messages, this way I can use third party tools easily as they can easily filter out logs based upon hostname.

btw, if I can add additional question- why are the logs not showing level of message? shouldn't I expect to see "info" in info syslog message I am logging ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卷耳 2024-12-22 15:28:21

完成此任务的最简单方法可能是将 syslog 设置为通过网络接收消息。对于rsyslog,这通常位于/etc/rsyslog.conf中:

# provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

我在这里使用UDP,因为它很容易在任何syslog上执行code> 守护进程并演示它很容易:

$ echo "<0>Oct 11 22:14:15 mymachine testing" | nc -u localhost syslog
^C
$ tail -1 /var/log/syslog
Oct 11 22:14:15 mymachine testing

简而言之: 表示设施和优先级,如 RFC 第 4.1.1 节。时间戳在 4.1.2 中得到了高度指定:简短的三个字母的英文月份缩写,没有前导 0 ——而是一个前导空格:Aug__8 而不是 Aug_8(使用下划线是因为代码块中的空格会折叠)。主机名不能包含任何域部分。 IP 地址很好,IPv4 和 IPv6 都可以。

您还可以使用 Unix 域套接字 (unix(7)),例如 /dev/log。这比 UDP 更可靠。

Probably the easiest way to accomplish this task is to set the syslog to receive messages over the network. For rsyslog, this is often in /etc/rsyslog.conf:

# provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514

I'm using UDP here because it is easy to do on just about any syslog daemon and demonstrating it is easy:

$ echo "<0>Oct 11 22:14:15 mymachine testing" | nc -u localhost syslog
^C
$ tail -1 /var/log/syslog
Oct 11 22:14:15 mymachine testing

In short: the <nnn> represents the facility and priority, as described in section 4.1.1 of the RFC. The timestamp is highly specified in 4.1.2: in short, three-letter English month abbreviations, no leading 0 -- instead a leading space: Aug__8 rather than Aug_8 (underscores used because spaces collapse in code blocks). The hostname can't have any domain portions. IP addresses are fine, both IPv4 and IPv6.

You could also use Unix domain sockets (unix(7)) such as /dev/log. That would be more reliable than UDP.

困倦 2024-12-22 15:28:21

不幸的是,我认为如果您在本地登录,则无法指定主机名。

另外,您不应该在日志前面看到“信息”。日志级别会影响消息的去向(您可以在 /etc/syslog.conf 中对此进行自定义)。默认情况下,LOG_INFO 和 LOG_WARNING 转到 /var/log/messages.log,LOG_EMERG 和 LOG_ERR 转到 /var/log/errors.log。该级别不会出现在输出中。

Unfortunately, I don't believe it is possible to specify a hostname if you are logging locally.

Also, you are not supposed to be seeing 'info' in front of your logs. The log level affects where the messages go (you can customize this in /etc/syslog.conf). By default, LOG_INFO and LOG_WARNING go to /var/log/messages.log and LOG_EMERG and LOG_ERR go to /var/log/errors.log. The level does not appear in the output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文