1

I am running a script to check for security vulnerabilities. How do I add a timestamp to the script with results going into a file in /var/log/security-check

#!/bin/sh

# watch accounts - keep an eye on /etc/passwd,
#                  report if accounts change

secretcopy="$HOME/.watchdb"
tempfile="$HOME/.watchdb.new"
passwd="/etc/passwd"
compare=0               # by default, don't compare

trap "/bin/rm -f $tempfile" 0

if [ -s "$secretcopy" ] ; then
  lastrev="$(cat $secretcopy)"
  compare=1
fi

cat $passwd | cut -d: -f1 > $tempfile

current="$(cat $tempfile)"

if [ $compare -eq 1 ] ; then
  if [ "$current" != "$lastrev" ] ; then
    echo "WARNING: password file has changed"
    diff $secretcopy $tempfile | grep '^[<>]' |
        sed 's/</Removed: /;s/>/Added:/'
  fi
else
   mv $tempfile $secretcopy
fi

exit 0
5
  • The script should already be timestamped, every file usually is. If you want a log of when the script was run (not sure if that is what you mean), then you can do echo $(date) >> /var/log/security-check. Commented Jul 21, 2019 at 19:41
  • Can you explain a bit more? Every file when modified gets timestamped. You can do a ls -l to check that. Also if you need to timestamp any file, just use touch. touch <script_name> creates if it doesn’t exist. If it exists, timestamp of that script that you passed to it will be updated to latest time. Commented Jul 21, 2019 at 19:45
  • Thank you. The log is exactly what I wanted Commented Jul 21, 2019 at 22:04
  • Read mywiki.wooledge.org/Quotes and porkmail.org/era/unix/award.html Commented Jul 21, 2019 at 22:31
  • @I'L'I : Why do you want to echo here? data >>/var/log/cecurity-check would do as well. Commented Jul 22, 2019 at 5:57

1 Answer 1

1

Every file when modified gets timestamped. You can do a ls -l to check that. Also if you need to timestamp any file, just use touch. touch creates if it doesn’t exist. If it exists, timestamp of that script that you passed to it will be updated to latest time.

If you want to add time stamp to a file separately, use:

date +"%T” > "filename"

This post adds some more detail to the answer.

If you want to get a file to be stamped to latest time:

touch filename
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.