Skip to content

Instantly share code, notes, and snippets.

@speaktech-account
Last active October 19, 2018 10:58
Show Gist options
  • Save speaktech-account/b4af94f22db1ab23d8b484ecc6d2fc48 to your computer and use it in GitHub Desktop.
Save speaktech-account/b4af94f22db1ab23d8b484ecc6d2fc48 to your computer and use it in GitHub Desktop.
When I do bad things, I tried to construct an unfriendly world (Linux) to be reported immediately <Prequel> ref: https://qiita.com/speaktech/items/43f2c99e27a4515eddac
mkdir /etc/vimrc.d
chmod 755 /etc/vimrc.d/watchvim.sh
chmod 644 /etc/vimrc.d/watchvim.conf
[badman@notkind ~]$ vim /home/badman/.bash_history
[badman@notkind ~]$ sudo vim /var/log/yum.log
# Force users to use vim instead of vi
alias vi="vim -c ':syntax off'"
Sep 25 14:54:33 notkind watchvim.sh: /var/log/yum.log has been edited by badman.
augroup watchvim
autocmd!
autocmd BufRead * silent :let $PREHASH = system('md5sum ' . expand('%:p') . ' | awk "{print \$1}"')
autocmd BufWritePost * silent !/etc/vimrc.d/watchvim.sh <afile> $PREHASH $USER
augroup END
# Created by watchvim.sh
# This is the watchvim.sh configuration file.
# watchvim.sh watches only the files you specify with absolute path below and logger who edits them.
# when they are edited with Vim.
/var/log/*
*bash_history
#!/bin/bash
PROGNAME=$(basename $0)
PROGDIR=$(dirname $0)
CONFPATH="$PROGDIR/${PROGNAME%.*}.conf"
VERSION="1.1"
usage() {
echo "
Usage:
$PROGNAME [FILE] [FILEHASH] [USER]
Description:
$PROGNAMEは、vimで編集されたファイルの名前をログ出力するためのシェルスクリプトです。
/etc/vimrcから呼び出されることを想定しています。
同一ディレクトリに配置される${PROGNAME%.*}.confに記載されたファイル一覧を監視対象とします。
/etc/vimrcにおける使用例:
autocmd BufRead * silent :let $PREHASH = system('md5sum ' . expand('%:p') . ' | awk \"{print \$1}\"')
autocmd BufWritePost,FileWritePost * silent !/etc/vimrc.d/$PROGNAME <afile> \$PREHASH \$USER
Options:
--help, -h ヘルプを表示
--version, -v バージョン情報を表示"
exit 1
}
for OPT in "$@"
do
case "$OPT" in
'-h'|'--help' )
usage
exit 1
;;
'-v'|'--version' )
echo $VERSION
exit 1
;;
-* )
echo "$PROGNAME: illegal option -- '$(echo $1 | sed 's/^-*//')'" 1>&2
exit 1
;;
*)
if [[ ! -z "$1" ]] && [[ ! "$1" =~ ^-+ ]]; then
param+=( "$1" )
shift 1
fi
;;
esac
done
if [ ${#param[@]} -ne 3 ]; then
echo "$PROGNAME: 3 arguments is required." 1>&2
echo "Try '$PROGNAME --help' for more information." 1>&2
exit 1
fi
if [ ! -e $CONFPATH ]; then
echo -e "# Created by $PROGNAME
# This is the $PROGNAME configuration file.
# $PROGNAME watches only the files you specify below and logger who edits them
# when they are edited with Vim." > $CONFPATH 2>&1
fi
set -f
for FILEPATH in `grep -v "^#" $CONFPATH`;
do
EDITFILE=`readlink -f ${param[0]}`
if [[ $EDITFILE == $FILEPATH ]]; then
EXHASH=`md5sum $EDITFILE | awk '{ print $1 }'`
if [ ${param[1]} != $EXHASH ]; then
logger -p local0.warning -t "$PROGNAME" "$EDITFILE has been edited by ${param[2]}."
fi
break
fi
done
set +f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment