Category Archives: Tips & Tricks - Page 4

How to log bash history with syslog

There are two methods to get this thing done.

1. Trap method - drop the following snippet into either the per-user or system-wide bash profile (~/.bash_profile or /etc/profile, respectively)

unset HISTSIZE HISTFILESIZE
export HISTTIMEFORMAT='%F %T'
function log2syslog
{
   declare COMMAND
   COMMAND=$(fc -ln -0)
   logger -p local1.notice -t bash -i -- "${USER}:${COMMAND}"
}
trap log2syslog DEBUG

2. Prompt Method - this method logs by hacking the prompt command to call history and write to syslog.

PROMPT_COMMAND='history -a >(tee -a ~/.bash_history | logger -t "$USER[$$] $SSH_CONNECTION")'

Fixing the annoying "perl: warning: Setting locale failed" error message

 This is what I get:

[codesyntax lang="bash"]

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = "en_US.utf8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").

[/codesyntax]

 This is how I fix it:

[codesyntax lang="bash"]

export LANGUAGE=en_US.UTF-8
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
locale-gen en_US.UTF-8
apt-get install locales
dpkg-reconfigure locales

[/codesyntax]

The export lines above can also be saved into either ~/.profile or ~/.bashrc.

Configuring Postfix as a Gmail Relay

This document describes how to configure postfix to use gmail as relay server on debian squeeze.

1. Install the required package. On the dialog window please select No configuration.
[codesyntax lang="bash"]

apt-get install postfix libsasl2-2 ca-certificates libsasl2-modules

[/codesyntax]

2. Configure postfix
[codesyntax lang="bash"]

vim /etc/postfix/main.cf

[/codesyntax]

relayhost = [smtp.gmail.com]:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes
alias_maps = hash:/etc/aliases
inet_interfaces = 127.0.0.1

[codesyntax lang="bash"]

:wq

[/codesyntax]

3. Define an username and a password
[codesyntax lang="bash"]

vim /etc/postfix/sasl_passwd

[/codesyntax]

[smtp.gmail.com]:587 email@gmail.com:password

[codesyntax lang="bash"]

:wq

[/codesyntax]

4. Fix file permissions
[codesyntax lang="bash"]

chmod 400 /etc/postfix/sasl_passwd

[/codesyntax]

5. Generate Postix lookup table
[codesyntax lang="bash"]

postmap /etc/postfix/sasl_passwd

[/codesyntax]

6. Use the ca-certificate package we installed and tell it where it can validate the certificate

[codesyntax lang="bash"]

cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | tee -a /etc/postfix/cacert.pem

[/codesyntax]

7. Restart postfix
[codesyntax lang="bash"]

/etc/init.d/postfix restart

[/codesyntax]

8. Test installation
[codesyntax lang="bash"]

echo "Test from a postfix configured to use gmail as a relay server" | mail -s "Test from postfix" "email@example.com"

[/codesyntax]

How to grep by date range

Have you ever wonder how to grep a file within a date range?

[codesyntax lang="bash"]

cat file.log | sed -n '/2012-01-05 16:55/,/2012-01-05 18:30/p' > file.log.date_range

[/codesyntax]