您的位置:首页 > 运维架构 > Shell

shell练习

2017-11-28 17:53 417 查看
1、
[root@centos7 shell]# cat test.sh
#!/bin/bash

var="http://www.aaa.com/root/123.htm"

#1.取出www.aaa.com/root/123.htm
echo $var|awk -F "//" '{print $2}'

#2.取出123.htm
echo $var|awk -F "/" '{print $5}'

#3.取出http://www.aaa.com/root
echo $var|awk -F "/123" '{print $1}'

#4.取出http:
echo $var|awk -F "://" '{print $1}'

#5.取出http://
echo $var|awk -F "www" '{print $1}'

#6.取出123
echo $var|awk -F "[/ .]+" '{print $6}'

#7.取出123.htm
echo $var|awk -F "[/ ]+" '{print $4}'

2、
我们使用的云主机,购买一块云盘后,默认并不是挂载状态的,用shell写一个脚本,只要把盘符和挂载点以参数的形式提供给脚本,该脚本就可以自动格式化、挂载。
要求:
1 不用分区,直接格式化
2 格式化为ext4文件系统类型

#!/bin/bash

echo "Useage $0 盘符 挂载点, 如: $0 /dev/xvdb /data"

if [ $# -ne 2 ]
then
exit
fi

if [ ! -b $1 ]
then
echo "你提供的盘符不正确,请检查后再操作"
exit 1
fi

mkfs -t ext4 $1

if [ ! -d $2 ] ;then
mkdir -p $2
fi

n=
egrep " $2 " /etc/fstab|wc -l

if [ $n -eq 0 ]
then
echo "$1 $2 ext4 defaults 0 0" >> /etc/fstab
mount -a
else
mount $1 $2
echo "配置文件/etc/fstab中已经存在挂载点$2,请检查一下."
fi

3、需求背景:
discuz论坛,每天有很多注册机注册的用户,然后发垃圾广告帖子。虽然使用了一些插件但没有效果。分析访问日志,发现有几个ip访问量特别大,所以想到可以写个shell脚本,通过分析访问日志,把访问量大的ip直接封掉。
但是这个脚本很有可能误伤,所以还需要考虑到自动解封这些ip。
思路:
1 可以每分钟分析1次访问日志,设定一个阈值,把访问量大的ip用iptables封掉80端口
2 每20分钟检测一次已经被封ip的请求数据包数量,设定阈值,把没有请求的或者请求量很小的解封

#! /bin/bash

log="/data/logs/www.aaa.com.log"
tmpdir="/tmp/badip"
#白名单ip,不应该被封
goodip="27.133.28.101"

[ -d $tmpdir ] || mkdir -p $tmpdir

t=`date -d "-1 min"  +%Y:%H:%M`

#截取一分钟以前的日志
grep "$t:" $log > $tmpdir/last_min.log

#把一分钟内日志条数大于120的标记为不正常的请求
awk '{print $1}' $tmpdir/last_min.log |sort -n |uniq -c |sort -n |tail |awk '$1>120 {print $2}'|grep -v "$good_ip"> $tmpdir/bad.ip

d3=`date +%M`

#每隔20分钟解封一次ip
if [ $d3 -eq "20" ] || [ $d3 -eq "40" ] || [ $d3 -eq "00" ]
then
/sbin/iptables -nvL INPUT|grep 'DROP' |awk '$1<10 {print $8}'>$tmpdir/good.ip
if [ -s $tmpdir/good.ip ]
then
for ip in `cat $tmpdir/good.ip`
do
/sbin/iptables -D INPUT -p tcp --dport 80 -s $ip -j DROP
d4=`date +%Y%m%d-%H:%M`
echo "$d4 $ip unblock" >>$tmpdir/unblock.ip
done
fi

#解封后,再把iptables的计数器清零
/sbin/iptables -Z INPUT
fi

if [ -s $tmpdir/bad.ip ]
then
for ip in `cat $tmpdir/bad.ip`
do
/sbin/iptables -A INPUT -p tcp --dport 80 -s $ip -j DROP
d4=`date +%Y%m%d-%H:%M`
echo "$d4 $ip block" >>$tmpdir/block.ip
done
fi

4、脚本安装samba服务
#!/bin/bash

is_samba_installed=
rpm -qa|grep samba|wc -l

if [ $is_samba_installed != 0 ]
then
echo "You had already installed Samba."
exit 0
fi

echo "It will install Samba."
sleep 1
cnfdir="/etc/samba/smb.conf"

chkok(){
if [ $? != 0 ]
then
echo "Error, Please try again."
exit 1
fi
}

yum install -y samba
chkok

sed -i 's/MYGROUP/WORKGROUP/' $cnfdir
sed -i 's/user/share/' $cnfdir
#在最后一行新增[fish]
sed -i '$a[fish]' $cnfdir
if [ -d $1 ]
then
cd $1
echo "test" > test.txt
sed -i '$a[fish]\n\tcomment = Share All\n\tpath = "'$1'"\n\tbrowseable = yes\n\tpublic = yes\n\twritable = no' $cnfdir
#类似:\n换行 \t tab键
#[fish]

comment = Share All

path = ""

browseable = yes

public = yes

writable = no

else
mkdir $1
cd $1
echo "test" > test.txt
sed -i '$a[fish]\n\tcomment = Share All\n\tpath = "'$1'"\n\tbrowseable = yes\n\tpublic = yes\n\twritable = no' $cnfdir
fi
/etc/init.d/smb start
chkok
echo "Please input [\sambaIP\sharename] to access the share dir."
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell 练习