每日一文件
https://github.com/aminglinux/shell100/blob/master/
要求:安照这样的日期格式(xxxx-xx-xx)每日生成一个文件,如生成的文件为2017-12-20.log,并且把磁盘的使用情况写到这个文件中,提示:date、df
[root@centos-04 tmp]# date2018年 12月 26日 星期三 19:29:13 CST[root@centos-04 tmp]# date +%Y2018[root@centos-04 tmp]# date +%y18[root@centos-04 tmp]# date +%d26[root@centos-04 tmp]# date +%m12[root@centos-04 tmp]# date +%H19[root@centos-04 tmp]# date +%M30[root@centos-04 tmp]# date +%S52[root@centos-04 tmp]# date +%s1545823854[root@centos-04 tmp]# date +%F2018-12-26[root@centos-04 tmp]# date +%T19:31:04[root@centos-04 tmp]#
[root@centos-04 tmp]# date +%w3[root@centos-04 tmp]# date +%W52[root@centos-04 tmp]#
昨天日期
[root@centos-04 tmp]# date -d "-1 day" +%F2018-12-25[root@centos-04 tmp]#
上一小时
[root@centos-04 tmp]# date -d "-1 hours" +%T18:34:54[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 1.sh#!/bin/bashd=`date +%F`df -h > $d.log~ [root@centos-04 tmp]# sh 1.sh [root@centos-04 tmp]# ls1.sh ansible-ssh-192.168.242.130-22-root ansible-ssh-192.168.242.133-22-root lua_uwpzx3 tmp.SLBPtZ45L92018-12-26.log ansible-ssh-192.168.242.131-22-root elasticsearch.4Kw1U8qo nginx_proxy_tmp456.log ansible-ssh-192.168.242.132-22-root hsperfdata_root proxy.log[root@centos-04 tmp]# [root@centos-04 tmp]# cat 2018-12-26.log 文件系统 容量 已用 可用 已用% 挂载点/dev/mapper/centos-root 18G 6.2G 12G 36% /devtmpfs 898M 0 898M 0% /devtmpfs 910M 0 910M 0% /dev/shmtmpfs 910M 30M 881M 4% /runtmpfs 910M 0 910M 0% /sys/fs/cgroup/dev/sda1 497M 167M 331M 34% /boottmpfs 182M 0 182M 0% /run/user/0overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/mergedshm 64M 0 64M 0% /var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shmoverlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/mergedoverlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/mergedshm 64M 0 64M 0% /var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shmshm 64M 0 64M 0% /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm[root@centos-04 tmp]#
改进版
d=`date +%F` #获取当前日期dir=/data/logs/disklog #指定日志文件生成的目录if [ ! -d $dir ] #判断如果没有目录创建then mkdir -p $dirfidf -h > $dir/$d.log #将硬盘信息写到日志find $dir/ -mtime +365 |xargs rm #删除一年之前的文件
统计IP访问量
awk '{print $3}' access_log.2018122918 |sort -n|uniq -c|sort -n -r|less
[root@centos-04 tmp]# vim 2.sh#!/bin/bashawk '{print $3}' access_log.2018122918 |sort -n|uniq -c|sort -n -r[root@centos-04 tmp]# sh 2.sh
统计内存占用之和
ps aux|sed '1d' (删除结果中第一行)
[root@centos-04 tmp]# vim 3.sh#!/bin/bashsum=0for n in `ps aux |grep -v 'TIME COMMAND' |awk '{print $6}'`do sum=$[$sum+$n]doneecho $sum [root@centos-04 tmp]# sh 3.sh 114892[root@centos-04 tmp]# free total used free shared buff/cache availableMem: 1863224 104260 1610364 9976 148600 1593352Swap: 0 0 0[root@centos-04 tmp]#
检测机器存活
[root@centos-04 tmp]# ping -c2 www.baidu.com|grep 'packet' |awk -F '%' '{print $1}' |awk '{print $NF}'0[root@centos-04 tmp]#
[root@centos-04 tmp]# vim mail.py#!/usr/bin/env python#-*- coding: UTF-8 -*-import os,sysreload(sys)sys.setdefaultencoding('utf8')import getoptimport smtplibfrom email.MIMEText import MIMETextfrom email.MIMEMultipart import MIMEMultipartfrom subprocess import *def sendqqmail(username,password,mailfrom,mailto,subject,content): gserver = 'smtp.qq.com' gport = 25 try: # msg = MIMEText(unicode(content).encode('utf-8')) //如果发送的邮件有乱码,可以尝试把这行改成如下: msg = MIMEText(content,'plan','utf-8') msg['from'] = mailfrom msg['to'] = mailto msg['Reply-To'] = mailfrom msg['Subject'] = subject smtp = smtplib.SMTP(gserver, gport) smtp.set_debuglevel(0) smtp.ehlo() smtp.login(username,password) smtp.sendmail(mailfrom, mailto, msg.as_string()) smtp.close() except Exception,err: print "Send mail failed. Error: %s" % errdef main(): to=sys.argv[1] subject=sys.argv[2] content=sys.argv[3]##定义QQ邮箱的账号和密码,你需要修改成你自己的账号和密码(请不要把真实的用户名和密码放到网上公开,否则你会死的很惨) sendqqmail('1234567@qq.com','aaaaaaaaaa','1234567@qq.com',to,subject,content)if __name__ == "__main__": main() #####脚本使用说明#######1. 首先定义好脚本中的邮箱账号和密码#2. 脚本执行命令为:python mail.py 目标邮箱 "邮件主题" "邮件内容"
[root@centos-04 tmp]# vim 4.sh
#!/bin/bashn=`ping -c5 www.baidu.com|grep 'packet' |awk -F '%' '{print $1}' |awk '{print $FN}'`if [ -z "$n" ]then echo "脚本有问题。" exitelse n1=`echo $n|sed 's/[0-9]//g'` if [ -n "$n" ] then echo "脚本$0有问题。" exit fifim=123@qq.comwhile :do if [ $n -ge 50 ] then python mail.py $m "机器宕机" "丢包率$n%" fi sleep 30done
批量改文件名
[root@centos-04 tmp]# cp -r /123/ /123.bak
[root@centos-04 tmp]# tar -tf 123.tar.gz
[root@centos-04 tmp]# vim 5.sh #!/bin/bashfind /123/ -type f -name "*.txt" > /tmp/txt.listfor f in `cat /tmp/txt.list`do mv $f $f.bakdone#find /123/ -type f -name *.txt |xargs -i mv {} {}.bak#find /123/ -type f -name *.txt -exec mv {} {}.bak \;for f in `cat /tmp/txt.list`do echo $f.bakdone > /tmp/txt.bak.listtar -czvf 123.tar.gz `cat /tmp/txt.bak.list |xargs`for f in `cat /tmp/txt.list`do mv $f.bak $fdone
[root@centos-04 tmp]# rsync -av /123.bak/ /123/
检测80端口
[root@centos-04 tmp]# netstat -lntp |grep 80tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6938/nginx: master tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 6938/nginx: master tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 6938/nginx: master [root@centos-04 tmp]# netstat -lntp |grep ':80 'tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6938/nginx: master [root@centos-04 tmp]#
[root@centos-04 tmp]# yum install -y nmap[root@centos-04 tmp]# nmap -p 80 127.0.0.1Starting Nmap 6.40 ( http://nmap.org ) at 2019-01-04 03:29 CSTNmap scan report for localhost (127.0.0.1)Host is up (0.00011s latency).PORT STATE SERVICE80/tcp open httpNmap done: 1 IP address (1 host up) scanned in 0.04 seconds[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 6.sh
#!/bin/bashm=123@123.comwhile :do n=`netstat -lntp |grep ':80 '|wc -l` if [ $n -eq 0 ] then /usr/local/apache2/bin/apachectl -k restart 2>/tmp/apache.err python mail.py $m "80端口关闭" "已经重启httpd服务" pn=`pgrep -l httpd|wc -l` if [ $pn -eq 0 ] then python mail.py $m "httpd重启失败" "`head -1 /tmp/apache.err`" fi sleep 30done
也可以用nohup或screen这screen里执行6.sh
[root@centos-04 tmp]# nohup sh 6.sh &
[root@centos-04 tmp]# nmap -p 80 127.0.0.1 |grep '80/tcp' |awk '{print $2}' (用nmap写法)open[root@centos-04 tmp]#
备份数据库
[root@centos-04 tmp]# vim 7.sh#!/bin/bashd1=`date +%w`d2=`date +%d`local_backdir=/bak/mysqlremote_backdir=192.168.242.130::backupexec 1> /tmp/mysqlbak.log 2>/tmp/mysqlbak.errecho "mysql backup begin at `date`"mysqldump -uroot -pxxx discuz > $local_backdir/discuz.sql.$d1rsync -az $local_backdir/discuz.sql.$d1 $remote_backdir/discuz.sql.$d2echo "mysql backup end at `date`"
检测502
1.502的情况:php配置有问题、php脚本耗资源。
[root@centos-04 tmp]# vim 8.sh#!/bin/bashlog=/data/log/access.logwhile :do502_n=`tail -n 300 $log |grep -c ' 502 '`if [ -z "$502_n" ]then exitfiif [ $502_n -gt 100 ]then /etc/init.d/php-fpm restart >/dev/null 2>/tmp/php-fpm.err fpm_p_n=`pgrep -l php-fpm|wc -l` if [ $fpm_p_n -eq 0 ] then python mail.py xxx@xx.com "php-fpm err" "head -1 /tmp/php-fpm.err" exit fifisleep 10done
删除字母和行
1.删除前五行中包含字母的行
[root@centos-04 tmp]# head -n5 test.sql |sed '/[a-zA-Z]/d'
2.查看文件前5行
[root@centos-04 tmp]# sed -n '1,5'p mail.py #!/usr/bin/env python#-*- coding: UTF-8 -*-import os,sysreload(sys)sys.setdefaultencoding('utf8')[root@centos-04 tmp]#
3.把6到10行中的全部字母删掉
[root@centos-04 tmp]# vim 9.sh#!/bin/bashsed -n '1,5'p 1.txt|sed '/[a-zA-Z]/d'sed '1,5d' 1.txt |sed '1,5s/[a-zA-Z]//g'
找单词
[root@centos-04 tmp]# for w in Bash also interprets a number of multi-character options.; do echo $w; doneBashalsointerpretsanumberofmulti-characteroptions.[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 10.sh#!/bin/bashc="Bash also interprets a number of multi-character options."n=`echo $c|awk -F '[ +-.]' '{print NF}'`for ((i=1;i<$n;i++))do l=`echo $c|awk -F '[ +-.]' -v j=$i '{print $j}'|wc -L` if [ $l -lt 6 ] then echo $c|awk -F '[ +-.]' -v j=$i '{print $j}' fidone[root@centos-04 tmp]# sh 10.sh Bashalsoaofmulti[root@centos-04 tmp]#
输入数字执行命令
[root@centos-04 tmp]# vim 11.sh#!/bin/bashecho "*cmd meau** 1 - date 2 - ls 3 - who 4 - pwd"read -p "Please input a number:" nif [ -z "$n" ]then echo "请输入一纯数字,范围1-4。" exitfin1=`echo $n|sed 's/[0-9]//g'`if [ -n "$n1" ]then echo "请输入一个纯数字,范围1-4。" exitficase $n in 1) date ;; 2) ls ;; 3) who ;; 4) pwd ;; *) echo "请输入1-4的数字" ;;esac[root@centos-04 tmp]# sh 11.sh *cmd meau** 1 - date 2 - ls 3 - who 4 - pwdPlease input a number:12019年 01月 04日 星期五 21:43:35 CST[root@centos-04 tmp]# sh 11.sh *cmd meau** 1 - date 2 - ls 3 - who 4 - pwdPlease input a number:210.sh 11.sh 1.sh 2018-12-26.log 2.sh 3.sh 456.log 4.sh 5.sh 6.sh 7.sh 8.sh 9.sh mail.py test.sql tmp.SzNhh17qiE[root@centos-04 tmp]# sh 11.sh *cmd meau** 1 - date 2 - ls 3 - who 4 - pwdPlease input a number:3root pts/1 2019-01-04 18:26 (192.168.242.1)[root@centos-04 tmp]# sh 11.sh *cmd meau** 1 - date 2 - ls 3 - who 4 - pwdPlease input a number:4 /tmp[root@centos-04 tmp]# sh 11.sh *cmd meau** 1 - date 2 - ls 3 - who 4 - pwdPlease input a number:5请输入1-4的数字[root@centos-04 tmp]# sh 11.sh *cmd meau** 1 - date 2 - ls 3 - who 4 - pwdPlease input a number:fsaf请输入一个纯数字,范围1-4。[root@centos-04 tmp]#
批量创建用户
[root@centos-04 tmp]# seq -w 00 0900010203040506070809[root@centos-04 tmp]# seq 0 9 0123456789[root@centos-04 tmp]#
给用户添加密码
[root@centos-04 tmp]# useradd user1[root@centos-04 tmp]# passwd user1更改用户 user1 的密码 。新的 密码:无效的密码: 密码少于 7 个字符重新输入新的 密码:passwd:所有的身份验证令牌已经成功更新。[root@centos-04 tmp]#
在脚本中自动给用户添加密码两种方式
[root@centos-04 tmp]# echo -e "user1\nuser1\n" |passwd user1更改用户 user1 的密码 。新的 密码:无效的密码: 密码少于 7 个字符重新输入新的 密码:passwd:所有的身份验证令牌已经成功更新。[root@centos-04 tmp]# echo "user1" |passwd --stdin user1更改用户 user1 的密码 。passwd:所有的身份验证令牌已经成功更新。[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 12.sh#!/bin/bashfor i in `seq -w 00 09`do useradd user_$i p=`mkpasswd -l 10 -s 0` echo "user_$i $p" >> /tmp/pass.tmp echo $p |passwd --stdin user_$idone[root@centos-04 tmp]# sh 12.sh 更改用户 user_00 的密码 。passwd:所有的身份验证令牌已经成功更新。更改用户 user_01 的密码 。passwd:所有的身份验证令牌已经成功更新。更改用户 user_02 的密码 。passwd:所有的身份验证令牌已经成功更新。更改用户 user_03 的密码 。passwd:所有的身份验证令牌已经成功更新。更改用户 user_04 的密码 。passwd:所有的身份验证令牌已经成功更新。更改用户 user_05 的密码 。passwd:所有的身份验证令牌已经成功更新。更改用户 user_06 的密码 。passwd:所有的身份验证令牌已经成功更新。更改用户 user_07 的密码 。passwd:所有的身份验证令牌已经成功更新。更改用户 user_08 的密码 。passwd:所有的身份验证令牌已经成功更新。更改用户 user_09 的密码 。passwd:所有的身份验证令牌已经成功更新。[root@centos-04 tmp]#
[root@centos-04 tmp]# tail /etc/passwduser_00:x:1001:1001::/home/user_00:/bin/bashuser_01:x:1002:1002::/home/user_01:/bin/bashuser_02:x:1003:1003::/home/user_02:/bin/bashuser_03:x:1004:1004::/home/user_03:/bin/bashuser_04:x:1005:1005::/home/user_04:/bin/bashuser_05:x:1006:1006::/home/user_05:/bin/bashuser_06:x:1007:1007::/home/user_06:/bin/bashuser_07:x:1008:1008::/home/user_07:/bin/bashuser_08:x:1009:1009::/home/user_08:/bin/bashuser_09:x:1010:1010::/home/user_09:/bin/bash[root@centos-04 tmp]# cat /tmp/pass.tmp user_00 8xiwgZSce6user_01 yaMp6cb2gAuser_02 jx0QtlL2fwuser_03 69bwuqgEDfuser_04 p3fpvMMl9cuser_05 fm5Bv4Xssxuser_06 ivx69zVIpyuser_07 l77CvxvuHyuser_08 MZfmi6kx4fuser_09 4bAkzeaKa6[root@centos-04 tmp]#
登录测试(复制user_00的密码粘贴密码登录成功)
[root@centos-04 ~]# ssh user_00@192.168.242.130user_00@192.168.242.130's password: Last login: Sat Jan 5 00:00:28 2019[user_00@centos-04 ~]$
删掉刚刚创建的用户
[root@centos-04 tmp]# for i in `seq -w 00 09`;do userdel -r user_$i; done[root@centos-04 tmp]# tail /etc/passwdtcpdump:x:72:72::/:/sbin/nologinrpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologinrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologinnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinhaproxy:x:188:188:haproxy:/var/lib/haproxy:/sbin/nologindockerroot:x:994:991:Docker User:/var/lib/docker:/sbin/nologinepmd:x:993:990:Erlang Port Mapper Daemon:/tmp:/sbin/nologinrabbitmq:x:992:989:RabbitMQ messaging server:/var/lib/rabbitmq:/bin/bashuser1:x:1000:1000::/home/user1:/bin/bash
监控httpd进程
[root@centos-04 tmp]# vim 13.sh #!/bin/bashcheck_service(){ c=0 for i in `seq 1 5` do /usr/local/apache2/bin/apachectl -k restart 2> /tmp/httpd.err if [ ! $? -eq 0 ] then c=$[$c+1] else break fi done if [ $c -eq 5 ] then python mail.py "123@qq.com " "apache进程数量大于500,重启失败。" "`head -1 /tmp/httpd.err`" exit fi}while :do n=`ps -C httpd --no-heading|wc -l` if [ $n -ge 500 ] then check_service sleep 60 n_new=`ps -C httpd --no-heading|wc -l` if [ $n_new -ge 500 ] then python mail.py "123@qq.com " "apache重启一分钟后进程数量仍然大于500" "请登录服务器排查问题" exit fi fisleep 10done
封ip
[root@centos-04 logs]# tail access.log 127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/replicationcontrollers?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/ scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/services?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/apps/v1beta1/statefulsets?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/ scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/storage.k8s.io/v1/storageclasses?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/ a452946/scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/persistentvolumes?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/policy/v1beta1/poddisruptionbudgets?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/ a452946/scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/extensions/v1beta1/replicasets?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/ scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/nodes?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/persistentvolumeclaims?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/ scheduler"127.0.0.1 - - [19/Dec/2018:23:46:50 +0800] "GET /api/v1/pods?fieldSelector=status.phase%21%3DFailed%2Cstatus.phase%21%3DSucceeded&limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" " kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"[root@centos-04 logs]# [root@centos-04 logs]# egrep '2018:23:46:[0-9]+' access.log |tail (这里使用egrep,因为正则中有+号)127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/replicationcontrollers?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/ scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/services?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/apps/v1beta1/statefulsets?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/ scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/storage.k8s.io/v1/storageclasses?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/ a452946/scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/persistentvolumes?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/policy/v1beta1/poddisruptionbudgets?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/ a452946/scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /apis/extensions/v1beta1/replicasets?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/ scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/nodes?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"127.0.0.1 - - [19/Dec/2018:23:46:49 +0800] "GET /api/v1/persistentvolumeclaims?limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/ scheduler"127.0.0.1 - - [19/Dec/2018:23:46:50 +0800] "GET /api/v1/pods?fieldSelector=status.phase%21%3DFailed%2Cstatus.phase%21%3DSucceeded&limit=500&resourceVersion=0 HTTP/1.1" 404 169 "-" "kube-scheduler/v1.11.3 (linux/amd64) kubernetes/a452946/scheduler"[root@centos-04 logs]#
求出上一分钟时间
[root@centos-04 logs]# echo `date -d "-1 min" +%Y:%H:%M`2019:01:39[root@centos-04 logs]# date2019年 01月 05日 星期六 01:40:53 CST[root@centos-04 logs]#
查看iptables (pkts有多少个数据包,bytes有多少字节)
[root@centos-04 tmp]# iptables -nvLChain INPUT (policy ACCEPT 6947 packets, 5761K bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 46 86418 DOCKER-ISOLATION all -- * * 0.0.0.0/0 0.0.0.0/0 46 86418 DOCKER all -- * docker0 0.0.0.0/0 0.0.0.0/0 44 86298 ACCEPT all -- * docker0 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 0 0 ACCEPT all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0 2 120 ACCEPT all -- docker0 docker0 0.0.0.0/0 0.0.0.0/0 Chain OUTPUT (policy ACCEPT 5072 packets, 1722K bytes) pkts bytes target prot opt in out source destination Chain DOCKER (1 references) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 172.17.0.2 tcp dpt:3306 0 0 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 172.17.0.3 tcp dpt:9000 0 0 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 172.17.0.4 tcp dpt:80Chain DOCKER-ISOLATION (1 references) pkts bytes target prot opt in out source destination 46 86418 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0 [root@centos-04 tmp]#
[root@centos-04 tmp]# iptables -I INPUT -p tcp --dport 80 -s 1.1.1.1 -j REJECT (封掉1.1.1.1)[root@centos-04 tmp]# iptables -nvLChain INPUT (policy ACCEPT 55 packets, 3968 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT tcp -- * * 1.1.1.1 0.0.0.0/0 tcp dpt:80 reject-with icmp-port-unreachableChain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 46 86418 DOCKER-ISOLATION all -- * * 0.0.0.0/0 0.0.0.0/0 46 86418 DOCKER all -- * docker0 0.0.0.0/0 0.0.0.0/0 44 86298 ACCEPT all -- * docker0 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED 0 0 ACCEPT all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0 2 120 ACCEPT all -- docker0 docker0 0.0.0.0/0 0.0.0.0/0 Chain OUTPUT (policy ACCEPT 34 packets, 3144 bytes) pkts bytes target prot opt in out source destination Chain DOCKER (1 references) pkts bytes target prot opt in out source destination 0 0 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 172.17.0.2 tcp dpt:3306 0 0 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 172.17.0.3 tcp dpt:9000 0 0 ACCEPT tcp -- !docker0 docker0 0.0.0.0/0 172.17.0.4 tcp dpt:80Chain DOCKER-ISOLATION (1 references) pkts bytes target prot opt in out source destination 46 86418 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0 [root@centos-04 tmp]#
#!/bin/bashblock_ip{t1=`date -d "-1 min" +%Y:%H:%M`log/data/logs/access_logegrep "$t1:[0-9]+" $log > /tmp/tmp_last_min.logawk '{print $1}' /tmp/tmp_last_min.log |sort -n |uniq -c|sort -n |awk '$1>100 {print $2}' > /tmp/bad_ip.listn=`wc -l /tmp/bad_ip.list|awk '{print $1}'`if [ $n -ne 0 ]then for ip in `cat /tmp/bad_ip.list` do iptables -I INPUT -s $ip -j REJECT donefi}unblock_ip(){ iptables -nvL INPUT |sed '1d' |awk '$1<5 {print $8}' > /tmp/good_ip.list n=`wc -l /tmp/good_ip.list|awk '{print $1}'` if [ $n -ne 0 ] then for ip in `cat /tmp/good_ip.list` do iptables -D INPUT -s $ip -j REJECT done fi iptables -Z}t=`date +%M`if [ $t == "00" ] || [ $t == "30" ]then unblock_ip block_ipelse block_ipfi
[root@centos-04 tmp]# vim 14.sh
将该脚本放到计划任务中每分钟执行一次
算数字
[root@centos-04 tmp]# vim 15.sh#!/bin/bashx=10y=21for i in `seq 0 15`; do echo $x;x=$[$x+$y]z=$[2**$i] (求幂)y=$[$y+$z]done[root@centos-04 tmp]# sh 15.sh 1031537710514119327742570112332277434584611667333077
查用户
获取linux版本
[root@centos-04 tmp]# awk -F 'release ' '{print $2}' /etc/redhat-release |cut -d '.' -f17[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 16.sh#!/bin/bashv=`awk -F 'release ' '{print $2}' /etc/redhat-release |cut -d '.' -f1`user(){ if [ $1 -eq 0 ] then echo "系统没有自定义的用户" else echo "系统存在自定义用户。有$1个" fi}case $v in 5|6) n=`awk -F ':' '$3>=500' /etc/passwd|wc -l` user $n ;; 7) n=`awk -F ':' '$3>=1000' /etc/passwd|wc -l` user $n ;; *) echo "脚本出错" ;;esac[root@centos-04 tmp]# sh 16.sh 系统存在自定义用户。有3个[root@centos-04 tmp]#
[root@centos-04 tmp]# awk -F ':' '$3 >= 1000' /etc/passwd nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinuser1:x:1000:1000::/home/user1:/bin/bashuser_00:x:1001:1001::/home/user_00:/bin/bash[root@centos-04 tmp]#
检测磁盘
查看空间使用量和inode使用量
[root@centos-04 tmp]# df[root@centos-04 tmp]# df -i
[root@centos-04 tmp]# df |awk '{print $5}'已用%36%0%0%2%0%34%36%0%36%0%36%0%0%[root@centos-04 tmp]# df |awk '{print $5}'|sed 's/%//'已用360020343603603600[root@centos-04 tmp]#
去除标头、解决有小数点的情况
[root@centos-04 tmp]# df|sed '1d' |awk '{print $5}'|sed 's/%//'|cut -d '.' -f1360020343603603600[root@centos-04 tmp]#
最终获取大于85的命令
[root@centos-04 tmp]# df|sed '1d' |awk -F ' +|%' '$5>85 {print $7}' [root@centos-04 tmp]#
[root@centos-04 tmp]# df|sed '1d' |awk -F ' +|%' '$5<85 {print $7}' //dev/dev/shm/run/sys/fs/cgroup/boot/var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/merged/var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shm/var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/merged/var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm/var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/merged/var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shm/run/user/[root@centos-04 tmp]# vim 17.sh
#!/bin/bashdir=/tmp/diskd=`date +%F`m=123@123.com[ -d $dir ] || mkdir $dirdf >> $dir/$d.logdf -i >> $dir/$d.logdf|sed '1d' |awk -F ' +|%' '$5>=85 {print $7}' > $dir/df.tmpdf -i|sed '1d' |awk -F ' +|%' '$5>=85 {print $7}' > $dir/df_i.tmpn1=`wc -l $dir/df.tmp|awk '{print $1}'`n2=`wc -l $dir/df_i.tmp|awk '{print $1}'`tag=0if [ $n1 -gt 0 ]then if [ $n2 -gt 0 ] then tag=11 else tag=10 fielse if [ $n2 -gt 0 ] then tag=01 else tag=00 fificase $tag in 11) python mail.py $m "磁盘空间和inode使用率高于85" "`cat $dir/df.tmp $dir/df_i.tmp|xargs`" ;; 10) python mail.py $m "磁盘空使用率高于85" "`cat $dir/df.tmp|xargs`" ;; 01) python mail.py $m "磁盘inode使用率高于85" "`cat $dir/df_i.tmp|xargs`" ;; *) ;;esac
检测新文件
需要任务计划执行
[root@centos-04 tmp]# vim 18.sh#!/bin/bashbasedir=/tmp/t=`date +%Y%m%d%H%M`find $basedir/ -type f -mmin -5 > /tmp/file.listn=`wc -l /tmp/file.list|awk '{print $1}'`if [ $n -gt 0 ]then mv /tmp/file.list /tmp/$t.listfi
[root@centos-04 tmp]# sh 18.sh [root@centos-04 tmp]# ls10.sh 12.sh 14.sh 16.sh 18.sh 2018-12-26.log 2.sh 456.log 5.sh 7.sh 9.sh mail.py test.sql11.sh 13.sh 15.sh 17.sh 1.sh 201901050745.list 3.sh 4.sh 6.sh 8.sh disk pass.tmp tmp.SzNhh17qiE
最常用的命令
history命令调用的就是~/.bash_history文件的内容,统计最常用的10条命令
[root@centos-04 tmp]# cat ~/.bash_history |sort |uniq -c |sort -nr |head 238 ls 34 docker ps 25 cd ../ 24 docker ps -a 18 docker exec -it c58be577ba9f bash 17 docker images 13 docker exec -it c00e5859f876 bash 10 ll 10 docker restart c00e5859f876 9 ps aux|grep nginx[root@centos-04 tmp]#
统计文件大小
计划任务执行
时间计算
[root@centos-04 tmp]# date -d "-16 hour" +%H08[root@centos-04 tmp]# date +%H 00[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 20.sh #!/bin/bashdir=/tmp/log_statt=`date +%d%H`t1=`date +%H`logdir=/data/log[ -f $dir/$t.log ] && rm -f $dir/$t.log[ -d $dir ] || mkdir $dirif [ $t == "00" -o $t == "12" ]then for f in `find $logdir/ -type f` do > $f doneelse for f in `find $logdir/ -type f` do du -sh $f >> $dir/$t.log donefi
计算数字个数
-e的用法
[root@centos-04 tmp]# for i in `echo -e "123\nabc 123"`; do echo $i; done123abc123[root@centos-04 tmp]#
[root@centos-04 tmp]# wc -l 202018-12-26.log 201901050745.list 20.sh [root@centos-04 tmp]# wc -l 2018-12-26.log 14 2018-12-26.log[root@centos-04 tmp]# for i in `seq 1 14`; do sed -n "$i"p 2018-12-26.log; done文件系统 容量 已用 可用 已用% 挂载点/dev/mapper/centos-root 18G 6.2G 12G 36% /devtmpfs 898M 0 898M 0% /devtmpfs 910M 0 910M 0% /dev/shmtmpfs 910M 30M 881M 4% /runtmpfs 910M 0 910M 0% /sys/fs/cgroup/dev/sda1 497M 167M 331M 34% /boottmpfs 182M 0 182M 0% /run/user/0overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/mergedshm 64M 0 64M 0% /var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shmoverlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/mergedoverlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/mergedshm 64M 0 64M 0% /var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shmshm 64M 0 64M 0% /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm[root@centos-04 tmp]#
[root@centos-04 tmp]# while read line; do echo $line; done < 2018-12-26.log 文件系统 容量 已用 可用 已用% 挂载点/dev/mapper/centos-root 18G 6.2G 12G 36% /devtmpfs 898M 0 898M 0% /devtmpfs 910M 0 910M 0% /dev/shmtmpfs 910M 30M 881M 4% /runtmpfs 910M 0 910M 0% /sys/fs/cgroup/dev/sda1 497M 167M 331M 34% /boottmpfs 182M 0 182M 0% /run/user/0overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/mergedshm 64M 0 64M 0% /var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shmoverlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/mergedoverlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/mergedshm 64M 0 64M 0% /var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shmshm 64M 0 64M 0% /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 21.sh#!/bin/bashsum=0while read linedo line_n=`echo $line|sed 's/[^0-9]//g'|wc -L` echo $line_n sum=$[$sum+$line_n]done < $1echo "sum:$sum"[root@centos-04 tmp]# sh 21.sh 2018-12-26.log088898129535247474850sum:359[root@centos-04 tmp]# head -1 2018-12-26.log 文件系统 容量 已用 可用 已用% 挂载点[root@centos-04 tmp]# tail -1 2018-12-26.log shm 64M 0 64M 0% /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm[root@centos-04 tmp]#
对比文件差异
将文件做MD5
[root@centos-04 tmp]# md5sum 2018-12-26.log b11c7a1a9da1ab2b474ce5dea5e02fe1 2018-12-26.log[root@centos-04 tmp]#
EOF嵌入文档
第一步:传列表文件,第二步我们又写了一个脚本,第三步将脚本传到1.1.1.1机器
[root@centos-04 tmp]# cat > 2.txt << EOF> 1> 2> 3> EOF[root@centos-04 tmp]# cat 2.txt123[root@centos-04 tmp]#
#!/bin/bashdir=/data/web[ -f /tmp/md5.list ] && rm -f /tmp/md5.listfind $dir/ -type f > /tmp/file.listwhile read line do md5sum $line >> /tmp/md5.listdone < /tmp/file.listscp /tmp/md5.list B:/tmp/[ -f /tmp/check_md5.sh ] && rm -f /tmp/check_md5.shcat >/tmp/check_md5.sh << EOF#!/bin/bashdir=/data/webn=\`wc -l /tmp/md5.list|awk '{print \$1}'\`for i in \`seq 1 \$n\`do file_name=\`sed -n "\$i"p /tmp/md5.list |awk '{print \$1}'\` md5=\`sed -n "\$i"p /tmp/md5.list|awk '{print \$2}'\` if [ -f \$file_name ] then md5_b=\`md5sum \$file_name\` if [\$md5_b != \$md5 ] then echo "\$file_name changed." fi else echo "\$file_name lose." fidoneEOFscp /tmp/check_md5.sh B:/tmp/ssh B "/bin/bash /tmp/check_md5.sh"
检测网卡流量
sar命令,我们这里看第五列和第六列(8bit=1byte,100Mbit=12.5MByte/s)
[sun.yujun@kddi-zol-php-test-web1 routes]$ sar -n DEV 1 5Linux 2.6.32-696.18.7.el6.x86_64 (kddi-zol-php-test-web1.zoldc.com.cn) 01/14/2019 _x86_64_ (12 CPU)07:19:09 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s07:19:10 PM lo 5.00 5.00 0.50 0.50 0.00 0.00 0.0007:19:10 PM eth0 13.00 6.00 0.77 0.42 0.00 0.00 0.0007:19:10 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s07:19:11 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.0007:19:11 PM eth0 12.00 6.00 0.71 0.73 0.00 0.00 0.0007:19:11 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s07:19:12 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.0007:19:12 PM eth0 12.00 5.00 0.72 0.68 0.00 0.00 0.0007:19:12 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s07:19:13 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.0007:19:13 PM eth0 16.83 5.94 1.00 0.74 0.00 0.00 0.0007:19:13 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s07:19:14 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.0007:19:14 PM eth0 8.00 3.00 0.48 0.56 0.00 0.00 0.00Average: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/sAverage: lo 1.00 1.00 0.10 0.10 0.00 0.00 0.00Average: eth0 12.38 5.19 0.74 0.62 0.00 0.00 0.00[sun.yujun@kddi-zol-php-test-web1 routes]$ ^C[sun.yujun@kddi-zol-php-test-web1 routes]$
将脚本放到crontab里面,一分钟执行一次
[root@centos-04 tmp]# vim 23.sh#!/bin/bashlogdir=/tmp/sar_logfile=$logdir/`date +%d$H`.logt=`date +"%F %H:%M"`[ -d $logdir ] || mkdir -p $logdirLANG=ensar -n DEV 1 5 |grep eth0 |grep "Average" > /tmp/sar.tmpexec >>$fileecho "$t"awk '{print "input:",$5*8000"bps""\n""output:",$6*8000"bps"}' /tmp/sar.tmpecho "###################"[root@centos-04 tmp]# sh 23.sh
[root@centos-04 tmp]# ls /tmp/sar_log/15.log[root@centos-04 tmp]# cat /tmp/sar_log/15.log 2019-01-15 03:48input: 160bpsoutput: 80bps###################[root@centos-04 tmp]#
批量杀进程
[root@centos-04 tmp]# vim 24.sh#!/bin/bashfor pid in `ps aux|grep clearnen.sh|awk '{print $2}'`; do echo $pid; kill -9 $pid;done
判断web服务
[root@centos-04 tmp]# netstat -lntp|grep ':80 '|awk -F '/' '{print $NF}'nginx: master [root@centos-04 tmp]#
[root@centos-04 tmp]# lsof -i :80 |grep 'LISTEN'nginx 6958 root 11u IPv4 38456 0t0 TCP *:http (LISTEN)nginx 6959 nobody 11u IPv4 38456 0t0 TCP *:http (LISTEN)[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 25.sh#!/bin/bashn=`netstat -lntp |grep ':80 '|wc -l`if [ $n -eq 0 ]then echo "It not listen port 80"else ser=`netstat -lntp |grep ':80 '|awk -F '/' '{print $NF}'|sed 's/ //g'` echo "It is listenning port 80,and the service is $ser."fi[root@centos-04 tmp]# sh 25.sh It is listenning port 80,and the service is nginx:master.[root@centos-04 tmp]#
监控mysql服务
[root@centos-04 tmp]# mysql -uroot -p123456 -h172.17.0.2 -e "show processlist"+----+------+------------------+------+---------+------+-------+------------------+| Id | User | Host | db | Command | Time | State | Info |+----+------+------------------+------+---------+------+-------+------------------+| 3 | root | 172.17.0.1:50676 | NULL | Query | 0 | init | show processlist |+----+------+------------------+------+---------+------+-------+------------------+[root@centos-04 tmp]#
判断是否是从数据库
[root@centos-04 tmp]# mysql -uroot -p123456 -h172.17.0.2 -e "show slave status\G"[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 26.sh #!/bin/bashmysql = "/usr/local/mysql/bin/mysql -uroot -p123456"if ! $mysql -e "show processlist" >/dev/null 2>/dev/nullthen echo "MySql service is down." exitelse $mysql -e "show slave status\G" 2>/dev/null >/tmp/slave.stat n=`wc -l /tmp/slave.stat|awk '{print $1}'` if [ $n -eq 0 ] then echo "This is master." else echo "This is slave." egrep 'Slave_10_Runing:|Slave_SQL_Running:'/tmp/slave/.stat|awk -F ': ' '{print $2}' > /tmp/SQL.tmp if grep -qw "No" /tmp/SQL.tmp then echo "The slave is down." fi fifi
增删用户
[root@centos-04 tmp]# vim 27.sh #!/bin/bashif [ $# -eq 0 ] || [ $# -gt 2 ]then echo "Wrong, use bash $0 --add username, or bash $0 --del username or bash $0 --help" exitfiex_user(){ if ! id $1 2>/dev/null >/dev/null then useradd $1 && echo "$1 add successful." else echo $1 exist. fi}notex_user(){ if id $1 2>/dev/null >/dev/null then userdel $1 && echo "$1 delete successful." else echo $1 not exist. fi}case $1 in --add) if [ $# -eq 1 ] then echo "Wrong, use bash $0 --add user or bash $0 --add user1,user2,user3..." exit else n=`echo $2| awk -F ',' '{print NF}'` if [ $n -gt 1 ] then for i in `seq 1 $n` do username=`echo $2 |awk -v j=$i -F ',' '{print $j}'` ex_user $username done else ex_user $2 fi fi ;; --del) if [ $# -eq 1 ] then echo "Wrong, use bash $0 --del user or bash $0 --del user1,user2,user3..." exit else n=`echo $2| awk -F ',' '{print NF}'` if [ $n -gt 1 ] then for i in `seq 1 $n` do username=`echo $2 |awk -v j=$i -F ',' '{print $j}'` notex_user $username done else notex_user $2 fi fi ;; --help) if [ $# -ne 1 ] then echo "Wrong, use bash $0 --help" exit else echo "Use bash $0 --add username or bash $0 --add user1,user2,user3... add user." echo " bash $0 --del username -r bash $0 --del user1,user2,user3... delete user." echo " bash $0 --help print this info." fi ;; *) echo "Wrong, use bash $0 --add username, or bash $0 --del username or bash $0 --help" ;;esac
计算和
#!/bin/bashsum=0for i in `seq 1 100`do j=$[$i%3] if [ $j -eq 0 ] then sum=$[$sum+$i] fidoneecho $sum
加减乘除
[root@centos-04 tmp]# vim 29.sh#!/bin/bashis_nu(){ n=`echo $1 |sed 's/[0-9]//g'` if [ -n "$n" ] then echo "给出的参数必须是正整数" exit fi}if [ $# -ne 2 ]then echo "必须要输入两个参数" exitelse is_nu $1 is_nu $2fibig(){ if [ $1 -gt $2 ] then echo $1 else echo $2 fi}small(){ if [ $1 -lt $2 ] then echo $1 else echo $2 fi}add(){ sum=$[$1+$2] echo "$1+$2=$sum"}jian(){ b=`big $1 $2` s=`small $1 $2` cha=$[$b-$s] echo "$b-$s=$cha"}cheng(){ ji=$[$1*$2] echo "$1x$2=$ji"}chu(){ b=`big $1 $2` s=`small $1 $2` v=`echo "scale=2;$b/$s"|bc` echo "$b/$s=$v"}add $1 $2jian $1 $2cheng $1 $2chu $1 $2
输入数字
[root@centos-04 tmp]# vim 30.sh#!/bin/bashwhile :do read -p "请输入一个数字:" n if [ -z "$n" ] then echo "请输入一个纯数字。" continue fi if echo $n |grep -qi 'end' then exit fi n1=`echo $n|sed 's/[0-9]//g'` if [ -n "$n1" ] then echo "请输入一个纯数字" continue else echo "你输入的数字是:$n" continue fidone
获取网卡ip
[root@centos-04 tmp]# ip add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}'loens33docker0vethd17e886@if4[root@centos-04 tmp]#
[root@centos-04 tmp]# ip add show dev ens33 |grep ' inet '|awk '{print $2}'192.168.242.130/24[root@centos-04 tmp]#
[root@centos-04 tmp]# ip add show dev ens33 |grep ' inet '|awk '{print $2}'|awk -F '/' '{print $1}'192.168.242.130[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 31.sh#!/bin/baship add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}' > /tmp/eth.listwhile :do eths=`cat /tmp/eth.list |xargs` read -p "Please input a if name(The eths is `echo -e "\033[31m$eths\033[0m"`): " eth if [ -z "$eth" ] then echo "Please input a if name." continue fi if ! grep -qw "$eth" /tmp/eth.list then echo "The if name is error." continue else break fidoneif_ip() { ip add show dev $1 |grep ' inet ' |awk '{print $2}'|awk -F '/' '{print $1}' >/tmp/$1.txt n=`wc -l /tmp/$1.txt|awk '{print $1}'` if [ $n -eq 0 ] then echo "There is no ip address on the eth." else echo "The ip addreess is:" for ip in `cat /tmp/$1.txt` do echo -e "\033[33m$ip\033[0m" done fi}if_ip $eth
列出目录内容
$@指的是1.sh 后面的一堆参数1 2 3 4 a b,$#表示所有参数的个数
[root@centos-04 tmp]# sh 1.sh 1 2 3 4 a b
[root@centos-04 tmp]# vim 32.sh #!/bin/bashif [ $# -eq 0 ]then ls -l .else for d in $@ do echo "There are these directorys in $d:" find $d -type d donefi[root@centos-04 tmp]# sh 32.sh /tmp/ /usr/local/There are these directorys in /tmp/:/tmp//tmp/.XIM-unix/tmp/.font-unix/tmp/.X11-unix/tmp/.ICE-unix/tmp/.Test-unixThere are these directorys in /usr/local/:/usr/local//usr/local/bin/usr/local/etc/usr/local/games
[root@centos-04 tmp]# sh 32.sh总用量 32-rw-r--r-- 1 root root 1771 1月 19 00:06 27.sh-rw-r--r-- 1 root root 122 1月 19 00:28 28.sh-rw-r--r-- 1 root root 742 1月 19 00:58 29.sh-rw-r--r-- 1 root root 343 1月 19 01:06 30.sh-rw-r--r-- 1 root root 765 1月 19 02:39 31.sh-rw-r--r-- 1 root root 134 1月 28 22:34 32.sh-rw-r--r-- 1 root root 33 1月 19 02:32 eth.list-rw-r--r-- 1 root root 10 1月 19 02:32 lo.txt[root@centos-04 tmp]#
(优化版) #!/bin/bashif [ $# -eq 0 ]then echo "当前目录下的文件是:" ls -l .else for d in $@ do if [ -d $d ] then echo "There are these directorys in $d:" find $d -type d else echo "并没有该目录:$d" fi donefi
下载文件
[root@centos-04 tmp]# vim 33.sh#!/bin/bashif [ $# -ne 2 ]then echo "你必须要输入两个参数,第一个参数是网址,第二个参数是目录。" exit 1fiif [ ! -d $2 ]then while : do echo "你输入的第二个参数并不是一个存在的目录,是否要创建该目录呢?(y|n):"c case $c in y|Y) mkdir -p $2 ;; n|N) exit 51 ;; *) echo "请输入y或n" continue ;; esac doneelse cd $2 wget $1 if [ $? -eq 0 ] then exit 0 else echo "下载失败" exit 52 fifi
猜数字
1.返回随机数
[root@centos-04 tmp]# echo $RANDOM15845[root@centos-04 tmp]#
[root@centos-04 tmp]# echo $[$RANDOM%101] (取0-100的数)77[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 34.sh #!/bin/bashn=$[$RANDOM%101]while :do read -p "请输入一个0-100的数字:" n1
if [ -z "$n1" ]
then echo "必须输入一个数字" continue fin2=`echo $n1 |sed 's/[0-9]//g'` if [ -n "$n2" ] then echo "你输入的数字并不是正整数。" continue else if [ $n -gt $n1 ] then echo "你输入的数字小了" continue elif [ $n -lt $n1 ] then echo "你输入的数字大了" continue else echo "恭喜你" break fi fidone[root@centos-04 tmp]# sh 34.sh 请输入一个0-100的数字:10你输入的数字小了请输入一个0-100的数字:20你输入的数字小了请输入一个0-100的数字:50你输入的数字小了请输入一个0-100的数字:90你输入的数字大了请输入一个0-100的数字:60你输入的数字小了请输入一个0-100的数字:70你输入的数字小了请输入一个0-100的数字:80你输入的数字小了请输入一个0-100的数字:85你输入的数字大了请输入一个0-100的数字:83你输入的数字小了请输入一个0-100的数字:84恭喜你[root@centos-04 tmp]#
根据名字得数字
[root@centos-04 tmp]# vim 35.sh#!/bin/bashf=/tmp/user_number.txtwhile :do read -p "Please input a username:" u u1=`echo $u|sed 's/[a-zA-Z0-9]//g'` if [ -n "$u1" ] then echo "你输入的用户名不符合规范,正确的用户名应该是大小写字母和数字的组合" continue else if [ -f $f ] then u_n=`awk -v uu=$u '$1==uu {print $2}' $f` if [ -n "$u_n" ] then echo "用户$u对应的数字是:$u_n" else n=$[$RANDOM%100] echo "用户$u对应的数字是:$n" echo $u $n >> $f fi else n=$[$RANDOM%100] echo "用户$u对应的数字是:$n" echo $u $n >> $f fi fidone[root@centos-04 tmp]# sh 35.sh Please input a username:user1用户user1对应的数字是:66Please input a username:user2用户user2对应的数字是:45Please input a username:
根据名字得数字优化
[root@centos-04 tmp]# vim 35.sh#!/bin/bashf=/tmp/user_number.txtjude_n(){#!/bin/bashf=/tmp/user_number.txtj_n(){ while : do n=$[RANDOM%100] if awk '{print $2}' $f|grep -qw $n then continue else break n=$[$RANDOM%100] n=$[$RANDOM%100] if awk '{print $2} $f|grep -qw $n' fi done}while :do read -p "Please input a username:" u if [ -z "$u" ] then echo "请输入用户名" continue fi if [ $u == "q" ] || [ $u=="Q" ] then exit fi u1=`echo $u|sed 's/[a-zA-Z0-9]//g'` if [ -n "$u1" ] then echo "你输入的用户名不符合规范,正确的用户名应该是大小写字母和数字的组合" continue else if [ -f $f ] then u_n=`awk -v uu=$u '$1==uu {print $2}' $f` if [ -n "$u_n" ] then echo "用户$u对应的数字是:$u_n" else j_n echo "用户$u对应的数字是:$n" echo $u $n >> $f fi else j_n echo "用户$u对应的数字是:$n" echo $u $n >> $f fi fidone
一个数字的行
[root@centos-04 tmp]# echo "aaaaa1bbbbb" |sed "s/[^0-9]//g"1[root@centos-04 tmp]# echo "aaaaa1bbbbb" |sed "s/[^0-9]//g" |wc -L1[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 1.txt 222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333222222222222fasf333[root@centos-04 tmp]# vim 36.sh #!/bin/bashwhile read linedo n=`echo $line |sed 's/[^0-9]//g'|wc -L` if [ $n -eq 1 ] then echo $line fidone < 1.txt
日志切割归档
想要实现的效果,当有日志文件1.log.5时把1.log.5文件删除,将1.log.4文件移到为1.log.5,以此类推。
#!/bin/bashcd /data/logs#rm 1.log.5#mv 1.log.4 1.log.5#mv 1.log.3 1.log.4#mv 1.log.2 1.log.3#mv 1.log.1 1.log.2#mv 1.log 1.log.1
[root@centos-04 tmp]# vim 37.sh#!/bin/bashcd /data/logslog=1.logmv_log(){ [ -f $1 ] && mv $1 $2}[ -f $log.5 ] && rm -f $log.5for i in `seq 4 -1 1`do j=$[$i+1] mv_log $log.$i $log.$jdonemv 1.log 1.log.1#rm 1.log.5#mv 1.log.4 1.log.5#mv 1.log.3 1.log.4#mv 1.log.2 1.log.3#mv 1.log.1 1.log.2#mv 1.log 1.log.1
[root@centos-04 tmp]# cd /data/logs/ [root@centos-04 logs]# touch 1.log[root@centos-04 logs]# echo "111" > 1.log[root@centos-04 logs]# cat 1.log 111[root@centos-04 logs]# cd /tmp/[root@centos-04 tmp]# sh 37.sh [root@centos-04 tmp]# cd /data/logs/[root@centos-04 logs]# ls1.log.1[root@centos-04 logs]# cat 1.log.1 111[root@centos-04 logs]# touch 1.log[root@centos-04 logs]# echo '000' > 1.log[root@centos-04 logs]# cd /tmp/[root@centos-04 tmp]# sh 37.sh [root@centos-04 tmp]# cd /data/logs/[root@centos-04 logs]# ls1.log.11.log.2[root@centos-04 logs]# cat 1.log.1000[root@centos-04 logs]# cat 1.log.2111[root@centos-04 logs]#
查找在线IP
[root@centos-04 tmp]# vim 38.sh#!/bin/bashfor i in `seq 1 254`do if ping -c 2 -W 2 10.19.37.$i > /dev/null 2>/dev/null then echo "10.19.37.$i 是通的。" else echo "10.19.37.$i 不通。" fidone
检查脚本错误
1.-n 检查脚本错误
2.演示语法错误,我们故意给for前加一个i
[root@centos-04 tmp]# vim 38.sh #!/bin/bashifor i in `seq 1 254`do if ping -c 2 -W 2 10.19.37.$i > /dev/null 2>/dev/null then echo "10.19.37.$i 是通的。" else echo "10.19.37.$i 不通。" fidone
[root@centos-04 tmp]# sh -n 38.sh38.sh:行3: 未预期的符号 `do' 附近有语法错误38.sh:行3: `do'[root@centos-04 tmp]# [root@centos-04 tmp]# sh -n 38.sh > /tmp/1.txt 2> /tmp/2.txt[root@centos-04 tmp]# echo $?2[root@centos-04 tmp]# [root@centos-04 tmp]# cat /tmp/2.txt 38.sh:行3: 未预期的符号 `do' 附近有语法错误38.sh:行3: `do'[root@centos-04 tmp]#
[root@centos-04 tmp]# sh 39.sh 1.shsh: 1.sh: 没有那个文件或目录请输入q|Q退出脚本。q[root@centos-04 tmp]# ls1.txt 31.sh 33.sh 35.sh 37.sh 39.sh proxy.log user_number.txt2.txt 32.sh 34.sh 36.sh 38.sh 456.log sh.err[root@centos-04 tmp]# sh 39.sh 31.sh 脚本31.sh没有语法错误。[root@centos-04 tmp]# sh 39.sh 38.sh 38.sh:行3: 未预期的符号 `do' 附近有语法错误38.sh:行3: `do'请输入q|Q退出脚本。#!/bin/bashifor i in `seq 1 254`do if ping -c 2 -W 2 10.19.37.$i > /dev/null 2>/dev/null then echo "10.19.37.$i 是通的。" else echo "10.19.37.$i 不通。" fidone "38.sh" 10L, 178C 已写入[root@centos-04 tmp]#
格式化数字
给每个数字前面添加空格s代表替换,点当前数字&点号代表的数字。
[root@centos-04 tmp]# echo "1234"|sed 's/./& /g'1 2 3 4 [root@centos-04 tmp]#
[root@centos-04 tmp]# vim 40.sh #!/bin/bashn=`echo $1|wc -L`for d in `echo $1|sed 's/./& /g'`do n2=$[$n%3] if [ $n2 -eq 0 ] then echo -n ",$d" else echo -n "$d" fi n=$[$n-1]done |sed 's/^,//'echo
[root@centos-04 tmp]# sh 40.sh 1234512,345[root@centos-04 tmp]# sh 40.sh 123123[root@centos-04 tmp]# sh 40.sh 12355551,235,555
问候脚本
获取10小时之前的时间
[root@centos-04 tmp]# d=`date -d "-10 hours" +%H`[root@centos-04 tmp]# echo $d17
[root@centos-04 tmp]# vim 41.sh #!/bin/bashd=`date +%H`if [ $d -ge 0 -a $d -lt 7 ]then tag=1elif [ $d -ge 7 -a $d -lt 12 ]then tag=2elif [ $d -ge 12 -a $d -lt 18 ]then tag=3else tag=4ficase $tag in 1) echo "早上好" ;; 2) echo "上午好" ;; 3) echo "下午好" ;; 4) echo "晚上好" ;; *) echo "脚本出错了" ;; esac ~ ~ "41.sh" 32L, 333C 已写入 [root@centos-04 tmp]# [root@centos-04 tmp]# sh 41.sh 早上好[root@centos-04 tmp]#
菜单脚本
[root@centos-04 tmp]# vim 42.sh#!/bin/bashPS3="Please input your choice:" 为了去掉结果中的#?select i in w ls pwd quitdo case $i in w) w ;; ls) ls ;; pwd) pwd ;; quit) exit ;; *) echo "please input 1-3." ;; esacdone "42.sh" [新] 21L, 179C 已写入 [root@centos-04 tmp]# sh 440.sh 41.sh 42.sh 456.log [root@centos-04 tmp]# sh 42.sh 1) w2) ls3) pwd4) quit#? 1 03:48:09 up 4 days, 8:11, 1 user, load average: 0.00, 0.01, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.242.1 21:44 1.00s 0.26s 0.01s w#? 21.txt 31.sh 33.sh 35.sh 37.sh 39.sh 41.sh 456.log sh.err2.txt 32.sh 34.sh 36.sh 38.sh 40.sh 42.sh proxy.log user_number.txt#? 3/tmp#? 4[root@centos-04 tmp]#
[root@centos-04 tmp]# echo -e "1) w\n2) ls\n3) pwd\n4)"1) w2) ls3) pwd4)[root@centos-04 tmp]# [root@centos-04 tmp]# vim 42-2.sh #!/bin/bashecho -e "1) w\n2) ls\n3) pwd\n4) quit"while :doread -p "Please input your choice(1-4):" ccase $c in 1) w ;; 2) ls ;; 3) pwd ;; 4) exit ;; *) echo "Please input 1-4" ;;esacdone "42-2.sh" 23L, 219C 已写入 [root@centos-04 tmp]# sh 42-2.sh 1) w2) ls3) pwd4) quitPlease input your choice(1-4):1 04:42:46 up 4 days, 9:06, 1 user, load average: 0.00, 0.01, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/0 192.168.242.1 21:44 6.00s 0.27s 0.00s wPlease input your choice(1-4):21.txt 31.sh 33.sh 35.sh 37.sh 39.sh 41.sh 42.sh proxy.log user_number.txt2.txt 32.sh 34.sh 36.sh 38.sh 40.sh 42-2.sh 456.log sh.errPlease input your choice(1-4):Please input 1-4Please input your choice(1-4):3/tmpPlease input your choice(1-4):4[root@centos-04 tmp]#
检查用户是否登录
思路:通过w命令获取第一列的用户
[root@centos-04 tmp]# vim 43.sh#!/bin/bashwhile :do if w|sed '1'd|awk '{print $1}'|grep -qw "$1" then echo "用户$1已经登录系统" exit fi sleep 300done [root@centos-04 tmp]# sh 43.sh root用户root已经登录系统[root@centos-04 tmp]#
检查系统是否入侵
$0 当前脚本的文件名$n 传递给脚本或函数的参数。n 是一个数字,表示第几个参数。例如,第一个参数是$1,第二个参数是$2。$# 传递给脚本或函数的参数个数。$* 传递给脚本或函数的所有参数。$@ 传递给脚本或函数的所有参数。被双引号(" ")包含时,与 $* 稍有不同,下面将会讲到。$? 上个命令的退出状态,或函数的返回值。$$ 当前Shell进程ID。对于 Shell 脚本,就是这些脚本所在的进程ID。
有时候,你会想手动跟踪命令的输出内容,同时又想将输出的内容写入文件
tee
命令基于标准输入读取数据,标准输出或文件写入数据
[root@centos-04 tmp]# vim 44.sh #!/bin/bashpp=$$ps -elf |sed '1'd > /tmp/pid.txtfor pid in `awk -v ppn=$pp '$5!=ppn {print $4}' /tmp/pid.txt`do if ! [ -d /proc/$pid ] then echo "系统中并没有pid为$pid的目录,需要检查。" fidone[root@centos-04 tmp]# sh 44.sh
三行并一行
[root@centos-04 tmp]# vim 45.sh #!/bin/bashn=1cat $1 |while read linedo n1=$[$n%3] if [ $n1 -eq 0 ] then echo "$line" else echo -n "$line" fi n=$[$n+1]done[root@centos-04 tmp]# sh 45.sh 1.txt 12345678 3333[root@centos-04 tmp]#
网卡和ip
~就是表示用来匹配后面的正则表达式,告诉awk后面开始是正则语法。
NF 表示的是浏览记录的元素的个数
$NF 表示的最后一个Field(列),即输出最后一个字段的内容[root@localhost SHELL]# free -m | grep buffers\/-/+ buffers/cache: 1815 1859[root@localhost SHELL]# free -m | grep buffers\/ | awk '{print $NF}'1859[root@localhost SHELL]# free -m | grep buffers\/ | awk '{print NF}'4[root@localhost SHELL]#
[root@centos-04 tmp]# ip add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}'loens33[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 46.sh #!/bin/baship add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}' > /tmp/ifs.txtget_ip(){ ip add show dev $1 |grep inet |awk '{print $2}' |awk -F '/' '{print $1}'}for eth in `cat /tmp/ifs.txt`do myip=`get_ip $eth` if [ -z "$myip" ] then echo $eth else echo $eth $myip fidone > /tmp/if_ip.txtif [ $# -ne 2 ]then echo "请输入正确的格式: bash $0 -i 网卡 或者 bash $0 -I ip" exitfiif [ $1 == "-i" ]then if awk '{print $1}' /tmp/if_ip.txt |grep -qw $2 then eth=$2 ip1=`awk -v aeth=$eth '$1==aeth' /tmp/if_ip.txt|sed "s/$eth //"` echo "网卡$2的ip是 $ip1" else echo "你指定的网卡不对,系统中的网卡有:`cat /tmp/ifs.txt|xargs`" exit fielif [ $1 == "-I" ]then if grep -qw " $2 " /tmp/if_ip.txt then eth=`grep -w " $2 " /tmp/if_ip.txt|awk '{print $1}'` echo "IP $2对应的网卡是$eth" else echo "你指定的ip不对,系统中的IP有:`ip add |grep inet |awk '{print $2}'|awk -F '/' '{print $1}'|xargs`" exit fielse echo "请输入正确的格式: bash $0 -i 网卡 或者 bash $0 -I ip"fi
[root@centos-04 tmp]# sh 46.sh 请输入正确的格式: bash 46.sh -i 网卡 或者 bash 46.sh -I ip[root@centos-04 tmp]# sh 46.sh -i eth33你指定的网卡不对,系统中的网卡有:lo ens33[root@centos-04 tmp]# sh 46.sh -i ens33网卡ens33的ip是 192.168.242.130 fe80::6244:d336:eacf:c4d6[root@centos-04 tmp]# sh 46.sh -i lo网卡lo的ip是 127.0.0.1 ::1[root@centos-04 tmp]# sh 46.sh -I 192.168.242.130你指定的ip不对,系统中的IP有:127.0.0.1 ::1 192.168.242.130 fe80::6244:d336:eacf:c4d6[root@centos-04 tmp]#
随机3位数
[root@centos-04 tmp]# echo $RANDOM26768[root@centos-04 tmp]# echo $[$RANDOM%10]6[root@centos-04 tmp]# echo $[$RANDOM%10]8[root@centos-04 tmp]# for i in `seq 0 2`; do a[$i]=$[$RANDOM%10]; done; echo ${a[@]}|sed s'/ //g'203[root@centos-04 tmp]#
把数组按字符串输出
echo ${a[@]}
[root@centos-04 tmp]#vim 47.sh#!/bin/bashget_number(){ for i in `seq 0 2` do a[$i]=$[$RANDOM%10] done echo ${a[@]}|sed s'/ //g'}if [ $# -eq 0 ]then get_numberelif [ $# -eq 1 ]then n=`echo $1|sed 's/[0-9]//g'` if [ -n "$n" ] then echo "给定的参数必须是一个数字" exit fi for i in `seq 1 $1` do get_number done |xargselse echo "格式不对,正确的格式是sh $0 [n],这里的n是一个数字。"fi~ ~ ~ ~ ~ "47.sh" 28L, 414C 已写入 [root@centos-04 tmp]# sh 47.sh 845[root@centos-04 tmp]# sh 47.sh 10898 422 695 369 417 402 573 800 957 614[root@centos-04 tmp]#
是否安装包
查看是否安装了包,未安装的包echo $? 会输出1,安装的包输出0
[root@centos-04 tmp]# rpm -q httpdhttpd-2.4.6-88.el7.centos.x86_64[root@centos-04 tmp]# yum list|grep httpdRepodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fasthttpd.x86_64 2.4.6-88.el7.centos @base
[root@centos-04 tmp]# rpm -q mysql未安装软件包 mysql [root@centos-04 tmp]# echo $?1[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 48.sh#!/bin/bashif_install(){ rpm -q $1 >/dev/null 2>/dev/null if [ $? -eq 0 ] then echo "$1已经安装" return 0 else echo "$1没有安装" return 1 fi}if_install httpdif [ $? -eq 0 ]then if ! pgrep httpd >/dev/null then service httpd start fielse yum install -y httpdfiif_install mysql-serverif [ $? -eq 0 ]then if ! pgrep mysqld >/dev/null then service mysqld start fielse yum install -y mysql-serverfi
[root@centos-04 tmp]# sh 48.shhttpd已经安装Redirecting to /bin/systemctl start httpd.serviceJob for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.mysql-server没有安装yum install -y mysql-server[root@centos-04 tmp]#
日期是否合法
查看日期是否在指定的月中
[root@centos-04 tmp]# cal 02 2010 二月 2010 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728[root@centos-04 tmp]#
[root@centos-04 tmp]# cal 02 2010 二月 2010 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 1314 15 16 17 18 19 2021 22 23 24 25 26 2728[root@centos-04 tmp]# cal 02 2010|grep -w 2828[root@centos-04 tmp]# cal 02 2010|grep -w 30[root@centos-04 tmp]#
[root@centos-04 php_client]# y=2012; m=02; d=30[root@centos-04 php_client]# cal $m $y 二月 2012 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 1112 13 14 15 16 17 1819 20 21 22 23 24 2526 27 28 29[root@centos-04 php_client]# cal $m $y|grep -w $d (grep发现没有东西,那说明我们的日期不合法)[root@centos-04 php_client]#
[root@centos-04 /]# a=20180418[root@centos-04 /]# echo ${a:0:4}(取变量的前四个字符)2018[root@centos-04 /]#
${#1} (获取参数的长度,$1的长度)
[root@centos-04 /]# echo '2018'|grep -q "^0" [root@centos-04 /]# echo $?1[root@centos-04 /]# echo '0018'|grep -q "^0" [root@centos-04 /]# echo $?0[root@centos-04 /]#
[root@centos-04 /]# vim 49.sh #!/bin/bashif [ $# -ne 1 ] || [ ${#1} -ne 8 ]then echo 'please input sh $0 yyyymmdd' exit 1fiy=`echo ${1:0:4}`m=`echo ${1:4:2}`d=`echo ${1:6:2}`if echo $d|grep -q "^0"then d=`echo ${1:6:2}|sed 's/^0//'`fiif cal $m $y > /dev/null 2>/dev/nullthen if ! cal $m $y |grep -qw "$d" then echo "你给的日期是不合法的" else echo "日期合法" fielse echo "你给的日期不合法"fi[root@centos-04 /]# sh 49.sh 20190101日期合法[root@centos-04 /]# sh 49.sh 20190131日期合法[root@centos-04 /]# sh 49.sh 20190132你给的日期是不合法的[root@centos-04 /]#
监控流量
检查网卡流量
[root@centos-04 /]# sar -n DEV 1 5 |grep ens33 01时42分32秒 ens33 1.01 0.00 0.06 0.00 0.00 0.00 0.0001时42分33秒 ens33 1.01 1.01 0.06 0.22 0.00 0.00 0.0001时42分34秒 ens33 0.99 0.99 0.06 0.21 0.00 0.00 0.0001时42分35秒 ens33 1.01 1.01 0.06 0.22 0.00 0.00 0.0001时42分36秒 ens33 1.00 1.00 0.06 0.21 0.00 0.00 0.00平均时间: ens33 1.00 0.80 0.06 0.17 0.00 0.00 0.00[root@centos-04 /]#
▽root@centos-04 /]# vim 50.sh #!/bin/bashLANG=ensar -n DEV 1 10|grep -w "$1" > /tmp/sar.tmpin=`grep "Average:" /tmp/sar.tmp|awk '{print $5}'|sed 's/\.//'`out=`grep "Average:" /tmp/sar.tmp|awk '{print $6}'|sed 's/\.//'`if [ $in == "000" ] && [ $out == '000']then ifdown $1 ifup $1fi[root@centos-04 /]# sh 50.sh ens33
判断网站是否正常
[root@centos-04 /]# curl -I www.baidu.com 2>/dev/null |head -1 (加上将错误输出到null里,不加会输出下面的红色部分)HTTP/1.1 200 OK[root@centos-04 /]# curl -I www.baidu.com |head -1 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 277 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0HTTP/1.1 200 OK[root@centos-04 /]#
[root@centos-04 /]# curl -I www.baidu.com 2>/dev/null |head -1|awk '{print $2}'200[root@centos-04 /]#
[root@centos-04 tmp]# vim 50.sh #!/bin/bash#这个脚本用来判断一个网址是否正常url='http://www.baidu.com'mail_user=sun.yujun@zol.com.cncode=`curl -I $url 2>/tmp/curl.err|head -1|awk '{print $2}'`if [ -z $code ]then python mail.py $mail_user "$url访问异常" "`cat /tmp/curl.err`" exitelif [ $code != "200" ]then curl -I $url &>/tmp/curl.log python mail.py $mail_user "$url访问异常 状态码$code" "`/tmp/curl.log`"fi[root@centos-04 tmp]# sh -x 50.sh + url=http://www.baidu.com+ mail_user=sun.yujun@zol.com.cn++ curl -I http://www.baidu.com++ head -1++ awk '{print $2}'+ code=200+ '[' -z 200 ']'+ '[' 200 '!=' 200 ']'[root@centos-04 tmp]#
这里发邮件需要把mail.py文件放上
小于5k文件打包
$HOME变量可以代替用户的家目录,当前登录的是哪个用户变量就指向这个用户的家目录。
[root@centos-04 tmp]# date +%F2019-03-10[root@centos-04 tmp]#
[root@centos-04 tmp]# tar zcvf 2018-02-02.tar.gz 1.txt 2.txt^C[root@centos-04 tmp]# find $HOME/ -type f -size -5k |xargs (xargs命令可以将打印结果放到一行)
root@centos-04 tmp]# vim 52.sh#!/bin/basht=`date +%F`cd $HOMEtar czf $t.tar.gz `find ./ -type f -size -5k|xargs`[root@centos-04 tmp]# sh -x 52.sh ++ date +%F+ t=2019-03-10+ cd /root++ find ./ -type f -size -5k++ xargs+ tar czf 2019-03-10.tar.gz ./.bash_logout ./.bash_profile[root@centos-04 home]# cd ~[root@centos-04 ~]# ls2019-03-10.tar.gz clouddemo.py [root@centos-04 ~]# tar -tf 2019-03-10.tar.gz (-tf查看压缩包里的文件列表)
监控22端口是否被封
[root@centos-04 ~]# iptables -nvL INPUTChain INPUT (policy ACCEPT 123K packets, 40M bytes) pkts bytes target prot opt in out source destination [root@centos-04 ~]# iptables -I INPUT -p tcp --dport 22 -s 1.1.1.1 -j DROP[root@centos-04 ~]# iptables -I INPUT -p tcp --dport 22 -s 1.1.1.2 -j REJECT [root@centos-04 ~]# iptables -nvL INPUTChain INPUT (policy ACCEPT 9 packets, 568 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT tcp -- * * 1.1.1.2 0.0.0.0/0 tcp dpt:22 reject-with icmp-port-unreachable 0 0 DROP tcp -- * * 1.1.1.1 0.0.0.0/0 tcp dpt:22[root@centos-04 ~]#
[root@centos-04 tmp]# iptables -nvL INPUT |grep -w 'dpt:22' (或者' dpt:22 ') 0 0 REJECT tcp -- * * 1.1.1.2 0.0.0.0/0 tcp dpt:22 reject-with icmp-port-unreachable 0 0 DROP tcp -- * * 1.1.1.1 0.0.0.0/0 tcp dpt:22[root@centos-04 tmp]#
[root@centos-04 tmp]# iptables -nvL INPUT |grep -w 'dpt:22' |awk '$3 ~/REJECT|DROP/' (正则匹配第三段的REJECT|DROP) 0 0 REJECT tcp -- * * 1.1.1.2 0.0.0.0/0 tcp dpt:22 reject-with icmp-port-unreachable 0 0 DROP tcp -- * * 1.1.1.1 0.0.0.0/0 tcp dpt:22[root@centos-04 tmp]#
[root@centos-04 tmp]# iptables -nvL INPUT --line-numbers |grep -w 'dpt:22' |awk '$4 ~/REJECT|DROP/ {print $1}' 12[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 53.sh #!/bin/bashiptables -nvL INPUT --line-numbers |grep -w 'dpt:22' |awk '$4 ~/REJECT|DROP/ {print $1}' > /tmp/iptables.logn=`cat /tmp/iptables.log |wc -l`if [ $n -gt 0 ]then for n in `tac /tmp/iptables.log` do iptables -D INPUT $n donefi[root@centos-04 tmp]# sh -x 53.sh + iptables -nvL INPUT --line-numbers+ grep -w dpt:22+ awk '$4 ~/REJECT|DROP/ {print $1}'++ cat /tmp/iptables.log++ wc -l+ n=2+ '[' 2 -gt 0 ']'++ tac /tmp/iptables.log+ for n in '`tac /tmp/iptables.log`'+ iptables -D INPUT 2+ for n in '`tac /tmp/iptables.log`'+ iptables -D INPUT 1[root@centos-04 tmp]# iptables -nvL INPUT --line-numbers |grep -w 'dpt:22' |awk '$4 ~/REJECT|DROP/ {print $1}'[root@centos-04 tmp]#
分析日志
查看date函数用法
[root@centos-04 logs]# man date
[root@centos-04 logs]# 28/Jul/2018:08:04:10^C[root@centos-04 logs]# LANG=en[root@centos-04 logs]# date +%d/%b/%Y:%T11/Mar/2019:04:30:44[root@centos-04 logs]#
核心命令
[root@centos-04 logs]# egrep "date +%d/%b/%Y:1[01]:[0-5]:[0-9]:" /usr/local/nginx/logs/access.log |awk '{print $1}' |sort -n |uniq -c |sort -n |tail -1 |awk '{print $2}'
[root@centos-04 tmp]# vim 54.sh #!/bin/bash#这个脚本用来分析Nginx访问日志#作者:SYJ#日期:2019-03-22export LANG=enlog="/usr/local/nginx/logs/access.log"t=`date +%d/%b/%Y:1[01]:[0-5][0-9]:`egrep "$t" $log |awk '{print $1}' |sort -n |uniq -c |sort -n |tail -1 |awk '{print $2}'
打印数字
[root@centos-04 tmp]# read -p "please: " nplease: 4[root@centos-04 tmp]# echo $n4[root@centos-04 tmp]#
[root@centos-04 tmp]# for i in `seq 1 $n`; do echo $i; done1234[root@centos-04 tmp]#
[root@centos-04 tmp]# echo $n4[root@centos-04 tmp]# echo $n |sed 's/[0-9]//g'(判断变量中是否全是数字,将数字全都替换成空,如果还有值说明不全是数字)[root@centos-04 tmp]# n=a[root@centos-04 tmp]# echo $n |sed 's/[0-9]//g'a[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 55.sh #!/bin/bash#这个脚本用来打印数字#作者:SYJ#日期:2019-03-22read -p "Please input a number: " nn1=`echo $n |sed 's/[0-9]//g'`if [ -n "$n1" ]then echo "Please input a number." exitfifor i in `seq 1 $n`do echo $idoneread -p "If continue? y/n" ccase $c in n|N) exit ;; y|Y) read -p "Please input a number: " n2 n3=`echo $n2 |sed 's/[0-9]//g'` if [ -n "$n3" ] then echo "Please input a number." exit fi if [ $n2 -le $n ] then echo "$n2 should grater than $n." exit fi for i in `seq $[$n+1] $n2` do echo $i done ;; *) echo "Please input y or n." ;;esac
[root@centos-04 tmp]# sh 55.sh Please input a number: 512345If continue? y/nyPlease input a number: 8678[root@centos-04 tmp]#
给文件增加内容
创建一个文件写入1、2、3、4、5、6
[root@centos-04 tmp]# vi 1.txt123456
在第五列后面添加文本123456789
[root@centos-04 tmp]# sed '5a123456789' 1.txt123451234567896
添加\n就是回车换行加上-i参数直接修改文件
[root@centos-04 tmp]# sed -i '5a12345\n6789' 1.txt 123451234567896[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 56.sh #!/bin/bash#这个脚本用来为文件增加行#作者:star#日期:2019-03-25n=0cat 1.txt |while read linedo n=$[$n+1] if [ $n -eq 5 ] then echo $line echo -e "# This is a test file.\n# Test insert line into this file." else echo $line fidone[root@centos-04 tmp]# sh 56.sh 12345# This is a test file.# Test insert line into this file.6[root@centos-04 tmp]#
备份etc目录
[root@centos-04 tmp]# date +%d11[root@centos-04 tmp]# date +%y19[root@centos-04 tmp]# date +%Y2019[root@centos-04 tmp]# date +%y%m%d190311[root@centos-04 tmp]# date +%Y%m%d20190311[root@centos-04 tmp]#
[root@centos-04 tmp]# date +%F2019-03-11[root@centos-04 tmp]#
自动添加shell脚本注释
[root@centos-04 tmp]# vim add_title.sh#!/bin/bashd=`date +%F`cat >$1<
[root@centos-04 tmp]# vim 57.sh #!/bin/bash#这个脚本用来备份/etc/目录#作者:SYJ#日期:2019-03-11d1=`date +%d`d2=`date +%y%m%d`if [ $d1 == "01" ]then cd /etc/ tar -czf "/root/bak/${d2}_etc.tar.gz" ./fi
[root@centos-04 tmp]# sh 57.sh
[root@centos-04 tmp]# cd /root/bak/[root@centos-04 bak]# ls190301_etc.tar.gz[root@centos-04 bak]#
找出重复的单词
将非字母的字符全部替换成空格
[root@centos-04 tmp]# cat /etc/passwd |sed 's/[^a-zA-Z]/ /g'root x root root bin bashbin x bin bin sbin nologin
将字母放到一列
[root@centos-04 tmp]# for w in `cat /etc/passwd|sed 's/[^a-zA-Z]/ /g'`; do echo $w; donerootxrootrootbinbashbinxbin
[root@centos-04 tmp]# for w in `cat /etc/passwd|sed 's/[^a-zA-Z]/ /g'`; do echo $w; done |sort |uniq -c|sort -nr |head 34 x 33 sbin 27 nologin 14 var 8 bin 7 lib 6 user 5 User 4 systemd 4 root[root@centos-04 tmp]#
[root@centos-04 tmp]# sh add_title.sh 58.sh[root@centos-04 tmp]# vim 58.sh #!/bin/bash#这个脚本用来找出重复的单词#作者:SYJ#日期:2019-03-11for w in `sed 's/[^a-zA-Z]/ /g' $1`do echo $wdone |sort |uniq -c |sort -nr |head[root@centos-04 tmp]# sh 58.sh /etc/passwd 34 x 33 sbin 27 nologin 14 var 8 bin 7 lib 6 user 5 User 4 systemd 4 root[root@centos-04 tmp]#
人员分组
首先需要有一个人员列表文件
[root@centos-04 tmp]# vim member.txt aaaaaaabbbbcccccddddddddeeeeeeeeeeeffffffgggggggghhhhhhhhh
需要安装bc命令,bc命令是linux下面的计算器
[root@centos-04 tmp]# yum install -y bc
计算一个字符cksum的值(是一串数字)
[root@centos-04 tmp]# echo adfasdf |cksum3506188467 8[root@centos-04 tmp]#
[root@centos-04 tmp]# echo -e "1\n2\n3\n4\n5"|shuf (shuf打乱列顺序)32541[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 59.sh #!/bin/bash#这个脚本用来人员分组#作者:SYJ#日期:2019-03-11#人员列表文件f=member.txt#小组数group_n=3#人员总数member_n=`wc -l $f |awk '{print $1}'`#根据姓名计算该用户所在小组的idget_n(){ #根据姓名计算cksum值 l=`echo $1 |cksum|awk '{print $1}'` #获取一个随机数 n1=$RANDOM #cksum值和随机数相机,然后除以小组数取余,这样可以确保每次获取到的余数都不一样 n2=$[$n1+$l] g_id=$[$n1%$group_n] #假如小组数为7,则余数范围0-6,如果余数为0,则小组为7 if [ $g_id -eq 0 ] then g_id=$group_n fi echo $g_id}for i in `seq 1 $group_n`do #n_$i.txt为临时文件,用来记录该小组内的成员 #脚本之前执行过,则该文件会存在,本次执行脚本前应该删除掉这个临时文件 [ -f n_$i.txt ] && rm -f n_$i.txtdoneshuf $f |while read namedo #计算用户所在小组的id g=`get_n $name` #将人员追加写入到他对应的小组里 echo $name >> n_$g.txtdone#定义计算文件行数的函数nu(){ wc -l $1 |awk '{print $1}'}#获取组员人数最多的小组max(){ ma=0; for i in `seq 1 $group_n|shuf` do n=`nu n_$i.txt` if [ $n -gt $ma ] then ma=$n fi done echo $ma}#获取组员人数最少的小组min(){ mi=$member_n for i in `seq 1 $group_n|shuf` do n=`nu n_$i.txt` if [ $n -lt $mi ] then mi=$n fi done echo $mi}#定义四舍五入函数div(){ n=`echo "scale=1;$1/$2"|bc` n1=`echo "scale=1;$n+0.5"|bc` echo $n1|cut -d. -f1}#小组组员平均值(非四舍五入)ava_n=$[$member_n/$group_n]#小组组员平均值(四舍五入)ava_n1=`div $member_n $group_n`if [ $ava_n -eq $ava_n1 ]then #定义初始最小值 ini_min=1 #以下while循环要做的事情,就是要把人数多的组里的人搞到人数少的组里去 #此while循环的条件是,当人数最少的组成员数小于组员平均值 while [ $ini_min -lt $ava_n1 ] do #找出人数最多的组 m1=`max` #找出人数最少的组 m2=`min` for i in `seq 1 $group_n|shuf` do n=`nu n_$i.txt` #找到人数最多的组对应的文件f1(可能有多个,这里取出现的第一个即可) if [ $n -eq $m1 ] then f1=n_$i.txt #找到人数最少的组对应的文件f2(可能有多个,这里取出现的第一个即可) elif [ $n -eq $m2 ] then f2=n_$i.txt fi done #取f1中最后一个人名 name=`tail -n1 $f1` #将这个人名追加写入f2中 echo $name >> $f2 #在f1中删除刚刚取走的人名 sed -i "/$name/d" $f1 #把此时的最少组人员数赋值给ini_min ini_min=`min` doneelse #定义初始最大值 ini_max=$member_n while [ $ini_max -gt $ava_n1 ] do #找出人数最多的组 m1=`max` #找出人数最少的组 m2=`min` for i in `seq 1 $group_n|shuf` do n=`nu n_$i.txt` #找到人数最多的组对应的文件f1(可能有多个,这里取出现的第一个即可) if [ $n -eq $m1 ] then f1=n_$i.txt #找到人数最少的组对应的文件f2(可能有多个,这里取出现的第一个即可) elif [ $n -eq $m2 ] then f2=n_$i.txt fi done #取f1中最后一个人名 name=`tail -n1 $f1` #将这个人名追加写入f2中 echo $name >> $f2 #在f1中删除刚刚取走的人名 sed -i "/$name/d" $f1 #把此时的最少组人员数赋值给ini_min ini_max=`max` donefifor i in `seq 1 $group_n`do echo -e "\033[34m$i 组成员有:\033[0m" cat n_$i.txt #把临时文件删除 rm -f n_$i.txt echo done
[root@centos-04 tmp]# sh 59.sh 1 组成员有:aaaaaaaeeeeeeeeeeeffffff2 组成员有:hhhhhhhhhbbbb3 组成员有:ddddddddggggggggccccc[root@centos-04 tmp]#
比较两个数大小
bc命令返回1代表成立,返回0代表不成立
[root@centos-04 tmp]# x=10.1; y=11[root@centos-04 tmp]# echo "$x > $y"|bc0[root@centos-04 tmp]# echo "$x < $y"|bc1[root@centos-04 tmp]#
[root@centos-04 tmp]# vim 60.sh if_number(){ if echo $1|grep -q '^-' then nu=`echo $1|sed 's/^-//'` else nu=$1 fi n=`echo $nu|sed 's/[0-9.]//g'` if [ -n "$n" ] then echo "$1不是合法数字。" exit fi if echo $1|grep -q '^\.' then echo "$1不是合法数字。" exit fi}if_number $1if_number $2n1=`echo "$1>$2"|bc`if [ $n1 -eq 1 ]then echo "$1 > $2"else if [ "$1" == "$2" ] then echo "$1=$2" else echo "$1 < $2" fifi
[root@centos-04 tmp]# sh 60.sh -100 -10-100 < -10[root@centos-04 tmp]# sh 60.sh 100 10 100 > 10[root@centos-04 tmp]#