{"id":37,"date":"2024-05-29T19:35:08","date_gmt":"2024-05-29T11:35:08","guid":{"rendered":"http:\/\/zhaoyanqi.cn\/?p=37"},"modified":"2024-06-14T20:48:21","modified_gmt":"2024-06-14T12:48:21","slug":"shell%e7%bb%83%e4%b9%a0%e5%91%bd%e4%bb%a41000%e8%a1%8c","status":"publish","type":"post","link":"https:\/\/zhaoyanqi.cn\/?p=37","title":{"rendered":"shell\u7ec3\u4e60\u547d\u4ee41000\u884c"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#! \/bin\/bash\n#\u731c\u5e74\u9f84 \u7248\u672c1\nage=87\nread -p 'num: ' n\n\nif &#91; $n -eq $age ]; then\n  echo 'you get it'\nelif &#91; $n -gt $age ]; then\n  echo 'too big'\nelif &#91; $n -lt $age]; then\n  ehco 'too small'\nfi\n\n#\u731c\u5e74\u9f84 \u7248\u672c2\nread -p \">>>\" num\n&#91;&#91; ! $num =~ ^&#91;0-9]+$ ]] &amp;&amp; echo \"enter numbers\" &amp;&amp; exit\nif &#91; $num -gt 18];then\n\techo \"too big\"\nelif &#91; $num -lt 18];then\n\techo \"too small\"\nelse\n\techo \"you got it\"\nfi\n\n#\u67e5\u6210\u7ee9 \u7248\u672c1\nread -p \"enter your score:\" score\n\nif &#91; $score -ge 90 ]; then\n\techo \"A\"\nelif &#91; $score -ge 80 -a $score -lt 90 ]; then\n\techo \"A-\"\nelif &#91; $score -ge 70 -a $score -lt 80 ]; then\n\techo \"B+\"\nelif &#91; $score -lt 70 ]; then\n\techo \"B\"\nfi\n\n#\u67e5\u6210\u7ee9 \u7248\u672c2\nread -p \"enter your score\" score\n&#91;&#91; ! $score =~ ^&#91;0-9]+$ ]] &amp;&amp; echo \"please enter number\" &amp;&amp; exit\nif &#91; $score -ge 90 ];then\n\techo \"A\"\nelif &#91; $score -ge 80 ];then\n\techo \"B\"\nelif &#91; $echo -ge 70 ];then\n\techo \"C\"\nelse \n\techo \"d\"\nfi\n\n#\u5224\u65ad\u662f\u5426\u662f\u6570\u5b57\nread -p \"please enter a number:\" num\n\nwhile:\ndo\n\tif &#91;&#91; $num =~ ^&#91;0-9]+$ ]];then\n\t\tbreak\n\telse\n\t\tread -p \"not a number, pleas enter a number:\" num\n\tfi\ndone\necho \"the number you enter is $num\"\n\n#\u7f16\u5199\u4e00\u4e2a\u811a\u672c\uff0c\u547d\u4ee4\u884c\u4f20\u5165\u4e00\u4e2a\u6587\u4ef6\u8def\u5f84\uff0c\u5224\u65ad\u6587\u4ef6\u7684\u7c7b\u578b\nif &#91; -d $1 ]; then #\u76f8\u5f53\u4e8e\u7528test\u6d4b\u8bd5\u8fd0\u7b97\u7b26 \u201ctest -d \/\u76ee\u5f55\u201d \u547d\u4ee4 \n\techo \"$1 is directory\"\nelif &#91; -b $1 ];then\n\techo \"$1 is block\"\nelif &#91; -f $1 ];then\n\techo \"$1 is regular file\"\nelse\n\techo \"unknown\"\nfi\n\n#\u68c0\u6d4b\u5236\u5b9a\u7684\u4e3b\u673a\u662f\u5426\u53ef\u4ee5ping\u901a\uff0c\u5fc5\u987b\u4f7f\u7528$1\u53d8\u91cf\nping -c2 $1 &amp;> \/dev\/null\nif &#91; $? -eq 0 ];then\n\techo \"ok\"\nelse\n\techo \"down\"\nfi\n\n#\u5224\u65ad\u4e00\u4e2a\u7528\u6237\u662f\u5426\u5b58\u5728\nid $1 &amp;> \/dev\/null\nif &#91; $? -eq 0 ];then\n\techo \"user $1 exists\"\nelse\n\techo \"user $1 not exists\"\nfi\n\n#\u68c0\u6d4bhttpd\nrpm -q httpd &amp;>\/dev\/null\nif &#91; $? -eq 0 ];then\n\techo \"already installed\"\nelse\n\techo \"intalling now...\"\n\tyum install httpd -y &amp;>\/dev\/null\nfi\n\n#\u5224\u65ad80\u7aef\u53e3\u7684\u72b6\u6001\uff0c\u672a\u5f00\u542f\u5219\u91cd\u542f\nnetstat -an | grep LISTEN | grep '\\b80\\b' &amp;>\/dev\/null\nif &#91; $? -eq 0 ];then\n\techo \"port 80 is ok\"\nelse\n\techo \"port 80 is down\"\n\techo \"restart now...\"\n\tsystemctl restart httpd &amp;> \/dev\/null\n\tif &#91; $? -eq 0 ];then\n\t\techo \"restart successful\"\n\telse\n\t\techo \"restart failed\"\n\tfi\nfi\n\n#\u7f16\u5199\u76d1\u63a7\u811a\u672c\uff0c\u5982\u679c\u6839\u5206\u533a\u5269\u4f59\u7a7a\u95f4\u5c0f\u4e8e10%\uff0c\u5185\u5b58\u53ef\u7528\u7a7a\u95f4\u5c0f\u670830%\uff0c\u5c31\u5411\u7ba1\u7406\u5458\u53d1\u9001\u8b66\u544a\u90ae\u4ef6\uff0c\u90ae\u4ef6\u5185\u5bb9\u5305\u542b\u76f8\u5173\u4fe1\u606f\n#\u63d0\u53d6\u6839\u5206\u533a\u5269\u4f59\u7a7a\u95f4\uff1a\nuse_disk = `df \/| grep \/| awk '{print $5}'`\nuse_percent=`echo $use_disk|cut -d% -f1`\n#\u63d0\u53d6\u5185\u5b58\u5269\u4f59\u7a7a\u95f4\uff1a\navail_mem=`free | awk 'NR==2{print $NF}'`\ntotal_mem=`free | awk 'NR==2{print $2}'`\navail_percent=`echo \"scale=2;$avail_mem\/$total_mem\"|bc |cut -d. -f2`\n#\u6ce8\u610f\u78c1\u76d8\u63d0\u53d6\u7684\u6570\u503c\u7684\u90a3\u4f4d\u662fkb\uff0c\u5185\u5b58\u63d0\u53d6\u7684\u7684\u90a3\u4f4d\u662fmb\nif &#91; $use_percent -gt 90 ];then\n\techo \"mail content: root part usage is ${user_disk}, lower than 10%, please handle it!!!\"\nfi\nif &#91; $avail_percent -lt 30 ];then\n\techo \"mail content: avail_mem is ${free_percent}%, lower than 30%\"\nfi\n\n\n#\u6839\u636e\u64cd\u4f5c\u7cfb\u7edf\u4e0d\u540c\u8fdb\u884cyum\u6e90\u4f18\u5316centos6\uff0ccentos7\uff0ccentos8\nmv \/etc\/yum.repos.d\/CentOS-Bsae.repo \/etc\/yum.repos.d\/CentOS-Base.repo.backup &amp;>\/dev\/null\nvar=$(awk '{print $(NF-1)}' \/etc\/redhat-release) #\u62ff\u5230\u5f53\u524d\u7cfb\u7edf\u7684\u7248\u672c\u53f7\nos_version=`echo ${var%%.*}` #\u4ece\u540e\u9762\u5f00\u59cb\u5220\uff0c\u628a\u70b9\u540e\u9762\u7684\u5185\u5bb9\u5168\u5220\u9664\uff0c\u800c\u4e14\u662f\u8d2a\u5a6a\u5339\u914d\u6240\u4ee5\u5220\u5230\u6700\u524d\u9762\u90a3\u4e2a\u70b9\nif &#91; $os_version -eq 7 ];then\n\twget -O \/etc\/yum.repos.d\/CentOS-Base.repo http:\/\/mirrors.aliyun.com\/repo\/Centos-7.repo &amp;>\/dev\/null\nelif &#91; $os_version -eq 6 ];then\n\twget -O \/etc\/yum.repos.d\/CentOS-Base.repo http:\/\/mirrors.aliyun.com\/repo\/Centos-6.repo &amp;>\/dev\/null\nelif &#91; $os_version -eq 8 ];then\n\twget -O \/etc\/yum.repos.d\/CentOS-Base.repo http:\/\/mirrors.aliyun.com\/repo\/Centos-8.repo &amp;>\/dev\/null\nelse\n\techo \"please check your system information\"\nfi\n\n\n#case\u6848\u4f8b1\nread -p \"username:\" -t 5 username\necho\nif &#91; -z $username ];then #-z\u8868\u793a\u4e3a\u7a7a\n\tusername=\"default\"\nfi\n\ncase $username in\nroot)\n\techo \"administrator\"\n\t;;\negon)\n\techo \"common user\"\n\t;;\ndefault)\n\techo \"default user\"\n\t;;\n*)\n\techo \"other users\"\nesac\n\n#case \u6848\u4f8b2\nread -p \">>>: \" BOOLEAN\ncase $BOOLEAN in\n\t&#91;yY]&#91;eE]&#91;sS])\n\techo '11111111Thanks' $BOOLEAN\n\t;;\n\t&#91;yY]|&#91;nN])\n\techo '222222222Thanks' $BOOLEAN\n\t;;\n\t'T'|'F')\n\techo '33333333333Thans' $BOOLEAN\n\t;;\n\t&#91;Tt]rue|&#91;Ff]alse)\n\techo '444444444Thanks' $BOOLEAN\n\t;;\n\t*)\n\texit 1\n\t;;\nesac\n\n#\u7f16\u5199nginx\u542f\u52a8\u811a\u672c\n\n. \/etc\/init.d\/functions\nif &#91; $# -ne 1 ];then # $#\u7684\u7528\u6cd5\u662f\u68c0\u67e5\u4f20\u7ed9\u8fd9\u4e2a\u811a\u672c\u7684\u53c2\u6570\u4e2a\u6570\uff0c\u4e2a\u6570\u4e0d\u7b49\u4e00\u7684\u65f6\u5019\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\n\techo \"USAGE $0 {start|stop|restart}\" #\u6253\u5370\u4f7f\u7528\u65b9\u6cd5\n\texit 1\nfi\n\nif &#91; \"$1\" == \"start\" ];then\n\taction \"start nginx\" \/bin\/true\nelif &#91; \"$1\" == \"stop\" ];then\n\taction \"stop nginx\" \/bin\/true\nelif &#91; \"$1\" == \"restart\" ];then\n\taction \"restart nginx\" \/bin\/true\nelse\n\techo \"USAGE $0 {start|stop|restart}\"\n\texit1\nfi\n\n#\u7f16\u5199nginx\u542f\u52a8\u811a\u672c2\nnetstat -lntup|grep \":80\\b\"\nargs=$1\n\nfun(){\n\t&#91; $? -eq 0 ] &amp;&amp; action \"Nginx $args is \" \/bin\/true || echo \"Nginx $args is \" \/bin\/false\n}\n\ncase $1 in\n\tstart)\n\t\tnetstat -an | grep -i Listen | grep -q \"\\b80\\b\" #-i\u5ffd\u7565\u5927\u5c0f\u5199\uff0c-q\u524d\u53f0\u4e0d\u663e\u793a\n\t\tif &#91; $? -eq 0 ]\n\t\tthen\n\t\t\techo \"Nginx is running ...\"\n\t\telse\n\t\t\t\/usr\/sbin\/nginx\n\t\t\tfun\n\t\tfi\n\t\t;;\n\tstop)\n\t\t\/usr\/sbin\/nginx -s stop\n\t\tfun\n\t\t;;\n\treload)\n\t\t\/usr\/sbin\/nginx -s reload\n\t\tfun\n\t\t;;\n\trestart)\n\t\tnetstat -lntup|grep \":80\\b\" &amp;>\/dev\/null\n\t\tif &#91; $? -ne 0 ]\n\t\tthen\n\t\t\t\/usr\/sbin\/nginx\n\t\t\t&#91; $? -eq 0 ] &amp;&amp; echo \"Nginx start is ok\" || echo \"Nginx start is failed\"\n\t\telse\n\t\t\t\/usr\/sbin\/nginx -s stop\n\t\t\t\n\t\t\t&#91; $? -eq 0 ] &amp;&amp; echo \"Nginx stop is ok\" || echo \"Nginx stop is failed\"\n\t\t\tsleep 2\n\t\t\t\/usr\/sbin\/nginx\n\t\t\tfun\n\t\tfi\n\t\t;;\n\tstatus)\n\t\tnetstat -lntup|grep \":80\\b\" &amp;>\/dev\/null\n\t\tif &#91; $? -eq 0 ]\n\t\tthen\n\t\t\techo \"Nginx is running ...\"\n\t\telse\n\t\t\techo \"Nginx is not running ...\"\n\t\tfi\n\t\t;;\n\t*)\n\t\techo \"Usage: $0 {start|stop|status|restart|reload}\"\n\t\texit 2\nesac\n\n#\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u8df3\u677f\u673a\u811a\u672c\ncat&lt;&lt;EOF\n1. BACKUP 10.0.0.41\n2. WEB02 192.168.12.21\n3. WEB03 10.0.0.9\nEOF\ntrap \"echo \u4e0d\u8981\u4e71\u6309\u952e\u76d8\uff0c\u5426\u5219\u670d\u52a1\u5668\u5c06\u4f1a\u7206\u70b8\" HUP INT TSTP\n\nwhile true\ndo\n\tread -p \"\u8bf7\u8f93\u5165\u8fde\u63a5\u4e3b\u673a\u7f16\u53f7\u4fe1\u606f\uff1a\" num\n\tread -p \"\u8bf7\u8f93\u5165\u8d26\u53f7\uff1a\" user\n\t# read -p \"\u8bf7\u8f93\u5165\u8981\u6267\u884c\u7684\u547d\u4ee4\uff1a\" cmd\n\tcase $num in\n\t\t1)\n\t\t\tssh $user@10.0.0.41\n\t\t\t&#91; $? -ne 0 ] &amp;&amp; echo \"connect faild\"\n\t\t\t;;\n\t\t2)\n\t\t\tssh $user@192.168.12.21\n\t\t\t&#91; $? -ne 0 ] &amp;&amp; echo \"connect faild\"\n\t\t\t;;\n\t\t*)\n\t\t\techo \"\u8bf7\u8f93\u5165\u94fe\u63a5\u4e3b\u673a\u7684\u4fe1\u606f\"\n\tesac\ndone\n\n#\u7f16\u5199\u811a\u672c,\u83b7\u53d6\u7edf\u8ba1\u5f53\u65e5\u8bbf\u95ee\u6392\u540d\u524d\u5341\u7684ip\u5730\u5740\uff0c\u7136\u540e\u52a0\u5165iptables\u7981\u7528\u3002\u4e0a\u8ff0\u811a\u672c\u7ed3\u5408\u8ba1\u5212\u4efb\u52a1\uff0c \u6bcf3\u5206\u949f\u6267\u884c\u4e00\u6b21\n#\u65e5\u5fd7\u6587\u4ef6\u8def\u5f84\nLOG_FILE=\"\/var\/log\/nginx\/access.log\"\n\n#\u83b7\u53d6\u5f53\u65e5\u65e5\u671f\nTODAY=$(date +\"%d\/%b\/%Y\")\n\n#\u4f7f\u7528awk\u63d0\u53d6\u5f53\u65e5\u65e5\u5fd7\u5e76\u7edf\u8ba1IP\uff0c\u9009\u51fa\u8bbf\u95ee\u6b21\u6570\u524d10\u7684ip,$4 ~ today\u5c31\u662f\u5339\u914d\u4e00\u884c\u7684\u7b2c\u56db\u4e2a\u5b57\u6bb5\u662f\u4e0d\u662ftoday\nIPS=$(awk -v today=\"$TODAY\" '$4 ~ today {print $1}' $LOG_FILE | sort | uniq -c |sort -nr | head -n 10 | awk '{print $2}')\n\n#\u5c06\u6392\u540d\u524d10\u7684IP\u52a0\u5165iptabels\u7981\u7528\nfor ip in $IPS;\ndo\n\tiptables -A INPUT -s $ip -j DROP\n\techo \"$ip has been blocked.\"\ndone\n\n*\/3 * * * * \/path\/script.sh\n\n\n#\u8df3\u677f\u673a\narr=(&#91;1]:\"1.1.1.1\" &#91;2]:\"2.2.2.2\" &#91;3]:\"3.3.3.3\" &#91;4]:\"192.168.1.111\")\n\nwhile true\ndo\ncat&lt;&lt;EOF\n1 backup 1.1.1.1\n2 web01 2.2.2.2\n3 web02 3.3.3.3\n4 db01 192.168.1.111\n5 quit\nEOF\n\n\tread -p \"input the num of host\" num\n\tcase $num in\n\t\t1)\n\t\t\techo \"${arr&#91;$num]}\"\n\t\t2)\n\t\t\techo \"${arr&#91;$num]}\"\n\t\t3)\n\t\t\techo \"${arr&#91;$num]}\"\n\t\t4)\n\t\t\tssh root@\"${arr&#91;$num]}\"\n\t\t5)\n\t\t\tbreak\n\t\t*)\n\t\t\techo \"please enter correct number\"\n\tesac\n\t\t\t\ndone\n\n#\u7528while\u5faa\u73af\u548ccase\u6765\u5236\u4f5c\u4e00\u4e2a\u83dc\u5355\necho \"script name: ${ basename $0}\" #Basenam \u53d6\u8fd0\u884c\u7684\u547d\u4ee4\u540d\u79f0\uff0c\u4e0d\u5305\u542b\u76ee\u5f55\necho \"version 1.0\"\necho `date +%F_%H:%M:%S`\necho \"Author:egon\"\nwhile read -p \"(h for help):\" var\ndo\n\tcase $var in\n\t\tcpu|CPU)\n\t\t\techo -e \"\\n\\n\" #-e\u9009\u9879\u662f\u53ef\u4ee5\u89e3\u91ca\\n\u8fd9\u4e9b\u8f6c\u4e49\u5b57\u7b26\n\t\t\tgrep 'model name\\|cpu MHz\\| processor' \/proc\/cpuinfo |sort |uniq\n\t\t\techo -e \"\\n\\n\"\n\t\t;;\n\t\tmem|MEM)\n\t\t\techo -e \"\\n\\n\"\n\t\t\tfree\n\t\t\techo -e \"\\n\\n\"\n\t\t\t;;\n\t\tdisk|DISK)\n\t\t\techo -e \"\\n\\n\"\n\t\t\tdf -Th\n\t\t\techo -e \"\\n\\n\"\n\t\t\t;;\n\t\thelp|HELP)\n\t\t\techo -e \"\\n\\tcommand\\taction\\n\\n\"\n\t\t\tfor i in cpu mem disk\n\t\t\tdo\n\t\t\t\techo -e \"\\t$i\\t${i}_info\"\n\t\t\tdone\n\t\t\techo -e \"\\thelp\\tthis help page...\"\n\t\t\techo -e \"\\tquit\\texit !!..\"\n\t\t\techo -e \"\\n\\n\"\n\t\t\t;;\n\t\tq|quit|exit)\n\t\t\texit\n\t\t\t;;\n\t\t*)\n\t\t\techo -e \"\\n$var Enter Error...\\n\"\n\tesac\ndone\n\t\t\t\n#\u7f16\u5199\u76d1\u63a7\u811a\u672c\uff0c\u5982\u679c\u6839\u5206\u533a\u5269\u4f59\u7a7a\u95f4\u5c0f\u4e8e10%\uff0c\u5185\u5b58\u53ef\u7528\u7a7a\u95f4\u5c0f\u670830%\uff0c\u5c31\u5411\u7ba1\u7406\u5458\u53d1\u9001\u8b66\u544a\u90ae\u4ef6\uff0c\u90ae\u4ef6\u5185\u5bb9\u5305\u542b\u76f8\u5173\u4fe1\u606f\n\nuse_disk =`df \/|grep \/ | awk '{print $5}'`\nuse_percent=`echo $use_disk|cut -d% -f1`\n\navail_mem=`free|awk 'NR==2{print $NF}'`\ntotal_mem=`free|awk 'NR==2{print $2}'`\navail_percent=`echo \"scale=2;$avail_mem\/$total_mem\"|bc|cut -d. f2`\n\nif &#91; $use_percent -gt 90 ];then\n\techo \"\u6839\u5206\u533a\u5df2\u4f7f\u7528${use_disk}\u4f4e\u4e8e10%\uff01\uff01\"\nfi\n\nif &#91; $avail_percent -lt 30 ];then\n\techo \"\u5185\u5b58\u5269\u4f59${avail_percent}%,\u4f4e\u4e8e30%\"\nfi\n\n#\u5c06\u5f53\u65e5\u8bbf\u95ee\u6b21\u6570\u8d85\u8fc7800\u6b21\u7684ip\u7981\u6389\uff0c\u7ed3\u5408\u8ba1\u5212\u4efb\u52a13\u5206\u949f\u6267\u884c\u4e00\u6b21\n\t*\/3 * * * * \/scripts\/ban_ip.sh\n\t\n\tcurrent_day_count_ip=$(\n\t\tcat \/var\/log\/nginx\/access.log |grep `LANG=\"en_US.UTF-8\" &amp;&amp; date +\"%d\/%b\/%Y\"`|awk '{print $1}' | sort|uniq-c|sort -rn|awk '{print $1\":\"$2}'\n\t\t)\n\tfor info in $current_day_count_ip;\n\tdo\n\t\tcount=`echo $info |cut -d:-f1`\n\t\tif &#91; $count -lt 800 ];then\n\t\t\tcontinue\n\t\tfi\n\t\tIP=`echo $info|cut -d: -f2`\n\t\t\n\t\t\/sbin\/iptables -t filter -L -n|grep $IP  # \u5728\u9632\u706b\u5899\u8868\u4e0a\u641c\u7d22\u6709\u6ca1\u6709\u8fd9\u6761\uff0c\u5982\u679c\u6709\u7684\u8bdd\u63d0\u793a\u5df2\u7ecf\u6709\u4e86\n\t\tif &#91; $? -eq 0 ];then\n\t\t\techo \"IP:$IP has been band, need not do it again\" >> \/tmp\/banip.log\n\t\t\tcontinue\n\t\tfi\n\t\t\n\t\techo \"$(date '+%Y-%m-%d_%H:%M:%S') IP:$IP is over $count,BAN IT\" >> \/tmp\/banip.log \n\t\t\n\t\t\/sbin\/iptables -I INPUT -s $IP -j DROP\t\n\tdone\n\t\n#\u68c0\u6d4bip\u811a\u672c\uff0cping.sh 1.1.1.11 \u53ef\u4ee5\u8fd4\u56de\u6210\u529f\u6216\u8005\u5931\u8d25\u4e24\u4e2a\u7ed3\u679c\nping -c2 1.1.1.11 &amp;> \/dev\/null\nif &#91; $? -eq 0 ];then\n\techo \"ok\"\nelse\n\techo \"failed\"\nfi\n\n#\u5e76\u53d1\u68c0\u6d4b\u67d0\u4e2a\u7f51\u6bb5\u6240\u6709ip\nfor i in {1..254}\n\ndo\n(\n\tip_add=192.168.1.$i\n\tping -c1 $ip_add &amp;> \/dev\/null\n\tif &#91; $? -eq 0 ];then\n\t\techo 'ok'\n\telse\n\t\techo 'failed'\n\tfi\n)&amp;\ndone\n\n#\u7f16\u5199\u811a\u672c\uff0c\u53ef\u4ee5\u8ba1\u7b97\u4efb\u610f\u4e24\u4e2a\u6570\u7684+\u3001-\u3001*\u3001\/\u7684\u7ed3\u679c\uff0c\u8981\u6c42\u8f93\u5165\u7684\u4f4d\u7f6e\u53c2\u6570\u5fc5\u987b\u4e3a2\u4e2a\n\na=$1\nb=$2\n\nif &#91; $# -ne 2]; then\n\techo \"you need input 2 numbers\"\n\texit\nfi\n\necho \"a-b=$((a-b))\"\necho \"a+b=$((a+b))\"\necho \"a*b=$((a*b))\"\necho \"a\/b=$((a\/b))\"\n\n#\u7f16\u5199\u811a\u672c sum.sh 111 222 333 444 \u4f20\u5165\u4efb\u610f\u4e2a\u6570\u7684\u6574\u6570\uff0c\u811a\u672c\u90fd\u80fd\u6c42\u548c\nres=0\nfor i in $@\ndo\n\t((res+=i))\ndone\n\necho $res\n\n#\u7528while\u5faa\u73af\u548cfor\u5faa\u73af\u4e24\u79cd\u65b9\u5f0fprint1\u52305\uff0c\u8df3\u8fc73\n\ni=1\nwhile &#91;$i -eq 5 ]\ndo\n\tif &#91;$i -eq 3 ];then\n\t\t((i++))\n\t\tcontinue\n\tfi\n\n\techo $i\n\t(i++)\ndone\n\nfor i in {1..5}\ndo\n\tif &#91;$i -eq 3 ];then\n\t\t((i++))\n\t\tcontinue\n\tfi\n\n\techo $i\n\t(i++)\t\ndone\n\n#\u7f16\u5199\u4e00\u4e2a\u8df3\u677f\u673a\uff0c\u628a\u914d\u7f6e\u4fe1\u606f\u5b58\u5165\u914d\u7f6e\u6587\u4ef6\u3002\u811a\u672c\u6267\u884c\u7684\u65f6\u5019\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u6807\u53f7\u767b\u5f55\u5230\u76ee\u6807\u4e3b\u673a\n#jump.conf\n\t0\u3001BACKUP 1.1.1.11\n\t1\u3001WEB01  1.1.1.12\n\t2\u3001WEB02  1.1.1.14\n\t3\u3001DB01   192.168.71.4\n\t4\u3001quit\n\t\n#jump.sh\nip_array=(`cat path\/jump.conf|awk '{print $NF}'`)\n\nwhile true\ndo\n\tcat path\/jump.conf\n\tread -p \"please input the NO.\" num\n\tcase $num in\n\t\t0)\n\t\t\tssh root@${ip_array&#91;$num]}\n\t\t\t;;\n\t\t1)\n\t\t\tssh root@${ip_array&#91;$num]}\n\t\t\t;;\n\t\t2)\n\t\t\tssh root@${ip_array&#91;$num]}\n\t\t\t;;\n\t\t3)\n\t\t\tssh root@${ip_array&#91;$num]}\n\t\t\t;;\n\t\t4)\n\t\t\tbreak\n\t\t\t;;\n\t\t*)\n\t\t\techo \"please input a correct number!\"\n\tesac\ndone\n\necho \"\u8df3\u677f\u673a end\"\n\n#\u5199\u51fa\u6210\u7ee9\u7b49\u7ea7\u67e5\u8be2\u811a\u672c\uff0c\u5927\u4e8e90\u4f18\u79c0\uff0c80-90\u826f\u597d\uff0c70-80\u4e00\u822c\uff0c\u5176\u4ed6\u4e0d\u53ca\u683c\nread -p \"Please input your score:\" score\n\nif &#91;&#91; \"$score\" =~^&#91;0-9]+$ ]];then\n\tif \n\t\tif &#91; $score -ge 90 ];then\n\t\t\techo \"\u4f18\u79c0\"\n\t\telif &#91; $score -ge 80];then\n\t\t\techo \"\u826f\u597d\"\n\t\telif &#91; $score -ge 70];then\n\t\t\techo \"\u4e00\u822c\"\n\t\telse\n\t\t\techo \"\u4e0d\u53ca\u683c\"\n\t\tfi\nelse\n\techo \"your score should be a num!\"\n\nfi\n\n#\u7f16\u5199\u767b\u5f55\u8ba4\u8bc1\u7a0b\u5e8f\uff0c\u8f93\u9519\u4e09\u6b21\u5bc6\u7801\u4f1a\u9000\u51fa\nuasename=\"egon\"\npassword=\"123\"\n\nwrong_num=0\nwhile true\ndo\n\tread -p \"input your username: \" inp_username\n\tread -p \"input your password: \" inp_password\n\t\n\tif &#91; \"$inp_username\" = \"$username\" -a \"$inp_password\" = \"$password\"];then\n\t\techo \"login successful\"\n\t\tbreak\n\telse\n\t\techo \"wrong username or password\"\n\t\tlet fail_num++\n\tfi\n\t\n\tif &#91; $fail_num -eq 3];then\n\t\techo \"input too many wrong username or password, program will quit now!\"\n\t\tbreak\n\tfi\ndone\n\n\n#\u7f16\u5199\u68c0\u6d4burl\u8054\u901a\u6027\u7684\u7a0b\u5e8f\uff0curl\u8bbf\u95ee\u4e09\u6b21\u5931\u8d25\u5219\u5224\u5b9a\u4e3a\u5931\u8d25\uff0c\u8bbf\u95ee\u6210\u529f\u4e00\u6b21\u5c31\u5224\u5b9a\u6210\u529f\nif &#91; $# -ne 1];then\n\techo \"usage: $0 '+' url\"\n\texit\nfi\n\nfail_num=0\nwhile true\ndo\n\tcode=`curl -l -s $1 | awk 'NR==1{print $2}'`\n\tif &#91; \"$code\" = \"200\" ];then\n\t\techo \"$1 is ok\"\n\t\tbreak\n\telse\n\t\tlet fail_num++\n\t\techo \"fail_num: $fail_num\"\n\tfi\n\t\n\tif &#91;$fail_num -eq 3];then\n\t\techo \"$1 is error\"\n\t\tbreak\n\tfi\n\ndone\n\n#\u7f16\u5199\u811a\u672c\uff0c\u547d\u4ee4\u884c\u4f20\u5165\u4e00\u4e2a\u76ee\u5f55\uff0c\u7edf\u8ba1\u51fa\u8fd9\u4e2a\u76ee\u5f55\u4e0b\u76ee\u5f55\u6587\u4ef6\uff0c\u666e\u901a\u6587\u4ef6\uff0c\u8f6f\u8fde\u63a5\u6587\u4ef6\uff0c\u5176\u5b83\u6587\u4ef6\u7684\u4e2a\u6570\n\ncheck_dir=$1\n\nd=0\nf=0\nl=0\no=0\n\nfor filename in `ls $check_dir`\ndo\n\tfile_path=\"$check_dir\/$filename\"\n\tif &#91; -d $file_path];then\n\t\tlet d++\n\telif &#91; -f $file_path];then\n\t\tlet f++\n\telif &#91; -L $file_path];then\n\t\tlet l++\n\telse\n\t\tlet o++\n\tfi\ndone\n\n\techo \"\u76ee\u5f55\u6587\u4ef6\u4e2a\u6570\uff1a$d\"\n\techo \"\u666e\u901a\u6587\u4ef6\u4e2a\u6570\uff1a$f\"\n\techo \"\u8fde\u63a5\u6587\u4ef6\u4e2a\u6570\uff1a$l\"\n\techo \"\u5176\u4ed6\u6587\u4ef6\u4e2a\u6570\uff1a$o\"\n\n# \u7f16\u5199\u811a\u672c\uff0c\u547d\u4ee4\u884c\u53ef\u4ee5\u4f20\u5165\u4efb\u610f\u6570\u91cf\u7684\u6587\u4ef6\u8def\u5f84\uff0c\u811a\u672c\u5904\u7406\u540e\u8f93\u51fa\u6bcf\u4e2a\u6587\u4ef6\u7684\u6587\u4ef6\u7c7b\u578b\n\nfor filename in \"$@\"\ndo\n\tif &#91; -d $filename];then\n\t\techo \"$filename\u662f\u4e00\u4e2a\u76ee\u5f55\"\n\telif &#91; -f $filename];then\n\t\techo \"$filename\u662f\u4e00\u4e2a\u666e\u901a\u6587\u4ef6\"\n\telif &#91; -b $filename];then\n\t\techo \"$filename\u662f\u4e00\u4e2a\u5757\u6587\u4ef6\"\n\telse\n\t\techo \"$filename\u662f\u4e00\u4e2a\u672a\u77e5\u6587\u4ef6\"\n\tfi\ndone\n\n#\u5224\u65ad80\u7aef\u53e3\u662f\u5426\u5f00\u542f\uff0c\u5f00\u542f\u8f93\u5165ok\uff0c\u672a\u5f00\u542f\u5c31\u91cd\u542fhttpd\u670d\u52a1\uff0c\u5e76\u4e14\u4e00\u6b21\u91cd\u542f\u7684\u72b6\u6001\u8f93\u51fa\u6210\u529f\u6216\u5931\u8d25\nnetstat -na|grep LISTEN|grep '\\b80\\b' &amp;>\/dev\/null\nif &#91; $? -eq 0 ];then\n\techo \"port 80 has openned\"\nelse\n\techo \"port 80 closed\"\n\techo \"port 80 is restarting now...\"\n\tsystemctl restart httpd &amp;>\/dev\/null\n\tif &#91; $? -eq 0 ];then\n\t\techo \"restart succeful\"\n\telse\n\t\techo \"restart failed\"\n\tfi\n\nfi\n\n\n#\u7f16\u5199nginx\u7ba1\u7406\u811a\u672c,\u547d\u4ee4\u5982\u4e0b\n#nginx_manager.sh start\n#nginx_manager.sh stop\n#nginx_manager.sh restart\n#nginx_manager.sh reload\n\nif &#91; $# -ne 1 ];then\n\techo \"Usage: $0 start|stop|reload|restart|status\" #\u5148\u5224\u65ad\u547d\u4ee4\u662f\u5426\u7b26\u5408\u53ea\u67091\u4e2a\u53c2\u6570\n\texit\nfi\n\nargs=$1 # \u83b7\u53d6\u53c2\u6570\nfunction f1(){ #\u5224\u65ad\u547d\u4ee4\u662f\u5426\u6267\u884c\u6210\u529f\n\tif &#91; $? -eq 0 ];then\n\t\techo \"nginx $args successful\"\n\telse\n\t\techo \"nginx $args failed\"\n\tfi\n}\n\ncase $1 in\n\tstart)\n\t\tnetstat -na|grep -wq \"80\" # \u5148\u901a\u8fc7\u5224\u65ad\u7aef\u53e3\u662f\u5426\u5df2\u7ecf\u88ab\u4f7f\u7528\uff0c\u786e\u8ba4nginx\u670d\u52a1\u6709\u6ca1\u6709\u6253\u5f00\n\t\tif &#91; $? -eq 0];then\n\t\t\techo \"nginx is already running\"\n\t\telse\n\t\t\t\/usr\/local\/nginx\/sbin\/nginx &amp;>\/dev\/null\n\t\t\tf1\n\t\tfi\n\t\t;;\n\tstop)\n\t\t\/usr\/local\/nginx\/sbin\/nginx -s stop &amp;>\/dev\/null\n\t\tf1\n\t\t;;\n\treload)\n\t\t\/usr\/local\/nginx\/sbin\/nginx -s reload &amp;>\/dev\/null\n\t\tf1\n\t\t;;\n\trestart)\n\t\tnetstat -na|grep \":80\\b\" &amp;>\/dev\/null\n\t\t# 1. \u5148\u5173\u95ed\n\t\tif &#91; $? -eq 0 ];then\n\t\t\t\/usr\/local\/nginx\/sbin\/nginx -s stop\n\t\tfi\n\t\tsleep 2 #\u9632\u6b62\u8fd8\u6ca1\u5173\u95ed\u5b8c\u5c31\u9a6c\u4e0a\u542f\u52a8\u8d77\u4e0d\u6765\n\t\t# 2. \u518d\u542f\u52a8\n\t\t\/usr\/local\/nginx\/sbin\/nginx\n\t\t# 3. \u6700\u540e\u5224\u65ad\u662f\u5426\u542f\u52a8\n\t\tf1\n\t\t;;\n\tstatus)\n\t\t\/usr\/local\/nginx\/sbin\/nginx -s status &amp;>\/dev\/null\n\t\t;;\n\t*)\n\t\techo \"Usage: $0 start|stop|reload|restart|status\"\nesac\n\n# \u7f16\u5199\u731c\u6570\u5b57\u7a0b\u5e8f\uff0c\u6700\u591a\u731c\u95193\u6b21\uff0c\u731c\u5bf9\u4e86\u663e\u793a\u731c\u5bf9\u4e86\uff0c\u5927\u4e86\u63d0\u793a\u731c\u5927\u4e86\uff0c\u5c0f\u4e86\u63d0\u793a\u731c\u5c0f\u4e86\nnum=`echo $((RANDOM%100+1))`\n\ncount=0\nwhile:\ndo\n\t&#91; $count -eq 3] &amp;&amp; echo \"guess a wrong number 3 times, quit!\" &amp;&amp; exit\n\tread -p \"input a number in the range of 1-100: \"x\n\t&#91;&#91; ! $x=~ ^&#91;0-9]+$ ]] &amp;&amp; echo \"must be a number!\" &amp;&amp; continue\n\tif &#91; $x -gt $num ];then\n\t\techo \"too big\"\n\telif &#91; $x -lt $num ];then\n\t\techo \"too small\"\n\telse\n\t\techo \"correct!\"\n\t\tbreak\n\tfi\n\tlet count++\ndone\n\n\n#\u7528expect\u514d\u4ea4\u4e92\u521b\u5efa\u8fde\u63a5\uff0c\u8fed\u4ee31\n#! \/bin\/bash\nuser=$1\nip=$2\npassword=$3\n\nexpect&lt;&lt;EOF\n\tspawn ssh $user@$ip\n\n\texpect {\n\n\t\t\"yes\/no\"{send \"yes\\n\";exp_continue}\n\t\t\"pass* {send \"$password\\n\"}\"\n\t}\n\n\texpect {\n\t\t\"\\#\" {send \"ls\\n\"}\n\t}\n\n\texpect {\n\t\t\"\\#\" {send \"exit\\n\"}\n\t}\n\n\texpect EOF\n\nEOF\n\n\n#\u7528expect\u514d\u4ea4\u4e92\u521b\u5efa\u8fde\u63a5\uff0c\u8fed\u4ee32 \u5faa\u73af\u521b\u5efa\u8fde\u63a5\n#\u5b9e\u9a8c\u4ee5\u524d\u5148\u5220\u9664\u672c\u673a\u7684\u516c\u94a5,\u5e76\u91cd\u65b0\u521b\u5efa\u5bc6\u94a5\u5bf9\nrm -rf \/root\/.ssh\/*\nssh-keygen\n\n\nfor ip in 192.168.1.111 192.168.1.103\ndo\nexpect&lt;&lt;EOF\n\n\tspawn ssh-copy-id -i root@$ip\n\texpect {\n\t\t\"yes\/no\" {send \"yes\\n\";exp_continue}\n\t\t\"password\" {send \"abcd@1234\\n\"}\n\t}\n\texpect eof\n\nEOF\ndone\n\n\n#\u7528expect\u514d\u4ea4\u4e92\u521b\u5efa\u8fde\u63a5\uff0c\u8fed\u4ee33 \u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\n#\u5b9e\u9a8c\u4ee5\u524d\u5148\u5220\u9664\u672c\u673a\u7684\u516c\u94a5,\u5e76\u91cd\u65b0\u521b\u5efa\u5bc6\u94a5\u5bf9\n\n# host_info.txt:\nuser1:password1:1.1.1.1\nuser2:password2:2.2.2.2\nuser3:password3:3.3.3.3\n\n# host_connect.sh\nfor host_info in `cat host_info.txt`\ndo\n\tusername=$(echo $host_info|awk -F:'{print $1}')\n\tpassword=$(echo $host_info|awk -F:'{print $2}')\n\tip_addr=$(echo $host_info|awk -F:'{print $3}')\n\nexpect&lt;&lt;EOF\n\tspawn ssh-copy-id -i $username@$ip_addr\n\t\n\texpect {\n\t\t\"yes\/no\" {send \"yes\\n\";exp_continue}\n\t\t\"pass*\" {send \"abcd@1234\\n\"}\n\t\n\t}\n\texpect EOF\nEOF\n\ndone\n\n#\u7528\u6237\u767b\u5f55\u6700\u521d\u7248\n#!\/bin\/bash\n\ni=0\nwhile true\ndo\n    read -p \"please input your username: \" inp_username\n    for line in `cat lock.txt`\n    do\n        lock_user=$(echo $line |cut -d: -f1)\n        lock_time=$(echo $line |cut -d: -f2)\n        if &#91; \"$inp_username\" = \"$lock_user\" ];then\n            # \u5224\u65ad\u9501\u5b9a\u7684\u65f6\u95f4\uff0c\u5982\u679c\u8d85\u8fc760s\uff0c\u5e94\u8be5\u89e3\u9501\u653e\u884c\uff0c\u5426\u5219\u9501\u5b9a\n        fi\n    done\n    read -p \"please input your password: \" inp_password\n    \n    \n    if &#91; \"$inp_username\" = \"egon\" ] &amp;&amp; &#91; \"$inp_password\" = \"123\" ];then\n        echo \"welcome to home page!!!\"\n        # \u653e\u767b\u5f55\u6210\u529f\u4e4b\u540e\u8981\u6267\u884c\u7684\u4ee3\u7801\uff01\uff01\uff01\n        break\n    else\n        echo \"username or password input error\"\n        let i++\n    fi\n    if &#91; $i -eq 3 ];then\n        echo \"try too many times\uff0cbreak\uff01\uff01\uff01\"\n        echo \"$inp_username:$(date '+%s')\" >> lock.txt \n    fi\ndone\n\n#\u7528\u6237\u767b\u5f55\u6539\u8fdb\u7248\n&#91;root@www.egonlin.com \/scripts]# cat login.sh \n#!\/bin\/bash\n\nfunction lock_test() {\n    for line in `cat lock.txt`\n    do\n        lock_user=$(echo $line |cut -d: -f1)\n        lock_time=$(echo $line |cut -d: -f2)\n        if &#91; \"$inp_username\" = \"$lock_user\" ];then\n            # \u5224\u65ad\u9501\u5b9a\u7684\u65f6\u95f4\uff0c\u5982\u679c\u8d85\u8fc760s\uff0c\u5e94\u8be5\u89e3\u9501\u653e\u884c\uff0c\u5426\u5219\u9501\u5b9a\n            current_time=$(date '+%s')\n            period=$((current_time-lock_time))\n            if &#91; $period -le 60 ];then\n               # \u5904\u4e8e\u9501\u5b9a\u5468\u671f\u5185\n               echo \"\u7528\u6237\u5904\u4e8e\u9501\u5b9a\u72b6\u6001\u3002\u3002\u3002\"\n               return 1\n            else\n               # \u5e94\u8be5\u89e3\u9501\uff0c\u7ee7\u7eed\u8fdb\u884c\u540e\u7eed\u8ba4\u8bc1\u73af\u8282\n               return 0\n            fi\n        fi\n    done\n}\n\ni=0\nwhile true\ndo\n    read -p \"please input your username: \" inp_username\n    lock_test\n    if &#91; $? -ne 0 ];then\n       echo \"===============================\"\n       continue\n    fi\n    read -p \"please input your password: \" inp_password\n    \n    \n    if &#91; \"$inp_username\" = \"egon\" ] &amp;&amp; &#91; \"$inp_password\" = \"123\" ];then\n        echo \"welcome to home page!!!\"\n        # \u653e\u767b\u5f55\u6210\u529f\u4e4b\u540e\u8981\u6267\u884c\u7684\u4ee3\u7801\uff01\uff01\uff01\n        break\n    else\n        echo \"username or password input error\"\n        let i++\n    fi\n    if &#91; $i -eq 3 ];then\n        echo \"try too many times\uff0cbreak\uff01\uff01\uff01\"\n        echo \"$inp_username:$(date '+%s')\" >> lock.txt \n    fi\ndone\n\n#\u7528\u6237\u767b\u5f55\u6700\u7ec8\u7248\n&#91;root@www.egonlin.com \/scripts]# cat login.sh \n#!\/bin\/bash\n\nfunction lock_test() {\n    for line in `cat lock.txt`\n    do\n        lock_user=$(echo $line |cut -d: -f1)\n        lock_time=$(echo $line |cut -d: -f2)\n        if &#91; \"$inp_username\" = \"$lock_user\" ];then\n            # \u5224\u65ad\u9501\u5b9a\u7684\u65f6\u95f4\uff0c\u5982\u679c\u8d85\u8fc760s\uff0c\u5e94\u8be5\u89e3\u9501\u653e\u884c\uff0c\u5426\u5219\u9501\u5b9a\n            current_time=$(date '+%s')\n            period=$((current_time-lock_time))\n            if &#91; $period -le 60 ];then\n               # \u5904\u4e8e\u9501\u5b9a\u5468\u671f\u5185\n               echo \"\u7528\u6237\u5904\u4e8e\u9501\u5b9a\u72b6\u6001\u3002\u3002\u3002\"\n               return 1\n            else\n               # \u5e94\u8be5\u89e3\u9501\uff0c\u7ee7\u7eed\u8fdb\u884c\u540e\u7eed\u8ba4\u8bc1\u73af\u8282\n               sed -ri \"\/^\\b($inp_username)\\b\/d\" lock.txt\n\t\t\t   lock_user_info&#91;$k]=0\n               return 0\n            fi\n        fi\n    done\n}\n\ndeclare -A lock_user_info\nwhile true\ndo\n    read -p \"please input your username: \" inp_username\n    lock_test\n    if &#91; $? -ne 0 ];then\n       echo \"===============================\"\n       continue\n    fi\n    read -p \"please input your password: \" inp_password\n    \n    \n    if &#91; \"$inp_username\" = \"egon\" ] &amp;&amp; &#91; \"$inp_password\" = \"123\" ];then\n        echo \"welcome to home page!!!\"\n        # \u653e\u767b\u5f55\u6210\u529f\u4e4b\u540e\u8981\u6267\u884c\u7684\u4ee3\u7801\uff01\uff01\uff01\n        break\n    else\n        echo \"username or password input error\"\n        let lock_user_info&#91;$inp_username]++\n    fi\n    declare -A |grep lock_user_info\n    for k in ${!lock_user_info&#91;@]};\n    do \n        username=$k \n        grep -q \"^\\b$username\\b\" lock.txt\n        if &#91; $? -eq 0 ];then\n            continue\n        fi\n        error_times=${lock_user_info&#91;$k]}\n        if &#91; $error_times -ge 3 ];then\n            echo \"try too many times\uff0cbreak\uff01\uff01\uff01\"\n            echo \"$username:$(date '+%s')\" >> lock.txt \n        fi\n    done\ndone<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-shell"],"_links":{"self":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=37"}],"version-history":[{"count":1,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":38,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=\/wp\/v2\/posts\/37\/revisions\/38"}],"wp:attachment":[{"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhaoyanqi.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}