zoukankan      html  css  js  c++  java
  • Shell常用脚本

    shell常用脚本

    [root@nginx ~]# basename /etc/sysconfig/network-scripts/ifcfg-eth0 
    ifcfg-eth0
    [root@nginx ~]# dirname /etc/sysconfig/network-scripts/ifcfg-eth0
    /etc/sysconfig/network-scripts
    
    [root@nginx ~]# cat process.sh
    #!/bin/bash
    echo "PID of this script: $$"              #($$)查看当前脚本的进程ID
    echo "PPID of this script: $PPID"          #($PPID)查看当前脚本父进程的进程ID
    echo "UID of this script: $UID"            #($UID)查看用户的UID
    echo $$ >/var/log/$(basename $0 .sh).pid   #basename $0 .sh:获取脚本名称和去除.sh文件后缀名
    echo "当前是第$LINENO"                     #$LINENO获取当前行号
    
    
    [root@web01 scripts]# cat ping.sh     #并发执行ping(快速执行加{ }&)
    #!/bin/bash 
    for n in {1..254};do
        {
        ping -c 1 10.0.0.$n &>/dev/null
        if [ $? -eq 0 ];then
            echo "10.0.0.$n" >>/tmp/cunhuo.log
        else
            echo "10.0.0.$n" >>/tmp/dead.log
        fi
    }&
    done
    wait      #wait等待前面的后台任务全部完成才往下执行
    echo "hello world"
    
    [root@web01 scripts]# cat iptables.sh
    #!/bin/bash
    . /etc/init.d/functions
    if [ $UID -ne 0 ];then
        echo "Please use root run scription!!!!!"
        exit 10
    fi
    while true
    do
        awk '{print $1}' /application/nginx/logs/access.log|sort -n -r|uniq -c >/tmp/ip.txt
        while read LINE
            do
                IP=${LINE#* }
                COUNT=${LINE%% *}
                ADD_IP=$(iptables -nL|egrep $IP|wc -l)
                IPTABLES(){
                    if [ "$COUNT" -gt 5 -a $ADD_IP -eq 0 ];then
                        iptables -I INPUT -s $IP -j DROP
                        action "$IP is dropped!!!!!!"  /bin/true
                    elif [ $COUNT -lt 5  ];then
                        iptables -D INPUT -s $IP -j DROP 2>/dev/null
                    fi
                }
                IPTABLES 
            done </tmp/ip.txt
        sleep 60
    done
    
    [root@db02 3306]# cat fenku.sh    #Mysql分库备份
    port=3306
    socket=/data/${port}/mysql.sock
    CmdPath="/application/mysql/bin"
    MysqlCmd="mysql -S $socket"
    MysqlDump="mysqldump -S $socket"
    DataPath=/data/backup
    [ -d $DataPath ] || mkdir $DataPath -p
    for dbname in `$CmdPath/$MysqlCmd -e "show databases;"|sed 1d|grep -v "_schema"`
    do
       $CmdPath/$MysqlDump  -B --master-data=2 $dbname|gzip >$DataPath/${dbname}_$(date +%F).sql.gz
    done
    
    [root@web01 scripts]# cat md5sum.sh
    #!/bin/sh
    array=(
    21029299
    00205d1c
    a3da1677
    1f6d12dd
    890684b
    )
    Path=/tmp/md5.txt
    funGetMd5() {
        > $Path
        for ((Num=0;Num<=32767;Num++))
        do     
           {
           Stat=$(echo $Num|md5sum)
           echo "$Stat $Num" >> $Path
           }&     
        done
    }
    funFindMd5() {
        word=$(echo "${array[@]}"|sed -r 's# |
    #|#g')
        grep -E "$word" $Path
    }
    funcMain(){
        funGetMd5
        funFindMd5
    }
    funcMain
    
    [root@web01 scripts]# cat arr3.sh 
    #!/bin/sh
    . /etc/init.d/functions
    array=(
    http://blog.oldboyedu.com
    http://blog.etiantian.org
    http://oldboy.blog.51cto.com
    http://10.0.0.7
    )
    function wait(){
        echo -n "Start Curl_check"
        for n in {1..3}
        do
            echo -n " ."
            sleep 1
            if [ $i -eq 4 ];then
                echo -e "	"
                action "Curl_check is start!!!!!!" /bin/true
            fi
        done
    }
    function check_url(){
        wget -o /dev/null --spider -T 10 --tries=1  $1 
        if [ $? -eq 0 ];then
            action "$1 is ok" /bin/true
        else
            action "$1 is no" /bin/false
        fi
    }
    function main(){
        wait
        for((i=0;i<${#array[*]};i++))
        do
            check_url ${array[i]}
            sleep 1
        done
    }
    main
    
    [root@nginx ~]# cat func.sh $1
    #!/bin/bash
    . /etc/init.d/functions
    function name() {
        if [ -z $1 ];then
            echo "Usg:sh $0 [ip/domain]"
            return 1
        else
        ping -c 3 $1 >/dev/null
            if [ $? -eq 0 ];then
                echo "up"
            else
                echo "down"
                return 2
            fi
        fi
    }
    result=$(name $1)   #取函数的运行结果,以及向函数里传参
    echo $?             #取函数的return值
    if [ "$result" == "down" ];then
        /sbin/ifdown eth1 >/dev/null
        action "eth1 is down" /bin/true
    elif [ "$result" == "up" ];then
        /sbin/ifup eth1 >/dev/null
        action "eth1 is up" /bin/true
    else
        echo $result
    fi
    
    [root@nginx ~]# cat menu.sh      #打印菜单脚本
    #!/bin/sh
    clear
    echo -e "
    
    			Scripts admin menu"
    echo -e "
    		++++++++++++++++++++++++++++
    "
    echo -e "		1.  Install Apche"
    echo -e "		2.  Install PHP"
    echo -e "		3.  Install Mysql"
    echo -e "
    		++++++++++++++++++++++++++++"
    echo -en "
    		  Please enter Install option: " 
    read -t 5 -n 1 NUM
    case $NUM in 
        1) 
        echo -e "
    
    Install Apple
    "
        ;;
        2) 
        echo -e "
    
    Install PHP
    "
        ;;
        3)
        echo -e "
    
    Install Mysql
    "
        ;;
            *)
            echo -e "
    
    	Usge: Enter 1|2|3
    "
    esac
    
    [root@nginx ~]# cat captrue.sh   #自动抓包脚本
    #!/bin/bash
    n=1
    while true;do
      tcpdumpid=`ps aux | grep tcpdump | awk '/^tcpdump/{print $2}'`
      curl 172.30.3.198:6011 &>/dev/null
      if [ $? -ne 0 ];then
        echo "$n `date +"%Y-%m-%d %T"` ---curl 172.30.3.198 false..." >>./status_error.log
        num=`ps aux | grep tcpdump| wc -l`
        if [ $num -eq 1 ];then
          tcpdump src host 172.30.4.152 and dst host 172.30.3.198 -w ./server_152.cap &
        fi
      else 
        kill $tcpdumpid >/dev/null 2>&1
        [ -f ./server_152.cap ] && mv ./server_152.cap ./server_$(date +%F-%T)_152.cap
        echo "$n `date +"%Y-%m-%d %T"` ---curl 172.30.3.198 ok..." >>./status.log
      fi
      ((n++))
      sleep 2
    done

    shell逐行读取文件

    方法一,指定换行符读取:
    #! /bin/bash 
    IFS="
    " 
    for LINE in `cat /etc/passwd` 
    do 
    echo $LINE 
    done
    方法二,文件重定向给read处理:
    #! /bin/bash 
    cat /etc/passwd | while read LINE
    do
    echo $LINE 
    done
    方法三,用read读取文件重定向:
    #! /bin/bash
    while read LINE
    do
    echo $LINE
    done < /etc/passwd 

    read   -p     接提示语句
              -t 3   超时设置(单位为秒)
              -n 3  允许输入的最大字符串个数,大于3个直接退出
              -s     隐藏输入的字符串

    while循环的特长是执行守护进程,以及实现我们希望循环不退出持续执行的应用,擅长用于频率小于1分钟循环处理,其他的while循环几乎都可以被for循环取代
    if语句、for语句最常用,其次while(守护进程),case(服务启动脚本)
    sleep默认单位秒(s),usleep默认单位微秒(μs)

    标准输入stdin,标准输出stdout和标准错误stderr,三个系统文件的文件描述符分别为0,1和2。所以2>&1的意思就是将标准错误也输出到标准输出当中

    Ubuntu中编写脚本时报错 let: not found,原因为在Ubuntu中/bin/sh链接默认指向的是dash shell,dash是一个小巧的shell,而dash shell不支持++运算和let命令
    解决办法:
         1、直接指明使用bash shell
         2、sudo dpkg-reconfigure dash 选择 "否", 表示用bash代替dash

    格式化时间
    [root@nginx ~]# date +%Y%m%d-%H:%M:%S
    [root@nginx ~]# date +%F" "%T

    参考链接:
           https://blog.51cto.com/cuimk/1347743   #iptables交互脚本

  • 相关阅读:
    PHP的pcntl进程控制之pcntl_wait
    初探PHP多进程
    php是单进程语言,但是也有办法支持多进程
    PHP中pack、unpack的详细用法
    php的几种运行模式
    PHP多进程用例--swoole和pcntl
    MySQL临时表
    centos-6.4 yum EPEL
    关于android上dpi/screen-size的厘清解释
    android studio在windows上设置git/ssh
  • 原文地址:https://www.cnblogs.com/xwupiaomiao/p/8037972.html
Copyright © 2011-2022 走看看