bash每日一练,8个经典脚本示例
Posted 运维部落
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bash每日一练,8个经典脚本示例相关的知识,希望对你有一定的参考价值。
bash脚本示例
一、ping C类网地址
#!/bin/bash
read -p "C NETWORK:" MYNET
PINGNET=`echo $MYNET | sed 's/\([0-9.]*\)\ .[0-9]*/\1/g'`
let I=1
while [ $I -le 254 ];do
ping –c1 –W1 $PINGNET.$I &>/dev/null
[ $? -eq 0 ] && echo "$PINGNET.$I online." || echo "$PINGNET.$I offline."
let I++
done
二、ping B类网地址
#!/bin/bash
read -p "B network:" MYNET
PINGNET=`echo $MYNET | sed 's/\([0-9]\{1,3\}\.[0-9]\{1,3\}\)\..*/\1/g'`
for P in {0..255}; do
for I in {1..255}; do
if ping -c1 -W2 $PINGNET.$P.$I &>/dev/null; then
echo "$PINGNET.$P.$I is online."
else
echo "$PINGNET.$P.$I is offline."
fi
done
done
三、删除当前目录下大小为0的文件
#!/bin/bash
for filename in `ls`
do
if test -d $filename
then b=0
else
a=$(ls -l $filename | awk '{ print $5 }')
if test $a -eq 0
then rm $filename
fi
fi
done
四、实现grep -i的功能
/home/lee#cat Grep
#!/bin/bash
#
(($#!=2))||[[ ! -f $2 ]]&&{ echo "Usage:$(basename $0) <patten> filename"; exit 1; }
str=$(echo $1|od -b)
str=${str% *}
str=${str#* }
patten=$(
for i in $str;do
[[ $i == 14[0-9] ]] && echo -ne "\\0133\\0${i/4/0}\\0$i\\0135" ||\
echo -ne "\\0133\\0${i/0/4}\\0$i\\0135"
done
)
while read line;do
[[ $line == *$patten* ]] && echo $line
done<$2
五、生产八位的随机密码
#!/bin/bash
MATRIX="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
LENGTH="8"
while [ "${n:=1}" -le "$LENGTH" ]
do
PASS="$PASS${MATRIX:$(($RANDOM%${#MATRIX})):1}"
let n+=1
done
echo "$PASS" # ==> Or, redirect to a file, as desired.
exit 0
六、将网站访问超过阀值的IP地址加入黑名单
#! /bin/bash
# fuckgo.sh
# by platinum
MYIP="xxx.xxx.xxx.xxx"
ftp_who()
{
netstat -an|grep $MYIP':21 '|grep -v LISTEN|grep -v ESTABLISHED|awk '{print $5;}'|awk -F':' '{print $1;}'|sort|uniq -c|awk '{print $1"="$2;}'
}
http_who()
{
netstat -an|grep $MYIP':80 '|grep -v LISTEN|awk '{print $5;}'|awk -F':' '{print $1;}'|sort|uniq -c|awk '{print $1"="$2;}'
}
for _un in $(ftp_who)
do
IP=`echo $_un|gawk -F'=' '{print $2}'`
NUM=`echo $_un|awk -F'=' '{print $1}'`
if [ $NUM -gt 5 ] && [ -z "`iptables -vnL|grep $IP`" ]
then
iptables -I INPUT -s $IP -p tcp --dport 21 -m state --state NEW,RELATED,ESTABLISHED -j DROP
echo "`date` FTP $IP NUM: $NUM" >> /var/log/fuck.log
fi
done
for _un in $(http_who|grep -v $MYIP)
do
IP=`echo $_un|gawk -F'=' '{print $2}'`
NUM=`echo $_un|awk -F'=' '{print $1}'`
if [ $NUM -gt 20 ] && [ -z "`iptables -vnL|grep $IP`" ]
then
iptables -I INPUT -s $IP -p tcp --dport 80 -m state --state NEW,RELATED,ESTABLISHED -j DROP
echo "`date` WEB $IP NUM: $NUM" >> /var/log/fuck.log
fi
done
七、列出目录及目录下的文件
#!/bin/bash
search () {
for dir in `echo *`
do
if [ -d "$dir" ] ; then # ==> If it is a directory (-d)...
zz=0 # ==> Temp variable, keeping track of directory level.
while [ $zz != $1 ] # Keep track of inner nested loop.
do
echo -n "| " # ==> Display vertical connector symbol,
zz=`expr $zz + 1` # ==> Increment zz.
done
if [ -L "$dir" ] ; then # ==> If directory is a symbolic link...
echo "+---$dir" `ls -l $dir | sed 's/^.*'$dir' //'`
else
echo "+---$dir" # ==> Display horizontal connector symbol...
numdirs=`expr $numdirs + 1` # ==> Increment directory count.
if cd "$dir" ; then # ==> If can move to subdirectory...
search `expr $1 + 1` # with recursion ;-)
cd ..
fi
fi
fi
done
}
if [ $# != 0 ] ; then
cd $1 # move to indicated directory.
fi
echo "Initial directory = `pwd`"
numdirs=0
search 0
echo "Total directories = $numdirs"
exit 0
八、打印杨辉三角
#!/bin/bash
if (test -z $1) ;then
read -p "Input Max Int Lines:" MAX
else
MAX=$1
fi
for ((i=1;i<=MAX;i++))
do
for ((j=1;j<=i;j++))
do
f=$(($i-1))
g=$(($j-1))
if [ "$j" == 1 ];then
declare SUM_${i}_$j=1
else
declare A=$[SUM_${f}_$j]
declare B=$[SUM_${f}_$g]
declare SUM_${i}_$j=`expr $A + $B`
fi
echo -n $[SUM_${i}_$j]
echo -en " "
done
echo " "
done
号外号外:
以上是今天为大家带来的内容,如果有任何问题,大家也可以添加以下QQ群参与问题的讨论。
Ansible中文权威群:372011984(已满)
AWK&SED企业实战: 260039357
docker企业架构实践:491533668
Jumpserver交流群 :399218702
Ansible中文权威-2号群:486022616
关于我们:
以上是关于bash每日一练,8个经典脚本示例的主要内容,如果未能解决你的问题,请参考以下文章