Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

Thursday, November 3, 2016

Shell Script grep The Same Word In 2 Files

Shell Script grep The Same Word In 2 Files
cat test1.txt
test001
test002
test003
test004
test005

cat test2.txt
test003
test004
test005
test006

./grep-word-2-files.sh
test003
test004
test005

cat grep-word-2-files.sh
#!/bin/bash
rm -rf /home/vannak/diff/match-word.txt
for i in `cat /home/vannak/diff/test1.txt`
do
grep $i /home/vannak/diff/test2.txt >> /home/vannak/diff/match-word.txt
done
cat /home/vannak/diff/match-word.txt

Friday, February 19, 2016

Script Check WiFi Users Connected

Script Check WiFi Users Connected

#!/bin/bash
# By: Vannak Ken
# This script going to check on the connected user on wifi in the wifi listed.
TIMESTAMP=`date +%Y-%m-%d-%H:%M:%S`
MAILTO="ken.vannakk@gmail.com vannak.ken@example.com"
for i in `cat /opt/scripts/wifi/all-wifi.txt`;
    do
        # ------------------------------- NUMBER USERS CONNECTED ------------------------------------------ #
        USERCONNECTED=`snmpwalk $i -v 1 -c PuBlic_SNMP-RO enterprises.41112.1.4.5.1.15.1 |awk '{print $4}'`
        echo "$i : $USERCONNECTED" >> /opt/scripts/wifi/wifi-report-$TIMESTAMP.txt
        #USERS CONNECT MORE THE 20 SEND EMAIL
        USERMAX=20
        if [[ "$USERCONNECTED" -ge "$USERMAX" ]];
                then
                        echo "$i Users More Than 20." > /opt/scripts/wifi/wifi-20users-or-more.txt
                        mail -s "$i Users More Than 20" $MAILTO < /opt/scripts/wifi/wifi-20users-or-more.txt
                else
                        echo "$i Users Less Than 20"
        fi
        # ------------------------------------------------------------------------------------------------- #
        # -------------------------------- CHECK WIFI NO USER OR CAN NOT CONNECTED ------------------------ #
        NOUSER=0
        if [[ "$USERCONNECTED" == "$NOUSER" ]]; then
                       echo "$i No Users connected." > /opt/scripts/wifi/wifi-no-users-connected.txt
                        mail -s "$i No Users connected." $MAILTO < /opt/scripts/wifi/wifi-no-users-connected.txt
                else
                        if [[ -z "$USERCONNECTED" ]] ; then
                                echo "$i Cannot Connected." > /opt/scripts/wifi/wifi-cannot-connected.txt
                                mail -s "$i Cannot Connected" $MAILTO < /opt/scripts/wifi/wifi-cannot-connected.txt
                           else
                                echo "$i Connected normally."
                        fi
        fi
        # ------------------------------------------------------------------------------------------------- #
    done
# Send email with ttached the report
mail -s "Wifi Report - $TIMESTAMP" $MAILTO < /opt/scripts/wifi/wifi-report-$TIMESTAMP.txt
rm -rf /opt/scripts/wifi/wifi-20users-or-more.txt
rm -rf /opt/scripts/wifi/wifi-no-users-connected.txt
rm -rf /opt/scripts/wifi/wifi-cannot-connected.txt
# Remove old report
find /opt/scripts/wifi/ -name "*.txt" -mtime +30 -exec rm -f {} \;

Friday, February 5, 2016

Script Monitor SSH

Script Monitor SSH

This is the script that monitor SSH session that check grep the specific word in the SSH login file. 

#!/bin/bash
cat /var/log/secure |egrep '(Accepted password|session opened for)' > /var/log/monitor-ssh.log
tail -F /var/log/secure | egrep --line-buffered '(Accepted password|session opened for)' | while read ;
do
mail -s "User Loged In - ServerName" ken.vannakk@gmail.com < /var/log/monitor-ssh.log ;
done
# ------------------------------------------------ End Of Script ----------------------------------------------------#

Copy this content above  put in a file and set in the crond.

Note: Run this script only one time, and keep it process running to keep monitor.