!/bin/bash

判断172.40.51.0网段有多少是开关机状态
x=0
y=0
for i in `seq 254`
do
 ping -c1 -i0.1 -W1 172.40.51.$i
if [ $? -eq 0 ];then
  echo "172.40.51.$i is up"
  let x++
else
  echo "172.40.51.$i is down"
   let y++
fi  
done
echo "开机的有$x个,关机的有$y个。"

1加到100的和
tmp=0
for  i  in  {1..100} 
do
	 let tmp=$i+$tmp
done
echo "总和:$tmp"

随机猜奖
u=0
num=$[ RANDOM%100+1 ]
while :
do
 read -p "[1-100]随即猜:" ca
if [ $ca -eq $num ];then
  echo  “恭喜中奖”
  exit
elif [ $ca -gt $num ];then
   echo “猜大了”
else
  echo “猜小了”
fi
 let u++
done
echo "你猜了$u次"

阉割版if
case "$1" in
-n)
  touch $2;;
-e)
  vim  $2;;
-c)
  cat $2;;
-r)
 rm -rf $2;;
*)
  echo "usage:$0 (-n|-e|-c|-r) 文件名"
esac



加法计算机
tmp=0
while :
do
 read -p "请输入一个数[0结束]:" shu
 let tmp+=$shu
  [ $shu -eq 0 ] && break
done
echo "总和$tmp"

99乘法表
for i in {1..9}
do
 for j in `seq $i`
do
echo -n " $i*$j=$[i*j]"
done
echo -e
done

  棋盘
for i in {1..10}
do 
  for j in {1..10}
  do
    he=$[i+j]
 if [ $[$he%2] -eq 0 ];then
  echo -en "\033[45m  \033[0m"  
 else
  echo -en "\033[47m  \033[0m"
fi
  done
	echo -e 
  done


   用户创建
read -p "请创建用户:" user
  echo "必须创建用户"
if [ -z "$user" ];then
while :
  do
 read -p "请创建用户:" user
 [ $? -ne 0 ] && exit
[ -n "$user" ]&& break
done
fi
 stty -echo
read -p "请创建密码:" pass
 stty echo
pass=${pass:-123456}
useradd "$user"
echo "$pass" |passwd --stdin "$user"
  
	自动 远程操作
rm /root/.ssh/known_host
i=172.25.0.10
expect << EOF
set timeout 30
spawn  ssh  $i
expext yes       {send "yes\n"}
expect password  {send "redhat\n"}
expect #         {send "touch /qq\n"}
expect #         {send "ls\n"} 最后一条不执行
EOF

   
	 
i=`uptime |awk -F: '{print $5}'`
if [ $? -eq 0 ];then
  echo "CPU 1-15分钟的负载的值是:$i"
fi 
y=`ifconfig eth0 |awk '/RX packets/{print $6,$7}'`
if [ $? -eq 0 ];then
  ##echo "RX 接受的数据流量是:$y"
fi 
u=`ifconfig eth0 |awk '/TX packets/{print $6,$7}'`
if [ $? -eq 0 ];then
  echo "TX 发送的数据流量是$u"
fi 
p=`free |awk '/Mem:/{print $4}'`
if [ $? -eq 0 ];then
  echo " 内存剩余:"$p"K"
fi 
q=`df|awk '/\//{print $4}'`
if [ $? -eq 0 ];then
  echo "根分区的剩余容量:"$q"K"
fi 
w=`who |awk '{print $1}'`
if [ $? -eq 0 ];then
  echo "已登陆的用户:$w"
fi 
a=`ps aux |wc -l`
if [ $? -eq 0 ];then
  echo "进程数量:$a"
fi 
s=`rpm -qa |wc -l`
if [ $? -eq 0 ];then
  echo "已装包数量:$s"
fi 

     源码安装ngix
ls /  |awk   '/nginx.*.gz/'
if [ $? -eq 0 ];then
tar -xf nginx-1.8.0.tar.gz
else
 echo "没有nginx-1.8.0.tar.gz包"
 exit
fi
cd  nginx-1.8.0
yum clean>/dev/null
yum repolist
yum -y install gcc pcre-deve  openssl-devel>/dev/null
xi(){
while :
do
echo -ne '\033[42m \033[0m'
sleep 1
done
}
xi &
if [ $? -eq 0 ];then
./configure >/dev/null
fi
make>/dev/null
make install>/dev/null
cd ..
kill $!
i=`ls /usr/local/nginx/`
if [ $? -eq 0 ];then 
   echo OK
else
   echo 失败 
fi

  nginx的启动服务
case $1 in
start)
 /usr/local/nginx/sbin/nginx;;
stop)
  /usr/local/nginx/sbin/nginx -s stop;;
restart)
   /usr/local/nginx/sbin/nginx -s stop
   /usr/local/nginx/sbin/nginx;;
status)
  netstat -ntulp  |grep nginx;;
*)
    echo  "请正确输入";;
esac

       拒绝访问失败的ip     
awk '/Failed/{print $11}'  /var/log/secure >> ip.txt
if [ -d ip.txt  ];then
echo  ip.txt没有内容
else
u=`cat ip.txt`
for i in  $u
do
iptables  -I INPUT -s 172.25.0.$i -j DROP 
done
fi