经常使用shell 脚本

1 将程序备份

#!/bin/sh
	savepath=/root/tar/$(date +%F_%H%M)
	if [ ! -d  $savepath ]  # !与 -d 之间要加空格,与[] 之间要有空格
	then
	  mkdir -p $savepath
	fi

	tar -jPcf $savepath/96.tar /www/wwwroot/default/96weixin.ewm

2 将Ip地址加入黑名单

!/bin/sh
	file=/root/ip.txt
	for val in `cat $file`
	do
	  iptables -I INPUT -s$val -j DROP
	  echo $val
	done

    结果:linux

[root@iZryxshkbkz2x2Z ~]# iptables -nvL
	Chain INPUT (policy DROP 0 packets, 0 bytes)
	 pkts bytes target     prot opt in     out     source               destinatio
		0     0 DROP       all  --  *      *       66.55.44.33          0.0.0.0/0
		0     0 DROP       all  --  *      *       12.4.5.6             0.0.0.0/0
		0     0 DROP       all  --  *      *       66.55.44.33          0.0.0.0/0
		0     0 DROP       all  --  *      *       12.4.5.6             0.0.0.0/0
	  124  6000 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:888
	 1169 55324 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpts:39000:40000
	  752 36764 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           s

3 统计nginx 访问日记中的状态码

3-1 首先写出统计出状态:

[root@iZryxshkbkz2x2Z wwwlogs]# cat  access.log| grep  -io 'HTTP\/1.[10]"[[:bla
	nk:]][0-9]\{3\}' | awk '{print $2}'
	403
	404

3-2 脚本

#!/bin/sh
	filepath=/www/wwwlogs/access.log
	getStatus(){
	httpStatus=`cat $filepath | grep -io 'HTTP\/1.[10]"[[:blank:]][0-9]\{3\}' | awk '{   
	if($2>=100&&$2<200)
		{i++}
		else if($2>=200&&$2<300 )
		{j++}
		else if($2>=300&&$2<400)
		{m++}
		else if($2>=400&&$2<500)
		{n++}
		else if($2>=500)
		{p++}
		else
		{a++}
		}END{
		print i?i:0,
		j?j:0,
		m?m:0,
		n?n:0,
		p?p:0,
		a?a:0
	}' `

	echo -e "${httpStatus}"
	}
	getStatus

执行结果: 0 17 2 662 0 0nginx

4 linux 分析系统内存tcp

4-1 经过free -m 分析系统内存: free -m:rest

[root@iZryxshkbkz2x2Z default]# free -m
				 total       used       free     shared    buffers     cached
	Mem:           996        885        110        103         64        456
	-/+ buffers/cache:        365        630
	Swap:         1024         57        967



	[root@iZryxshkbkz2x2Z default]# free -m |sed -n '2p' | awk 'used=($3-$6-$7),rest=($6+$7+$4){print "总内存:" $2,"已用内存:" $3-$6-$7 ,"剩余内存:"$6+$7+$4}END{printf "使用百分比:%10d%\n",(used/$2)*100 }'
	总内存:996 已用内存:365 剩余内存:630
	使用百分比:        36%

4-2 经过 proc/meminfo 分析系统内存:code

[root@iZryxshkbkz2x2Z default]#  cat /proc/meminfo | awk '/MemTotal/{total=$2}/MemFree/{free=$2}/^Cached/{cache=$2}/Buffers/{buffers=$2}END{printf "系统已用内存:%10d\n",  (total-free-cache-buffers)/1024}'
	系统已用内存:       343

5 系统磁盘分析:

[root@iZryxshkbkz2x2Z ~]# df -h | grep -Ev 'Filesystem|tmpfs' | awk '{print "总磁盘空间:" $2, "已使用空间:" $3,"使用率:" $5}'
	总磁盘空间:40G 已使用空间:5.4G 使用率:15%

写的语句不太好,有空重写。ip

未完待续。。。。内存

相关文章
相关标签/搜索