Bash Scripting Made Simple: Practical Examples

Bash Scripting Made Simple: Explore practical examples to streamline your workflow and enhance your bash scripting skills effortlessly.

List all IP addresses connected to Server

Discovering all IP addresses connected to the server is essential for network monitoring and security. Utilize tools like netstat or ss commands to obtain this information efficiently.

#!/bin/bash
while true; do
echo "CTRL+C TO BREAK"
netstat -tnu | grep :22 | awk '{print $5}' | cut -f1 -d : | sort | uniq -c | sort -nr | head -n 8
sleep 15
clear
done
###########################################################################################
# site: huuphan.com
# author: phan van huu
# List all IP addresses connected to Server
# For example: list ip address connected to server via ssh port 22
# awk '{print $5}' | cut -f1 -d : | sort | uniq -c | sort -nr | head -n 8
# awk '{print $5}': the display 5th field only
# sort | uniq -c | sort -nr: Sort the list, group it and sort it again in reverse order.
# head -n 8: The display te first 8 line.

###########################################################################################

How to check bind9 DNS service running and starting bind9 DNS services

To verify whether the bind9 DNS service is running, utilize commands like systemctl status bind9. To start the bind9 DNS service, execute systemctl start bind9. These commands offer quick checks and actions for managing bind9 service status on your system.

#!/bin/bash
send-mail(){
SUBJECT="Automated  Alert for DNS status after stop keepalived"
TO="pvhuu90@huuphan.com"
MESSAGE="/tmp/message.txt"
echo "Service DNS die after Stop keepalived:!" >> $MESSAGE
echo "Time: `date +%Y-%m-%d` " >> $MESSAGE
/usr/bin/mailq  "$SUBJECT" "$TO" < $MESSAGE
rm $MESSAGE
}
#For example: H=192.168.1.12
H=`ifconfig eth1| grep 'inet addr:' | cut -d: -f2 | awk '{print $1}'`
ps -e | grep named &>/dev/null
if [ $? != 0 ]; then
        D=`dig -x $H | grep -o 'ANSWER: [0-9]\+' | awk '{print $2}'`;
        if [ $D == 0 ]; then
                         echo "Stopping Keepalived services!!!"
                        /etc/init.d/named start &>/dev/null
                        send-mail ## Sending mail
                        exit 1
                fi
else
        echo "DNS bind9 Running!!"
fi

How to print network interface name exclude network interface lo and export to text.txt

To print network interface names excluding 'lo' interface and export to 'text.txt', execute the command 'ip -o link show | awk '!/lo:|^[0-9]:/ {print $2}' > text.txt'. This command filters out the 'lo' interface and saves the results to the specified text file.
#!/bin/bash
rm -rf /tmp/text.txt;
for i in `grep -H . /sys/class/net/*/address`
do
if [ `echo ${#i}` != 43 ]; then
echo ${i:15:4} >> /tmp/text.txt
fi
done
How to change /etc/network/interfaces file 
#!/bin/bash
path_file1=/tmp/text.txt
if [ -f $path_file1 ]; then
echo "File existing";
fi
for ii in `cat $path_file1`
do
if [ -f /etc/sysconfig/network-scripts/ifcfg-$ii ]; then
echo "Backup file";
mkdir -p /tmp/backup-interface &>/dev/null
cp /etc/sysconfig/network-scripts/ifcfg-$ii /tmp/backup-interface &>dev/null
fi
cat <<EOF >>interface-$ii
DEVICE=$ii
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=none
IPADDR="Edit manual"
NETMASK=255.255.255.0
DNS1=192.168.131.143
GATEWAY=192.168.131.2
#DNS2=8.8.8.8
#DNS3=8.8.4.4
USERCTL=no
PEERDNS=no
EOF
done
How to send notification email use bash script
# echo "Content of notify" | mail -s "Subject line" huupv@huuphan.com
How to get file_new_20170202 file newest with string

FILE=`ls -lrt /tmp | grep "file_new" | awk '{ f=$NF }; END{ print f }'`
FILE='/tmp/'${FILE}

Comments

Popular posts from this blog

zimbra some services are not running [Solve problem]

Bash script list all IP addresses connected to Server with Country Information

Zimbra Client host rejected Access denied fixed