求详解释一个shell脚本

xinghun2800000 2011-02-28 03:28:59
# /bin/sh
# hist_record.sh
# This script is often to write in /etc/profile.
# This script could record history input and send mail to root.

RMIP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
TIME=`date +%Y%m%d.%H:%M.%S`

if [ "$RMIP" = "" ]
then
RMIP=`hostname`
fi

LOGS="/tmp/.hist/$TIME.$USER.$RMIP.hist"
HOST=`/bin/hostname`

if [ ! -d /tmp/.hist ]
then
mkdir /tmp/.hist
chmod 777 /tmp/.hist
fi

script -q $LOGS && mail -s "$TIME, $USER FROM $RMIP LOGIN $HOST" root < $LOGS
...全文
154 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
xinghun2800000 2011-03-01
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 masmaster 的回复:]
引用 14 楼 xinghun2800000 的回复:
引用 13 楼 justkk 的回复:
直接执行history 命令,输出就是带时间的


history 输出只有序号,没有时间

如果你用的是bash的话, 可以尝试一下,在命令行里输入下面语句以后再输入history,看看有什么!
export HISTTIMEFORMAT="%F %T"
[/Quote]

我用的是bash,这个方法也听过的,可是不知怎么回事还是不显示时间的,
masmaster 2011-02-28
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 xinghun2800000 的回复:]
引用 13 楼 justkk 的回复:
直接执行history 命令,输出就是带时间的


history 输出只有序号,没有时间
[/Quote]
如果你用的是bash的话, 可以尝试一下,在命令行里输入下面语句以后再输入history,看看有什么!
export HISTTIMEFORMAT="%F %T"
xinghun2800000 2011-02-28
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 justkk 的回复:]
直接执行history 命令,输出就是带时间的
[/Quote]

history 输出只有序号,没有时间
justkk 2011-02-28
  • 打赏
  • 举报
回复
直接执行history 命令,输出就是带时间的
xinghun2800000 2011-02-28
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 justkk 的回复:]
看script的联机帮助
Certain interactive commands, such as vi(1), create garbage in the typescript file.

除非你能修改它的源码
[/Quote]

我对脚本语言还是不太熟悉,请问可不可通过history将命令按时间分解出一条条的呢?
justkk 2011-02-28
  • 打赏
  • 举报
回复
看script的联机帮助
Certain interactive commands, such as vi(1), create garbage in the typescript file.

除非你能修改它的源码
xinghun2800000 2011-02-28
  • 打赏
  • 举报
回复
各位大哥,这是网上的一个记录键盘交互的一个脚本文件,我所不理解的是这两句,RMIP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`


script -q $LOGS && mail -s "$TIME, $USER FROM $RMIP LOGIN $HOST" root < $LOGS
这两句

我只大致明白是利用script命令记录的键盘交互,然后发邮件给本机保存

现在的问题是使用script ,它记录所有特殊的字符;因此输入的文件中将充满控制字符和ANSI转义序列。

各位可以尝试下script命令,请问有什么好的解决方法吗?

我想实现和改进下这个键盘交互记录的脚本,望大家集思广益
iguest 2011-02-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 justkk 的回复:]

晕,你不能这么拆啊
who -u am i 2>/dev/null

后面的部分表示把错误输出定向到/dev/null,也就是丢弃可能的错误输出
[/Quote]

了解, thanks.

who -u am i 2>/dev/null


anonymous pts/2 2011-02-28 13:46 . 5491 (192.168.1.1)

awk '{print $NF}'|sed -e 's/[()]//g'

这一句的作用就是把192.168.1.1提取出来。
justkk 2011-02-28
  • 打赏
  • 举报
回复
晕,你不能这么拆啊
who -u am i 2>/dev/null

后面的部分表示把错误输出定向到/dev/null,也就是丢弃可能的错误输出
iguest 2011-02-28
  • 打赏
  • 举报
回复
[anonymous@DELL Android]$ who -u am i 2
who: extra operand `2'
Try `who --help' for more information.


我边执行的结果。
iguest 2011-02-28
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 justkk 的回复:]

who am i
显示“我是谁”,也就是当前登录用户自己的信息
[/Quote]

who -u am i 2

那个参数 ‘2’ 作用是什么呢?
iguest 2011-02-28
  • 打赏
  • 举报
回复
TIME=`date +%Y%m%d.%H:%M.%S`

TIME这个变量定义了时间格式,如下所示:

TIME=20110228.16:08.56


if [ "$RMIP" = "" ]
then
RMIP=`hostname`
fi

判断RMIP变量是否为空,若空,则赋值为 hostname.

LOGS="/tmp/.hist/$TIME.$USER.$RMIP.hist"
HOST=`/bin/hostname`


给LOGS 及 HOST 变量赋值.


if [ ! -d /tmp/.hist ]
then
mkdir /tmp/.hist
chmod 777 /tmp/.hist
fi

如果 /tmp/.hist目录不存在的话。 mkdir 建立目录.hist, 并且将其属性改为可读、写、执行最大权限。

script -q $LOGS && mail -s "$TIME, $USER FROM $RMIP LOGIN $HOST" root < $LOGS

给 root 账户发邮件。
mail -s : Specify subject on command line.

justkk 2011-02-28
  • 打赏
  • 举报
回复
who am i
显示“我是谁”,也就是当前登录用户自己的信息
iguest 2011-02-28
  • 打赏
  • 举报
回复
RMIP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`

在自己机器上测试一下:

RMIP=192.168.1.1

打印出了,登录到此OS上的终端IP地址,楼主参考理解一下。
iguest 2011-02-28
  • 打赏
  • 举报
回复
我来试一下。

# /bin/sh
# hist_record.sh
# This script is often to write in /etc/profile.
# This script could record history input and send mail to root.

#后面是解释,类似于C语言的//

RMIP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
TIME=`date +%Y%m%d.%H:%M.%S`

RMIP是变量, =后面是赋值

who - show who is logged on
who -u list users logged in.
who -a time of last system boot.
who -m only hostname and user associated with stdin.

i 2 不清楚。


justkk 2011-02-28
  • 打赏
  • 举报
回复
记录交互历史信息,并给root用户发送邮件

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧