在命令上运行 shell 脚本

Lin*_*eur 6 scripting linux shell command

我想在date -s <string>使用命令时运行 shell 脚本。例如,我想通过在 shell 脚本中执行以下命令将命令记录到文件 /tmp/user.log

logger -p user.notice "date -s command executed" -f /tmp/user.log
Run Code Online (Sandbox Code Playgroud)

在shelldate -s <string>上执行时如何运行shell脚本?

为了使它更通用,当其他人在我的系统上发出特定的 linux 命令时,我想运行我的 shell 脚本。这该怎么做?

lar*_*sks 7

重新表述这个问题,您想知道何时有人尝试设置系统时间。这正是audit子系统的用途……它允许您审计特定系统调用的执行。在这种情况下,您想知道何时有人调用可以更改系统时间的各种系统调用中的任何一个。通过使用audit子系统,无论是有人调用/bin/date还是他们自己在本地构建的命令版本,您都有一个有效的解决方案。

有关规则语法的完整说明,以及审核时间更改操作的示例,请参阅auditd(8)audit.rules(7),您可以在示例nispom.rules文件中查找“时间更改” 。您可以在本地系统上找到它,也可以在这里找到它:

有关audit子系统的更多信息(文档有点难获得):


Lin*_*eur 1

非常感谢serverfault朋友们的解答。

我想在你们的帮助下我已经找到了问题的答案。但是如果有任何与之相关的错误或需要改进的话,请大家帮帮我?我来啦。

这些是我所做的事情。

我)。datewrapper.sh使用以下代码在 /etc 中创建了一个脚本

#! /bin/bash

# wrapper script for the date command.
# whenever the date -s command is issued, it will be logged.

# executing the date command first with parameter list if any
date $@

# check whether the date command executed successfully
if [ "$?" == "0" ] ; then
   for param in $@ ; do
      # if "-s" option is used, log it
      if [ "$param" == "-s" ] ; then
         # user.notice logs the message to /tmp/log/user.log
         # as per the commands in /etc/syslog-ng/syslog-ng.conf
         logger -p user.notice "New date set"
         break
      fi
   done
fi

exit 0
Run Code Online (Sandbox Code Playgroud)

ii). chmod a+x /etc/datewrapper.sh ; 别名 date='/etc/datewrapper.sh'

三)。日期

世界标准时间 2010 年 12 月 21 日星期二 21:51:01

四)。日期-s“21:53:05”

世界标准时间 2010 年 12 月 21 日星期二 21:53:05

五)。我检查了/tmp/log/user.log。它显示消息

12 月 21 日 21:53:05 localhost root:新日期设置

所以结果是,每当用户发出date命令时,我的脚本都会被执行,每当他发出带有选项的命令时-s,它都会登录到/tmp/log/user.log