linux poison RSS
linux poison Email

Bash Script: Write debugging information to /var/log/messages

logger is a shell command interface to the syslog system log module, it appends a user-generated message to the system log (/var/log/messages). You do not have to be root to invoke logger.

Below is the simple bash script to log the debugging information to /var/log/message
Feel free to copy and use the below code

Source: cat logger.sh
#!/bin/bash

echo -n "Enter your username: "
read username

logger -i "Username $username started the script"

echo -n "Enter your Password: "
read password

# This is just an sample for comparing the password
if [ "$password" = "linuxpoison" ]; then
        echo "welcome $username"
        logger -i "Authentication successful for $username."
else
        echo "your Username or password does not match"
        logger -i "Authentication failed for $username."
fi

 Output:
./logger.sh
Enter your username: nikesh
Enter your Password: nikesh
your Username or password does not match

./logger.sh
Enter your username: nik
Enter your Password: linuxpoison
welcome nik

And, here is what we can see in the /var/log/message log file ....

$ tail -f /var/log/message
......
Sep 13 13:56:53 poison poison[18790]: Username nikesh started the script
Sep 13 13:56:55 poison poison[18792]: Authentication failed for nikesh.
Sep 13 13:57:12 poison poison[18801]: Username nik started the script
Sep 13 13:57:15 poison poison[18802]: Authentication successful for nik.




1 comments:

Post a Comment

Related Posts with Thumbnails