#!/bin/bash
#################################################
# 记录用户登录ip，以ip分类记录历史命令
# author:wangzhigang
# date:20200824
#################################################

USER_IP=`who -u am i | awk '{print $(NF)}' | sed -e "s/[()]//g"`
if [ "$USER_IP" = "" ];then
	USER_IP=`hostname`
fi
if [ ! -d /var/log/history ];then
	mkdir -p /var/log/history
fi

HISTFILE_PATH=/var/log/history/$USER_IP
if [ ! -f $HISTFILE_PATH ];then
	touch $HISTFILE_PATH
	chmod 666 $HISTFILE_PATH
fi

DATESTR=`date +"%y%m%d %H:%M:%S"`

echo " " >> $HISTFILE_PATH
echo " -------${DATESTR}------ " >> $HISTFILE_PATH

HISTSIZE=5000
HISTFILE=$HISTFILE_PATH
shopt -s histappend
PROMPT_COMMAND="history -a"
 