Huu Phan | Blog Linux operating system | Linux operating system -Blog Huu Phan | www.huuphan.com
huuphan.com. Powered by Blogger.

bash script examples

List all IP addresses connected to Server
 You may reading link as below:





#!/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

#!/bin/bash
send-mail(){
SUBJECT="Automated  Alert for DNS status after stop keepalived"
TO="[email protected]"
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
#!/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" [email protected]
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}


Thanks for reading bash script examples My blog Zimbra Mail Server,linux,bash script,centos,linux command I hope this is useful.

post new :

Huu Phan | Blog Linux operating system | Huu Phan ~ Zimbra Mail Server,linux,bash script,centos,linux command | www.huuphan.com