2021-07-22

MONITOR ROUTE TABLE IN LINUX

MONITOR ROUTE TABLE IN LINUX

#!/bin/bash
#!/bin/sh -e
#/opt/script/routing-monitor-stats.sh
##############################################
#LastUpdate: 2021.07.23 8:28:04
##############################################
# Check ROUTING status every minute:
# * * * * * /opt/script/routing-monitor-stats.sh >> /opt/log/routing-monitor-stats/routing-monitor-stats.log 2>&1
##############################################
now1="$(date +'%Y.%m.%d-%H.%M.%S.%3N')"
##############################################
COMMAND1="ip route"
SUBNET1="192.168.202"
##############################################
mkdir -p /opt/log/routing-monitor-stats/
cd /opt/log/routing-monitor-stats/

VAR1=$($COMMAND1 | grep "$SUBNET1" | awk '{print $1}');
if [ -z "$VAR1" ]
then
      /opt/script/route-2-dbzone.sh
      echo "[$now1]: Subnet [$SUBNET1"".0/24] is MISSING";
else      
      echo "[$now1]: Subnet [$SUBNET1"".0/24] is RUNNING";
fi

#truncate file, keep 7days log:
FILE_NAME="/opt/log/routing-monitor-stats/routing-monitor-stats.log"
tail -10080 $FILE_NAME > $FILE_NAME.temp && cat $FILE_NAME.temp > $FILE_NAME
#THE_END

2021-07-17

Windows Subsystem for Linux Installation Guide for Windows 10/11

#Windows Subsystem for Linux Installation Guide for Windows 10/11:
#REF: https://docs.microsoft.com/en-us/windows/wsl/install-win10

# Manual Installation Steps
# Step 1 - Enable the Windows Subsystem for Linux
# You must first enable the "Windows Subsystem for Linux" optional feature before installing any Linux distributions on Windows.
# Open PowerShell as Administrator and run:
# PowerShell
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

# Step 2 - Check requirements for running WSL 2
# To update to WSL 2, you must be running Windows 10.

# Step 3 - Enable Virtual Machine feature
# Before installing WSL 2, you must enable the Virtual Machine Platform optional feature. Your machine will require virtualization capabilities to use this feature.
# Open PowerShell as Administrator and run:
# PowerShell
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
#Restart your machine to complete the WSL install and update to WSL 2.

# Step 4 - Download the Linux kernel update package
# Download the latest package:

# Step 5 - Set WSL 2 as your default version
# Open PowerShell and run this command to set WSL 2 as the default version when installing a new Linux distribution:
# PowerShell
wsl --set-default-version 2

# Step 6 - Install your Linux distribution of choice
# Open the Microsoft Store and select your favorite Linux distribution.
wsl --list --verbose

#9:36 2021.07.18
#REF: https://github.com/microsoft/WSL/issues/5393
If someone have trouble in Windows 11
you should:
wsl --update
wsl --shutdown


CONGRATULATIONS! You've successfully installed and set up a Linux distribution that is completely integrated with your Windows operating system!

2021-06-18

Get file content without comment

#Select all content, paste it into linux shell
#__________[CONFIG]:BEGIN
FILE_NAME="/opt/script/file-uncommented-line.sh"
now1="$(date +'%Y.%m.%d-%H.%M.%S.%3N')"
chattr -i -f $FILE_NAME
cp -v $FILE_NAME $FILE_NAME-[$now1].bk

cat <<EOF > $FILE_NAME
#!/bin/bash
#!/bin/sh -e
###################################
#FILE_NAME: $FILE_NAME
#Author: qwerty
#LastUpdate: #$now1
###################################
#Usage: file-uncommented-line.sh <filename>
###################################CONTENT:BEGIN
now1="\$(date +'%Y.%m.%d-%H.%M.%S.%3N')"
TEMP_LOC="/opt/temp"; mkdir -p \$TEMP_LOC

FileWithFullPath="\$1"
FileNameOnly="\${FileWithFullPath##*/}"
FileName_WithoutExtension="\${FileNameOnly%.*}"
FileExtensionOnly="\${FileNameOnly##*.}"

echo "FileWithFullPath         : \$FileWithFullPath"
echo "FileNameOnly             : \$FileNameOnly"
echo "FileName_WithoutExtension: \$FileName_WithoutExtension"
echo "FileExtensionOnly        : \$FileExtensionOnly"

echo "=========================="
echo "REMOVE HASH COMMENT: DONE"
grep -v '^ *#' \$FileWithFullPath > \$TEMP_LOC/\$FileName_WithoutExtension"_RemovedHash".\$FileExtensionOnly
sleep 2


echo "=========================="
echo "REMOVE EMPTY LINES: DONE"
sed '/^\$/d' \$TEMP_LOC/\$FileName_WithoutExtension"_RemovedHash".\$FileExtensionOnly > \$TEMP_LOC/\$FileName_WithoutExtension"_RemovedEmpyLines".\$FileExtensionOnly
sleep 2


echo "=========================="
echo "RESULT: "
ls -l \$TEMP_LOC/\$FileName_WithoutExtension*
###################################CONTENT:END
#THE-END
EOF

cat $FILE_NAME
chmod +x $FILE_NAME
chattr +i -f $FILE_NAME
echo "FILE_NAME: $FILE_NAME"; echo ""
#__________[CONFIG]:END



#RESULT:
root@host /opt/temp# file-uncommented-line.sh /etc/MailScanner/MailScanner.conf 
FileWithFullPath         : /etc/MailScanner/MailScanner.conf
FileNameOnly             : MailScanner.conf
FileName_WithoutExtension: MailScanner
FileExtensionOnly        : conf
==========================
REMOVE HASH COMMENT: DONE
==========================
REMOVE EMPTY LINES: DONE
==========================
RESULT: 
-rw-r--r--. 1 root root 14609 Jun 18 14:22 /opt/temp/MailScanner_RemovedEmpyLines.conf
-rw-r--r--. 1 root root 14983 Jun 18 14:22 /opt/temp/MailScanner_RemovedHash.conf
root@host /opt/temp# 

2020-12-14

GitHub Download SubFolder Only with SVN

#13:57 2020.12.14
#Git-Clone-SubFolderOnly-C3-UsingSVN.sh
#REF: https://coderwall.com/p/o2fasg/how-to-download-a-project-subdirectory-from-github
sudo apt-get install subversion
#REPO: 
#FULL: 
#https://github.com/mgiay/UbuntuDebianEnv.git
#https://github.com/mgiay/UbuntuDebianEnv/
#SUB : https://github.com/mgiay/UbuntuDebianEnv/tree/main/HaProxy/HaProxy-2.2.6

GITHUB_USERNAME="mgiay"
GITHUB_PROJECT="UbuntuDebianEnv"
GITHUB_SUB_FLD="HaProxy/HaProxy-2.2.6"
DEST_LOCAL="HaProxy-2.2.6"

#LISTING: svn ls https://github.com/$GITHUB_USERNAME/$GITHUB_PROJECT
#DOWNLOADING: 
svn export https://github.com/$GITHUB_USERNAME/$GITHUB_PROJECT/trunk/$GITHUB_SUB_FLD $DEST_LOCAL
#

#RESULT:


2020-11-26

Linux CLI, SpeedTest

#/opt/script/SPEEDTEST.sh
#10:06 2020.04.07
##############################
#REF: https://williamyaps.github.io/wlmjavascript/servercli.html
now1="$(date +'%Y.%m.%d_%H.%M.%S.%3N')"
CITY="Hanoi"
##############################

#_________[SpeedTest_Script]:BEGIN
RESULT_FLD="/opt/temp"
mkdir -p $RESULT_FLD
mkdir -p /opt/script/;cd /opt/script/
#wget https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py
#chmod +x speedtest.py
/opt/script/speedtest.py --list > $RESULT_FLD/speedtest-server-list-all.txt
cat $RESULT_FLD/speedtest-server-list-all.txt | grep "$CITY" | awk '{gsub(")",""); print $1}' > $RESULT_FLD/$CITY-isp-full-name.txt

cd $RESULT_FLD
while read LINE; \
do /opt/script/speedtest.py --server "$LINE"; \
done < $RESULT_FLD/$CITY-isp-full-name.txt > $RESULT_FLD/$CITY-isp-result-$now1.txt
#_________[SpeedTest_Script]:END


#______________________DELETE_OLD_DATA:BEGIN
# SERVER_NAME=$(hostname)
# mkdir -p /opt/bk/$SERVER_NAME/
# cd /opt/bk/$SERVER_NAME/
NumberOfFileWantToKeep=3
#Giu lai [3] file moi nhat trong thu muc [$FOLDER_DST]:
#XOA FILE:
#/bin/rm -rf `ls -t "result*.txt" | awk 'NR>3'`
/bin/rm -rf `ls -t *.txt | awk 'NR>3'`
#XOA FOLDER:
/bin/rm -rf `ls -td */ | awk 'NR>3'`
#______________________DELETE_OLD_DATA:END
#THE_END

2020-11-25

Solve “The following signatures were invalid: BADSIG” in Ubuntu

 #15:30 2020.11.25

Solve “The following signatures were invalid: BADSIG” in Ubuntu
In this post, we will discuss another very common problem that occurs when users upgrade or update their system. This error is “BADSIG” error which looks something like this:

W: A error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: http://extras.ubuntu.com precise Release: The following signatures were 
invalid: BADSIG 16126D3A3E5C1192 Ubuntu Extras Archive Automatic Signing Key

W: GPG error: http://ppa.launchpad.net precise Release:
The following signatures were invalid: BADSIG 4C1CBC1B69B0E2F4 Launchpad PPA for Jonathan French W: Failed to fetch 


http://extras.ubuntu.com/ubuntu/dists/precise/Release
sudo apt-get clean
cd /var/lib/apt
sudo mv lists oldlist
sudo mkdir -p lists/partial
sudo apt-get clean
sudo apt-get update


REF: https://itsfoss.com/solve-badsig-error-quick-tip/

2020-11-19

HaProxy: re-route to other backend based on REGEX

acl URL_upload_mgiay_com           hdr(host) -i    upload.mgiay.com

#CURRENT STORAGE: node02
acl URL_file_mgiay_com                  hdr(host) -i    file.mgiay.com



#OLD STORAGE: node01
#_____OLD_STORAGE_[10.0.2.127]:BEGIN
#2010-2019:
acl URL_file_mgiay_com_s01 url_reg /(resize|crop)/\d+x\d+/201[0-9]/\d{1,2}/
acl URL_file_mgiay_com_s01 url_reg /201[0-9]/\d{1,2}/

#2020.01-2020.10:
acl URL_file_mgiay_com_s01 url_reg /(resize|crop)/\d+x\d+/2020/(0[1-9]|10)/
acl URL_file_mgiay_com_s01 url_reg /2020/(0[1-9]|10)/

#2020.11.01-2020.11.18:
acl URL_file_mgiay_com_s01 url_reg /(resize|crop)/\d+x\d+/2020/11/(0[1-9]|1[0-8])/
acl URL_file_mgiay_com_s01 url_reg /2020/11/(0[1-9]|1[0-8])/
#_____OLD_STORAGE_[10.0.2.127]:END




use_backend BE_URL_upload_mgiay_com       if  URL_upload_mgiay_com
use_backend BE_URL_file_mgiay_com_s01     if  URL_file_mgiay_com URL_file_mgiay_com_s01
use_backend BE_URL_file_mgiay_com              if  URL_file_mgiay_com



#_____________________________________________mgiay_STORAGE:BEGIN
backend BE_URL_upload_mgiay_com
    mode http
    balance roundrobin
    #server Storage_127_8081 10.0.2.127:8081 check
    #server Storage_100_8081 2.2.2.100:8081 check backup
    #server 2.2.2.126:8081  2.2.2.126:8081 check
    server  10.0.2.126:8081  10.0.2.126:8081 check
    
backend BE_URL_file_mgiay_com_s01
    #acl blacklist src -f /etc/haproxy/blacklist.lst
    #http-request deny if blacklist
    mode http
    #balance source
    balance roundrobin
    #redirect scheme https code 301 if !{ ssl_fc }
    cookie BE_URL_file_mgiay_com_s01 insert indirect
    #server Storage_2.92_63202 10.0.2.92:63202 check
    #server Storage_127_64280 10.0.2.127:64280 check backup
    #server Storage_100_64280 10.0.2.100:64280 check backup
    server  10.0.2.127:64280 10.0.2.127:64280 cookie 127_64280 weight 1 check
    
backend BE_URL_file_mgiay_com
    # acl blacklist src -f /etc/haproxy/blacklist.lst
    # http-request deny if blacklist
    mode http
    balance roundrobin
    #server Storage_2.92_63202 10.0.2.92:63202 check    
    #server Storage_127_64280 10.0.2.127:64280 check backup 
    #server Storage_100_64280 10.0.2.100:64280 check backup
    cookie BE_URL_file_mgiay_com insert indirect
    server  10.0.2.126:64280 10.0.2.126:64280 cookie 126_64280 weight 1 check
#_____________________________________________mgiay_STORAGE:END