LoginSignup
1
2

More than 1 year has passed since last update.

historyを完全に消化

Last updated at Posted at 2020-11-25

概要

historyは「history -c」で削除できる。しかし、historyはメモリにも保管されていて、ログアウトした時にメモリから復元されてしまう。その結果、再びログインするとhistoryが再び表示される。

コマンドライン

次のスクリプトを実行すると完全に削除される。

cat /dev/null > ~/.bash_history && history -c && exit

Ansible

ディレクトリ構成

~/ansible
  |- server
     |- erase_history.yml
~/scripts
  |- server
     |- erase_script.sh

ansible-playbook

erase_history.yml

---
- name: check firewall setting
  hosts: "{{ host | default('network') }}"
  gather_facts: no

  tasks:
    - name: erase history
      shell:  "cat /dev/null > ~/.bash_history && history -c && exit"
      args:
        executable: /bin/bash

bashスクリプト

erase_history.sh

#!/bin/bash

HOST=""

usage() {
  echo "Usage: erase_history.sh [-n ホスト名] [-h ヘルプ]\nhistoryを初期化する" 1>&2
}

exit_abnormal() {
  usage
  exit 1
}

call_ansible_script() {
  cd ~/ansible/server/
  CMD="ansible-playbook erase_history.yml --ask-vault-pass"
  ARG=""
  if [ ! -z "$HOST" ]; then
    ARG="host=$HOST"
  fi
  ARG="$(echo -e "${ARG}" | sed -e 's/^[[:space:]]*//')"
  if [ ! -z "$ARG" ]; then
    CMD+=" -e \"$ARG\""
  fi
  eval ${CMD}
}

while getopts ":n:h" OPT; do
  case $OPT in
  n) HOST=${OPTARG};;
  h) usage
    exit 0
    ;;
  :) echo "-$OPTARGはホスト名が必須です"
    exit_abnormal;;
  esac
done
call_ansible_script
exit 0

以上

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2