分类 "shell脚本" 下的文章

#!/bin/bash
#ping.sh
#Auther:stone
#取起始IP
read -p "please input start IPadr:" startip
#判断输入的是否为IP地址
while :; do
echo $startip |grep -E '\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>' && break
      read -p "please input IPadr(192.168.1.1):" startip

done
#结束IP    
read -p "please input end   IPadr:" endip
#判断结束IP的格式是否正确
while :; do
echo $endip |grep -E '\<([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.\<([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>' && break 
      read -p "please input end IPadr(192.168.1.255)" endip
done

#分别将起始IP地址和结束IP地址的四段提取出来用于判断
head1=`echo $startip |cut -d . -f 1`
head2=`echo $startip |cut -d . -f 2`
head3=`echo $startip |cut -d . -f 3`
head4=`echo $startip |cut -d . -f 4`
tail1=`echo $endip |cut -d . -f 1`
tail2=`echo $endip |cut -d . -f 2`
tail3=`echo $endip |cut -d . -f 3`
tail4=`echo $endip |cut -d . -f 4`

#判断结束IP段是否大于起始IP段,有四种可能:
if [ $tail1 -lt $head1 ]; then
    echo -e "\033[31mend IPadr smaller than start IPadr.\033[0m"
    exit 10 
fi

if [ $tail1 -lt $head1 ] && [ $tail2 -lt $head2 ]; then
    echo -e "\033[31mend IPadr smaller than start IPadr.\033[0m"
    exit 10 
fi

if [ $tail1 -lt $head1 ] && [ $tail2 -lt $head2 ] && [ $tail3 -lt $head3 ]; then
    echo -e "\033[31mend IPadr smaller than start IPadr.\033[0m"
    exit 10 
fi

if [ $tail1 -lt $head1 ] && [ $tail2 -lt $head2 ]  && [ $tail3 -lt $head3 ] && [ $tail4 -lt $head4 ]; then
    echo -e "\033[31mend IPadr smaller than start IPadr.\033[0m"
    exit 10 
fi

#以下为ping命令循环
while :; do
  if [ $head1 -eq $tail1 ]; then
     if [ $head2 -eq $tail2 ]; then
        if [ $head3 -eq $tail3 ]; then
           for ((d=$head4;d<=$tail4;d++)); do
              ping -c 1 -W 1 $head1.$head2.$head3.$d &>/dev/null   
              [ $? == 0 ] && echo -e "\033[32m$head1.$head2.$head3.$d is up.\033[0m"
              [ $? == 1 ] && echo -e "\033[31m$head1.$head2.$head3.$d id down.\033[0m"
           done
                  exit 0
        else
           for ((d=$head4;d<=255;d++)); do
              ping -c 1 -W 1 $head1.$head2.$head3.$d &>/dev/null    
              [ $? == 0 ] && echo -e "\033[32m$head1.$head2.$head3.$d is up.\033[0m"
              [ $? == 1 ] && echo -e "\033[31m$head1.$head2.$head3.$d id down.\033[0m"
           done
           declare c=$head3
           let c++
           while (($tail3-$c>0)); do
              for ((d=1;d<=255;d++)); do
              ping -c 1 -W 1 $head1.$head2.$c.$d &>/dev/null    
              [ $? == 0 ] && echo -e "\033[32m$head1.$head2.$c.$d is up.\033[0m"
              [ $? == 1 ] && echo -e "\033[31m$head1.$head2.$c.$d id down.\033[0m"
              done
           done
           if [ `expr $tail3 - $c` -eq 0 ]; then
              for ((d=0;d<=$tail4;d++)); do
              ping -c 1 -W 1 $head1.$head2.$c.$d &>/dev/null    
              [ $? == 0 ] && echo -e "\033[32m$head1.$head2.$c.$d is up.\033[0m"
              [ $? == 1 ] && echo -e "\033[31m$head1.$head2.$c.$d id down.\033[0m"
              done
              exit 0
           fi
        fi
     fi
  fi
done

#!/bin/bash
#choise.sh
#Auther:stone

cat << EOF
d|D) show disk usages
m|M) show memory usages
s|S) show swap usages
*) quit
EOF

read -p "please input your choice:" CHOICE
while [ $CHOICE != 'quit' ]; do
   case $CHOICE in
    d|D)
       echo "disk usages:"
       df -h
       ;;
    m|M)
       echo "memory usages"
       free -m |grep 'Mem'
       ;;
    s|S)
       echo "swap usages"
       free -m |grep 'Swap'
       ;;
      *)
       echo "UNknow.."
       ;;
   esac
     read -p "please input again your choice:" CHOICE
done

#!/bin/bash
#archive
#lm
read -p "please input save filename:" filename
read -p "please input need archive file:" file
read -p "please choose archive or commpress(a:archive|gzip|bzip2|xz):" fmart
case $fmart in
   a|archive)
      tar -cf ${filename}.tar $file &>/dev/null
      ;;
   gzip)
      tar -zcf ${filename}.tar.gz $file &>/dev/null
      ;;
   bzip2)
      tar -jcf ${filename}.tar.bz2 $file &>/dev/null
      ;;
   xz) 
      tar -Jcf ${filename}.tar.xz $file &>/dev/null
      ;;
   *)
      echo "Unknow options."
esac

#!/bin/bash
#showlog.sh 
#lm

v=0
c=0
n=`who |wc -l`

for i in `seq 0 $#`;do
    if [ $1 ]; then
       case $1 in
         -v)
            v=1
            shift
            ;;
         -c)
            c=1
            shift
            ;;
  -h|--help)
            echo "Usage:`basename $0` -v | -c | -h"
            exit 0
            ;;
          *)
            echo "Usage:`basename $0` -v | -c | -h"
            exit 7
            ;;
       esac
    fi
done

if [ $c -eq 1 ]; then 
   [ $v -eq 1 ] && echo "They users:$n" && echo "They are:" && who
   [ $v -eq 0 ] && echo "They users:$n"
   exit 0
fi

#!/bin/bash
#test -add & -del & -h|--help & -v|--verbose  etc
#lm
v=0
add=0
del=0
for i in `seq 0 $#`; do
   if [ $1 ]; then
      case $1 in 
         -v|--verbose)
                     v=1
                     shift
                     ;;
                 -add)
                     add=1
                     addusers=$2
                     shift 2
                     ;;
                 -del)
                     del=1
                     delusers=$2
                     shift 2
                     ;;
            -h|--help)
                     echo "Usage:`basename $0` -v -add username1,username2..|-del username1,username2..|-h|--help"
                     exit 0
                     ;;
                    *)
                     echo "Usage:`basename $0` -v -add username1,username2..|-del username1,username2..|-h|--help"                    
                     exit 7
                     ;;
      esac
    else
            break
    fi
done

   echo "$v,$add,$del"

if [ $add -eq 1 ]; then
    for a in `echo $addusers |sed a@,@ @g`;do 
         if `id $a &>/dev/null`; then
             echo "$s is exists."
         else 
             [ $v -eq 1 ] && useradd $a
             [ $v -eq 0 ] && useradd $a &>/dev/null
             [ $v -eq 1 ] && echo $a |passwd --stdin $a 
             [ $v -eq 0 ] && echo $a |passwd --stdin $a &>/dev/null
         fi 
    done
              exit 0
fi

if [ $del -eq 1 ]; then
    for b in `echo $delusers |sed s@,@ @g`; do 
          if `id $b &>/dev/null`;then
              userdel -r $b
              [ $v -eq 1 ] && echo "$b is delete."
          else 
              echo "$b is not exists."
          fi
    done
               exit 0
fi

#!/bin/bash
#version:0.0.2
#Auther:stone
#Discription:add user and password.

if [ $UID -ne 0 ]; then
    echo -e "\033[31mOnly root exec!\033[0m"
fi

echo -n "please input needs users:"
read username
if [ -z "$username" ]; then
    echo "please input a arguments!"
else
    for i in $username; do
        if id $i &>/dev/null; then
            echo -e "\033[31m$i exists!\033[0m"
        else
            [ -z "$(echo $i |sed 's/[0-9]//g')" ] && echo -e "\033[31m$i is number!$i not create!\033[0m" && continue
            useradd $i &>/dev/null
            [ $? -ne 0 ] && echo -e "\033[31madd user fail\033[0m"
            echo "$i" |passwd --stdin $i &>/dev/null
            echo -e "\033[32m$i is ok!\033[0m"
        fi

    done
fi