Category Archives: Scripts

NTP offset monitoring bash script

#!/bin/bash
# Created by Rodel
# 27/08/2020
MAIL=`which mail`
THIS_SERVER=`hostname -s`
RCPTS="rodel@test.com"
check_ntp_return_value () {
        ntpq_count=$(ntpq  -p |grep  -e '*'|awk  '{print $9}' | wc -l)
#       ntpq_count="0"
        if [ "$ntpq_count" -ne "0" ];
                then
                        check_ntp_offset;
                else
                        message=$(echo "No NTP master ip address assign <example: *192.168.0.x>. Please login to the server and check. Thank you.")
                        send_email "$message";
        fi
}
check_ntp_offset () {
        ntpq_p=$(ntpq  -p |grep  -e '*'|awk  '{print $9}')
        ntpq_p=${ntpq_p%.*}
#       ntpq_p="6001"
        ntpq_server=$(ntpq  -pn |grep  -e '*'|awk  '{print $1}' | sed 's/*//g')
        if [ "$ntpq_p" -gt "6000" ];
                then
                        message1=$(echo "Critical - `hostname` Offset value is $ntpq_p greater than 6000 milliseconds")
                        message2=$(ntp_sync)
                                                send_email "$message1 \n\n Executing ntpdate...\n$message2";
                else
                        exit 0
        fi
}

ntp_sync () {
#       ntpdate -q $ntpq_server >> /dev/null 2>&1
        ntpdate -q $ntpq_server
}
send_email () {
        STR2="${1}"
        echo -e "${STR2}" | ${MAIL} -s "[${THIS_SERVER}] NTP Synchronization Alert" "${RCPTS}"
}
check_ntp_return_value

Windows Pre migration checklist script

@echo off
echo. > %computername%.txt
echo. >> %computername%.txt
echo ======IP Configuration============================================================== >> %computername%.txt
ipconfig /all >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
echo ======Net Start===================================================================== >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
net start >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
echo =====Netstat Output================================================================= >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
netstat -anob >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
echo ======Route Print=================================================================== >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
route print >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
echo ======System Information============================================================ >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
systeminfo >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
echo ======Services Query================================================================ >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
sc query state= all >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
echo ======Check Disk==================================================================== >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
chkdsk >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
echo ======Disk Volumes================================================================== >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
diskpart /s list.txt >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
echo =====Serial Number================================================================== >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
wmic /append:%computername%.txt bios get serialnumber/Format:List
echo. >> %computername%.txt
echo. >> %computername%.txt
echo =====services List and Status======================================================= >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
wmic /append:%computername%.txt Service Get Name, DisplayName, ServiceType, Started, StartMode, Status/Format:List
echo. >> %computername%.txt
echo. >> %computername%.txt
echo =====CPU INFO======================================================================= >> %computername%.txt
echo. >> %computername%.txt
echo. >> %computername%.txt
wmic /append:%computername%.txt cpu get deviceid, CpuStatus, NumberofCores, NumberofLogicalProcessors/Format:List
echo. >> %computername%.txt
echo. >> %computername%.txt

Sample Ping Script

It’s just a simple ping script.

#!/bin/bash
# Program name: test.sh
date
cat /root/listping.txt | while read output
do
ping -c 1 "$output" > /dev/null
if [ $? -eq 0 ]; then
echo "node $output is up"
else
echo "node $output is down"
fi
done

Add restricted user in linux manually.

The code below will only allow the following commands.
ping
ssh
telnet
traceroute
ifconfig
route


Add_Restricted_User.sh


#!/bin/sh
# Add restricted user in linux manually.
# August 13, 2015
# Created by Rodel

for a in `cat /opt/scripts/Add_Restricted_User/serverlist.txt`;
do
for b in `cat /opt/scripts/Add_Restricted_User/usernames.txt`;
do echo $a $b; ssh $a "useradd -s /bin/rbash '$b'; echo -e '$b:mypassword' | chpasswd;\
cd /home/$b;\
rm -rf .bash_logout .bash_profile .bashrc .profile .bash_login .emacs .mozilla;\
mkdir /home/$b/bin;\
cd /home/$b/bin;\
ln -s /bin/ping ping;\
ln -s /usr/bin/ssh ssh;\
ln -s /usr/bin/telnet telnet;\
ln -s /bin/traceroute traceroute;\
ln -s /sbin/ifconfig ifconfig;\
ln -s /sbin/route route;\
chmod 2070 -R /home/$b;\
chown root:$b /home/$b;\
chown root:root /home/$b/bin -Rf;\
chmod -Rf 755 /home/$b/bin;\
chmod u+s /home/$b/bin/ping;"
rsync -azP .profile root@$a:/home/$b;\
ssh $a "chown root:$b /home/$b/.profile && chmod 750 /home/$b/.profile";done
done

.profile environmental settings.
export PATH=$HOME/bin
export PS1="[\u@\h \W]$ "
export HISTTIMEFORMAT='%Y-%m-%d %H:%M:%S - '
export PROMPT_COMMAND='history -n;history -a'
export HISTSIZE=10000
alias netinfo='echo -e "Ifconfig\n"; ifconfig -a; echo -e "Route\n"; route -n'
alias alias=""

readonly PROMPT_COMMAND
readonly HISTSIZE
readonly HISTFILE
readonly HOME
readonly HISTIGNORE
readonly HISTCONTROL
echo -e "\nAvailable Commands:\nping\nssh\ntelnet\ntraceroute\nnetinfo - print network info\n\n"