Shell 实践、常用脚本进阶

1.备份单个文件

#!/bin/bash
#备份单个文件
DATE=`/bin/date +%y%m%d`
/bin/tar -czpf /backup/$1.$DATE.tar.gz /backup/$1 >/dev/null
if [ $? -eq 0 ]
then
        echo "$1 备份成功" >> /backup/$1.bak.log
else
        echo "备份$1失败" >> /backup/$1.bak.log
fi
View Code

 

2.查找大于10M的文件和目录

#!/bin/bash
#查找大于10M的文件和目录
ll -h /var/log | awk '$5>10 && $5<10240 {print $5,$9}' > file.txt
cat file.txt
View Code

 

3.ping网段内所有的主机存活

#!/bin/bash
#ping网段内所有主机存活
for i in {1..254}
do
if ping -w 1 -c 1 192.168.31.$i | grep "100%" >/dev/null
then
echo "192.168.31.$i  YES!!!"  
else
echo "192.168.31.$i  NO!!!" 
fi
done
View Code

 

4.查找前十个404的ip与访问量最多的十个ip

#/bin/bash
#查找前十个404的ip与访问量最多的前十个ip
no=`cat /var/log/httpd/access_log | awk '/404/{print $1}' | sort | uniq -c | sort -nr | head -10 | awk '{print $2}' `
pv=`cat /var/log/httpd/access_log | awk '{print $1}' | sort | uniq -c | sort -nr | head -10 | awk '{print $2}' `
echo "fang wen bu dao URL$no"
echo "zui da fang wen liang $pv"
View Code

 

5.算平均值,并以排序形式有规矩的列出

#!/bin/bash
#算平均值,并以排序形式有规矩的列出
cat 1 |awk '{print $1,$2.($3+$4+$5)/3}' >/1.txt 
cat /1.txt  |awk '{print $1," " $2 "  ", $3}' |sort -r    >/fan.txt
sed '1c 班级   姓名     平均分数 ' /fan.txt   >/fan 
cat /fan.txt 
View Code

 

6.ssh密码被尝试10次以上就禁用此ip

#!/bin/bash
#ssh密码被尝试十次以上就禁用此ip
ip=`cat /var/log/secure | awk '/^Apr/{print $4}' | sort | uniq -c |sort -nr | head -1 | awk '{print $2}' `
pv=`cat /var/log/secure | awk '/^Apr/{print $4}' | sort | uniq -c |sort -nr | head -1 | awk '{print $1}'`
if [ $pv -gt 10 ];then
iptables -A INPUT -s $pv -p tcp --dport 22 -j DROP
echo "yi lan jie $ip "
else
echo "ok !"
fi
View Code

 

7.远程备份周日完全备份,周一至周六增量备份

crontab=`echo "* * * * */0 sh beifen.sh" > /var/spool/cron/root`
scp=`echo "* * * * */0 scp access_log xsk@10.1.1.0:/home/xsk" >> /var/spool/cron/root`
A=`echo "* * * * 1-6 rsync -arHz --delete access_log xsk@10.1.1.10:/home/xsk " >> /var/spool/cron/root`
if [ -e /etc/spool/cron/root ];then
mkdir /var/spool/cron/root
echo "$crontab"
echo "$scp"
echo "$A"
else
echo "$crontab"
echo "$scp"
echo "$A"
fi
View Code

 

8.服务监控http。发现服务关闭则发送邮件

#!/bin/bash
#服务器监控http。发现服务关闭则发送邮件
if pgrep httpd>&/dev/null;then
        echo "fu wu zheng chang"
else
    service httpd restart
        usernames="usr01 usr02 usr03"
        for user in $usernames
        do
                mail -s "warning" $user@localhost.localdomain<zhengwen
        done
fi
View Code

 

9.修改网卡配置文件内的ip与子网掩码

#/bin/bash
A=`awk -F\= '/^IPADDR/{print $2}' /etc/sysconfig/network-scripts/ifcfg-eth0`
B=`awk -F\= '/^NETMASK/{print $2}' /etc/sysconfig/network-scripts/ifcfg-eth0`
sed -i '/^IPADDR/s/'$A'/'$1'/' /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i '/^NETMASK/s/'$B'/'$2'/' /etc/sysconfig/network-scripts/ifcfg-eth0
View Code
#!/bin/sh
read -p "input" ip
grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 &>/dev/null
if [ $? -gt 0 ]
then
echo "IPADDR=$ip " >> /etc/sysconfig/network-scripts/ifcfg-eth0
else
sed -i '/IPADDR/d' /etc/sysconfig/network-scripts/ifcfg-eth0
echo "IPADDR=$ip " >>/etc/sysconfig/network-scripts/ifcfg-eth0
fi
View Code

 

10.将两个文件相同数值列出,并输出到文本内

echo "* * * 1-12 * sh /root/zuoye/ceshi.sh" > /var/spool/cron/root
diff file1 file2 | awk '{print $2}' | sed -e '/^$/d' > file
View Code

 

11.删除所有小于2M大于10M的以mp4.mp3的文件

a=`find / -type f -name "(*.mp3 && *.mp4)"`
for i in $a
do
b=`ls -l $i | awk '$5>2048 && $5<10240000 {print $9}'`
rm -rf $b
done
View Code

 

12.删除所有大于1M以上的mp4.mp3文件

a=`find / -type f   -name "*.mp3" -o -name "*.mp4" -a -size +1M`
for i in $a
do
rm -rf $i
done
View Code

 

13.查找所有有关192.168.20.0 网段的数据

#!/bin/sh
#查找所有有关192.168.20.网段的东西
find / -type f -exec grep -l "192.168.20.*"{} \;
View Code

 

14.查找大于10M的文件和目录

#!/bin/sh
#查找大于10M的文件和目录
ll -h /var/log | awk '$5>10 && $5<10240 {print $5,$9}' > file.txt
cat file.txt
View Code

 

15.ping网段内所有的主机存活

#!/bin/sh
#ping网段内所有主机存活
for i in {1..254}
do
if ping -w 1 -c 1 192.168.31.$i | grep "100%" >/dev/null
then
echo "192.168.31.$i  YES!!!"  
else
echo "192.168.31.$i  NO!!!" 
fi
done
View Code

 

16.nginx启动服务脚本

#!/bin/bash
#nginx的启动服务脚本
# chkconfig: - 85 15
# description: nginx is a World Wide Web server. It is used to serve
pid="/usr/local/nginx/logs/nginx.pid"
on="/usr/local/nginx/sbin/nginx"

start ()
{
if [ -f $pid ];then
echo "ON! qi dong cheng gong!!!"
else
echo "YES! cheng gong!!!"
#/bin/su - nginx -c "/usr/local/nginx/sbin/nginx $2 1>/dev/null 2>/dev/null"
/usr/local/nginx/sbin/nginx
fi
}

stop ()
{
if [ -f $pid ];then
echo "YES! yi guan bi!!!"
killall nginx
else
echo "NO! yi guan bi!!!"
fi
}

case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
exit)
exit
;;
*)
echo $"Usage: $0 (start,stop,restart)"
exit
;;
esac
exit
View Code

 

posted @ 2018-01-15 20:51  kevin.Xiang  阅读(425)  评论(0编辑  收藏  举报