Advertisement
vartik

logger.sh

Jan 24th, 2024 (edited)
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.05 KB | Software | 0 0
  1. #!/bin/sh
  2. VERSION=1.2
  3. PORT=514
  4. PRIORITY=14
  5.  
  6. sendmsg() {
  7.     echo "<$PRIORITY>$(date +'%b %e %T') $HOSTNAME $USER: $*" | nc -w 5 $SERVER $PORT 2> /dev/null
  8. }
  9.  
  10. MSG=
  11. j=
  12. for i in $* ; do
  13.     if [ "$j" == "-n" ] ; then
  14.         SERVER=$i
  15.     elif [ "$j" == "-P" ] ; then
  16.         PORT=$i
  17.     elif [ "$j" == "-p" ] ; then
  18.         PRIORITY=$i
  19.     elif [ "${i:0:1}" != "-" ] ; then
  20.         if [ -z "$MSG" ] ; then
  21.             MSG="$i"
  22.         else
  23.             MSG="$MSG $i"
  24.         fi
  25.     fi
  26.     j=$i
  27. done
  28. if [ -z "$SERVER" ] ; then
  29.     echo "BusyBox-friendly syslog sender v$VERSION"
  30.     echo "Author: Branislav Vartik"
  31.     echo
  32.     echo "Usage: $0 -n <server> [-P <port>] [-p <priority>] [message]"
  33.     echo
  34.     echo "Defaults: message is read from STDIN"
  35.     echo "          port: 514 (only TCP due the BusyBox's NetCat limitations)"
  36.     echo "          priority: 14 (user.info)"
  37. else
  38.     [ -z "$USER" ] && USER=$LOGNAME
  39.     [ -z "$HOSTNAME" ] && HOSTNAME=$(grep -m 1 ^ /etc/hostname)
  40.     if [ -z "$MSG" ] ; then
  41.         # stdin which can be without last newline
  42.         grep ^ | while read i ; do
  43.             sendmsg "$i"
  44.         done
  45.     else
  46.         sendmsg "$MSG"
  47.     fi
  48. fi
  49.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement