jy166102 发表于 2018-8-19 11:23:32

shell 脚本基础

  !/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
  foriin{1..100}
  do
  let tmp=$i+$tmp
  done
  echo "总和:$tmp"
  随机猜奖
  u=0
  num=$[ RANDOM%100+1 ]
  while :
  do
  read -p "随即猜:" 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 "请输入一个数:" 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=$j]"
  done
  echo -e
  done
  棋盘
  for i in {1..10}
  do
  for j in {1..10}
  do
  he=$
  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 /dev/null
  yum repolist
  yum -y install gcc pcre-deveopenssl-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
  echoip.txt没有内容
  else
  u=cat ip.txt
  for i in$u
  do
  iptables-I INPUT -s 172.25.0.$i -j DROP
  done
  fi


页: [1]
查看完整版本: shell 脚本基础