Script命令

Script可用于记录当前用户的操作记录,默认写在typescript文件中。

用法:
usage: script [-a] [-f] [-q] [-t] [file]
-a:追加到输出文件
-f:输出到指定文件
-q:安静模式,不显示Script started…这些信息
-t:指定时间文件和日志文件,格式:-t time.log action.log,用于回放操作。
 
同步操作,可用于教学演示:
步骤:
1、 在教师机执行script –f,网上有些
2、 开始演示操作
3、 在学生机执行tail –f ceshi.log
 
终端一:
[root@10:09:44 soft]#script -f ceshi.log
Script started, file is ceshi.log
[root@10:10:14 soft]#uname -r
2.6.18-164.el5PAE
[root@10:10:30 soft]#cls
bash: cls: command not found
[root@10:10:35 soft]#cat ceshi.log
Script started on 2012年08月28日 星期二 10时10分14秒
[root@10:10:14 soft]#uname -r
2.6.18-164.el5PAE
[root@10:10:30 soft]#cls
bash: cls: command not found
终端二:
[root@10:09:32 soft]#tail -f ceshi.log
Script started on 2012年08月28日 星期二 10时10分14秒
[root@10:10:14 soft]#uname -r
2.6.18-164.el5PAE
[root@10:10:30 soft]#cls
bash: cls: command not found
[root@10:10:35 soft]#cat ceshi.log
Script started on 2012年08月28日 星期二 10时10分14秒
[root@10:10:14 soft]#uname -r
2.6.18-164.el5PAE
[root@10:10:30 soft]#cls
bash: cls: command not found
    --- 这位兄弟使用了mkfifo也可以实现此功能,其实以上的命令也可以满足。http://blog.csdn.net/zs75622126/article/details/7291204
 
 
制作回放操作:
1、 录制:
script -t 2>time.log -a output.log
开始演示
Exit
 
执行exit后录制完成。
Time记录操作时间,output将命令以追加方式写入
2、 回放:
回放会用到scriptreplay,格式:scriptreplay time.log output.log,两个文件的顺序不能放反。
在RHEL 5.4中无此命令。需要下载手动安装:
ftp://ftp.kernel.org/pub/linux/utils/util-linux/v2.14/ 中的 util-linux-ng-2.14.1.tar.gz
然后解压编译安装:
tar zxvf util-linux-ng-2.14.1.tar.gz
./configure && make && make install
 
可以通过此命令记录用户所有的操作过程:
 
命令记录:
#!/bin/bash
user_ip=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
 if [ "$USER_IP" = "" ]
 then
         USER_IP=`hostname`
 fi
 if [ ! -d /tmp/login ]
 then
         mkdir /tmp/login
         chmod 777 /tmp/login
 fi
 if [ ! -d /tmp/login/${LOGNAME} ]
 then
         mkdir /tmp/login/${LOGNAME}
         chmod 300 /tmp/login/${LOGNAME}
 fi
if [ $UID -ge 500 ]
then
  /usr/bin/script -a -f -q /tmp/login/${LOGNAME}/${USER_IP}_login.`date '+%Y%m%d%H%M%S'`
  chmod 600 /tmp/login/${LOGNAME}/*login* 2>/dev/null
fi
 
加入记录:
 
#!/bin/bash
user_ip=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
 if [ "$USER_IP" = "" ]
 then
         USER_IP=`hostname`
 fi
 if [ ! -d /tmp/login ]
 then
         mkdir /tmp/login
         chmod 777 /tmp/login
 fi
 if [ ! -d /tmp/login/${LOGNAME} ]
 then
         mkdir /tmp/login/${LOGNAME}
         chmod 300 /tmp/login/${LOGNAME}
 fi
if [ $UID -ge 500 ]
then
  /usr/bin/script –t 2>/tmp/login/${LOGNAME}/${USER_IP}_`date '+%Y%m%d%H%M%S'`.time -a -f -q /tmp/login/${LOGNAME}/${USER_IP}_ `date '+%Y%m%d%H%M%S'`.log
  chmod 600 /tmp/login/${LOGNAME}/*login* 2>/dev/null
fi
 
 
操作日志记录脚本:
 
history
USER_IP=`who -u am i 2>/dev/null| awk ‘{print $NF}’|sed -e ’s/[()]//g’`
if [ "$USER_IP" = "" ]
then
USER_IP=`hostname`
fi
if [ ! -d /tmp/dbasky ]
then
mkdir /tmp/dbasky
chmod 777 /tmp/dbasky
fi
if [ ! -d /tmp/dbasky/${LOGNAME} ]
then
mkdir /tmp/dbasky/${LOGNAME}
chmod 300 /tmp/dbasky/${LOGNAME}
fi
if [ ! -d /tmp/dbasky/${LOGNAME}/SIP=${USER_IP}.txt ]
then
touch /tmp/dbasky/${LOGNAME}/SIP=${USER_IP}.txt
chmod 600 /tmp/dbasky/${LOGNAME}/SIP=${USER_IP}.txt
fi
export HISTSIZE=4096
#DT=`date “+%Y%m%d_%H%M%S”`
export HISTFILE=”/tmp/dbasky/${LOGNAME}/${USER_IP} dbasky”
chmod 600 /tmp/dbasky/${LOGNAME}/*dbasky 2>/dev/null
if [ -a "/tmp/dbasky/${LOGNAME}/${USER_IP} dbasky" ]
then
cat “/tmp/dbasky/${LOGNAME}/${USER_IP} dbasky” | perl -pe ’s/(/d+)/localtime($1)/e’ >> /tmp/dbasky/${LOGNAME}/SIP=${USER_IP}.txt
fi
 

你可能感兴趣的:(script,记录日志)