Snippets

Edel SM misc script for ISPs annoyances

Created by Edelberto Mania
#!/bin/bash
# check if 100.x.x.x IP is in the traceroute, indication that this connection is in CGNAT
# for single-isp only. needs slight modification on multi-homed routers
# run in crontab (daily, for example)
# Edelberto Mania <sierra2@gmail.com>
# 20211116
                                                                             
## adjust values below ######################################################
test_ip='8.8.8.8'   
log_dir="/data/logs"           
log="${log_dir}/cgnat-pldt.log"                
cgnat_ip_previous="${log_dir}/cgnat-pldt.state"                              
#############################################################################
                                                              
## 100 is for PLDT. maybe GLOBE/CONVERGE too. i only have PLDT        
cgnat_octet_one='100' # first octet of 100.x.x.x, assuming non-private
                                                                                      
cgnat=$(traceroute -n ${test_ip}|awk '{print $2}'|egrep "(^|)${cgnat_octet_one}(.|$)")
ret=$?
                           
function cgnat_detected() {                                     
        if [ "$(cat ${cgnat_ip_previous})" != "${cgnat}" ]; then         
                echo "$(TZ=Asia/Manila date) - CGNAT: ${cgnat}" >> ${log}
                echo ${cgnat} > ${cgnat_ip_previous}
        fi
}
                              
function no_cgnat_detected() {                              
        if [ "$(cat ${cgnat_ip_previous})" != "NONE" ]; then         
                echo "$(TZ=Asia/Manila date) - CGNAT: NONE" >> ${log}
                echo NONE > ${cgnat_ip_previous}
        fi
}
                                     
## create empty file if doesn't exist                      
[ ! -f ${cgnat_ip_previous} ] && touch ${cgnat_ip_previous}
                         
if [ ${ret} -eq 0 ]; then
        cgnat_detected
else                                                                                                
        ## TODO: add additional check and to verify when NAT is NOT detected, ie if internet is down
        no_cgnat_detected
fi

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.