https://github.com/aminglinux/shell100/blob/master/php
要求:安照這樣的日期格式(xxxx-xx-xx)每日生成一個文件,如生成的文件爲2017-12-20.log,而且把磁盤的使用狀況寫到這個文件中,提示:date、dfnode
[root@centos-04 tmp]# date 2018年 12月 26日 星期三 19:29:13 CST [root@centos-04 tmp]# date +%Y 2018 [root@centos-04 tmp]# date +%y 18 [root@centos-04 tmp]# date +%d 26 [root@centos-04 tmp]# date +%m 12 [root@centos-04 tmp]# date +%H 19 [root@centos-04 tmp]# date +%M 30 [root@centos-04 tmp]# date +%S 52 [root@centos-04 tmp]# date +%s 1545823854 [root@centos-04 tmp]# date +%F 2018-12-26 [root@centos-04 tmp]# date +%T 19:31:04 [root@centos-04 tmp]#
[root@centos-04 tmp]# date +%w 3 [root@centos-04 tmp]# date +%W 52 [root@centos-04 tmp]#
昨天日期python
[root@centos-04 tmp]# date -d "-1 day" +%F 2018-12-25 [root@centos-04 tmp]#
上一小時mysql
[root@centos-04 tmp]# date -d "-1 hours" +%T 18:34:54 [root@centos-04 tmp]#
[root@centos-04 tmp]# vim 1.sh #!/bin/bash d=`date +%F` df -h > $d.log ~ [root@centos-04 tmp]# sh 1.sh [root@centos-04 tmp]# ls 1.sh ansible-ssh-192.168.242.130-22-root ansible-ssh-192.168.242.133-22-root lua_uwpzx3 tmp.SLBPtZ45L9 2018-12-26.log ansible-ssh-192.168.242.131-22-root elasticsearch.4Kw1U8qo nginx_proxy_tmp 456.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% /dev tmpfs 910M 0 910M 0% /dev/shm tmpfs 910M 30M 881M 4% /run tmpfs 910M 0 910M 0% /sys/fs/cgroup /dev/sda1 497M 167M 331M 34% /boot tmpfs 182M 0 182M 0% /run/user/0 overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/merged shm 64M 0 64M 0% /var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shm overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/merged overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/merged shm 64M 0 64M 0% /var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shm shm 64M 0 64M 0% /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm [root@centos-04 tmp]#
改進版linux
d=`date +%F` #獲取當前日期 dir=/data/logs/disklog #指定日誌文件生成的目錄 if [ ! -d $dir ] #判斷若是沒有目錄建立 then mkdir -p $dir fi df -h > $dir/$d.log #將硬盤信息寫到日誌 find $dir/ -mtime +365 |xargs rm #刪除一年以前的文件
awk '{print $3}' access_log.2018122918 |sort -n|uniq -c|sort -n -r|less
[root@centos-04 tmp]# vim 2.sh #!/bin/bash awk '{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/bash sum=0 for n in `ps aux |grep -v 'TIME COMMAND' |awk '{print $6}'` do sum=$[$sum+$n] done echo $sum [root@centos-04 tmp]# sh 3.sh 114892 [root@centos-04 tmp]# free total used free shared buff/cache available Mem: 1863224 104260 1610364 9976 148600 1593352 Swap: 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,sys reload(sys) sys.setdefaultencoding('utf8') import getopt import smtplib from email.MIMEText import MIMEText from email.MIMEMultipart import MIMEMultipart from 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" % err def 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/bash n=`ping -c5 www.baidu.com|grep 'packet' |awk -F '%' '{print $1}' |awk '{print $F N}'` if [ -z "$n" ] then echo "腳本有問題。" exit else n1=`echo $n|sed 's/[0-9]//g'` if [ -n "$n" ] then echo "腳本$0有問題。" exit fi fi m=123@qq.com while : do if [ $n -ge 50 ] then python mail.py $m "機器宕機" "丟包率$n%" fi sleep 30 done
[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/bash find /123/ -type f -name "*.txt" > /tmp/txt.list for f in `cat /tmp/txt.list` do mv $f $f.bak done #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.bak done > /tmp/txt.bak.list tar -czvf 123.tar.gz `cat /tmp/txt.bak.list |xargs` for f in `cat /tmp/txt.list` do mv $f.bak $f done
[root@centos-04 tmp]# rsync -av /123.bak/ /123/
[root@centos-04 tmp]# netstat -lntp |grep 80 tcp 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.1 Starting Nmap 6.40 ( http://nmap.org ) at 2019-01-04 03:29 CST Nmap scan report for localhost (127.0.0.1) Host is up (0.00011s latency). PORT STATE SERVICE 80/tcp open http Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds [root@centos-04 tmp]#
[root@centos-04 tmp]# vim 6.sh
#!/bin/bash m=123@123.com while : 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 30 done
也能夠用nohup或screen這screen裏執行6.shnginx
[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/bash d1=`date +%w` d2=`date +%d` local_backdir=/bak/mysql remote_backdir=192.168.242.130::backup exec 1> /tmp/mysqlbak.log 2>/tmp/mysqlbak.err echo "mysql backup begin at `date`" mysqldump -uroot -pxxx discuz > $local_backdir/discuz.sql.$d1 rsync -az $local_backdir/discuz.sql.$d1 $remote_backdir/discuz.sql.$d2 echo "mysql backup end at `date`"
1.502的狀況:php配置有問題、php腳本耗資源。git
[root@centos-04 tmp]# vim 8.sh #!/bin/bash log=/data/log/access.log while : do 502_n=`tail -n 300 $log |grep -c ' 502 '` if [ -z "$502_n" ] then exit fi if [ $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 fi fi sleep 10 done
1.刪除前五行中包含字母的行github
[root@centos-04 tmp]# head -n5 test.sql |sed '/[a-zA-Z]/d'
2.查看文件前5行web
[root@centos-04 tmp]# sed -n '1,5'p mail.py #!/usr/bin/env python #-*- coding: UTF-8 -*- import os,sys reload(sys) sys.setdefaultencoding('utf8') [root@centos-04 tmp]#
3.把6到10行中的所有字母刪掉正則表達式
[root@centos-04 tmp]# vim 9.sh #!/bin/bash sed -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; done Bash also interprets a number of multi-character options. [root@centos-04 tmp]#
[root@centos-04 tmp]# vim 10.sh #!/bin/bash c="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}' fi done [root@centos-04 tmp]# sh 10.sh Bash also a of multi [root@centos-04 tmp]#
[root@centos-04 tmp]# vim 11.sh #!/bin/bash echo "*cmd meau** 1 - date 2 - ls 3 - who 4 - pwd" read -p "Please input a number:" n if [ -z "$n" ] then echo "請輸入一純數字,範圍1-4。"
exit fi n1=`echo $n|sed 's/[0-9]//g'` if [ -n "$n1" ] then echo "請輸入一個純數字,範圍1-4。" exit fi case $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 - pwd Please input a number:1 2019年 01月 04日 星期五 21:43:35 CST [root@centos-04 tmp]# sh 11.sh *cmd meau** 1 - date 2 - ls 3 - who 4 - pwd Please input a number:2 10.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 - pwd Please input a number:3 root 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 - pwd Please input a number:4 /tmp [root@centos-04 tmp]# sh 11.sh *cmd meau** 1 - date 2 - ls 3 - who 4 - pwd Please input a number:5 請輸入1-4的數字 [root@centos-04 tmp]# sh 11.sh *cmd meau** 1 - date 2 - ls 3 - who 4 - pwd Please input a number:fsaf 請輸入一個純數字,範圍1-4。 [root@centos-04 tmp]#
[root@centos-04 tmp]# seq -w 00 09 00 01 02 03 04 05 06 07 08 09 [root@centos-04 tmp]# seq 0 9 0 1 2 3 4 5 6 7 8 9 [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/bash for 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_$i done [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/passwd user_00:x:1001:1001::/home/user_00:/bin/bash user_01:x:1002:1002::/home/user_01:/bin/bash user_02:x:1003:1003::/home/user_02:/bin/bash user_03:x:1004:1004::/home/user_03:/bin/bash user_04:x:1005:1005::/home/user_04:/bin/bash user_05:x:1006:1006::/home/user_05:/bin/bash user_06:x:1007:1007::/home/user_06:/bin/bash user_07:x:1008:1008::/home/user_07:/bin/bash user_08:x:1009:1009::/home/user_08:/bin/bash user_09:x:1010:1010::/home/user_09:/bin/bash [root@centos-04 tmp]# cat /tmp/pass.tmp user_00 8xiwgZSce6 user_01 yaMp6cb2gA user_02 jx0QtlL2fw user_03 69bwuqgEDf user_04 p3fpvMMl9c user_05 fm5Bv4Xssx user_06 ivx69zVIpy user_07 l77CvxvuHy user_08 MZfmi6kx4f user_09 4bAkzeaKa6 [root@centos-04 tmp]#
登陸測試(複製user_00的密碼粘貼密碼登陸成功)
[root@centos-04 ~]# ssh user_00@192.168.242.130 user_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/passwd tcpdump:x:72:72::/:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin haproxy:x:188:188:haproxy:/var/lib/haproxy:/sbin/nologin dockerroot:x:994:991:Docker User:/var/lib/docker:/sbin/nologin epmd:x:993:990:Erlang Port Mapper Daemon:/tmp:/sbin/nologin rabbitmq:x:992:989:RabbitMQ messaging server:/var/lib/rabbitmq:/bin/bash user1:x:1000:1000::/home/user1:/bin/bash
[root@centos-04 tmp]# vim 13.sh #!/bin/bash check_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 fi sleep 10 done
[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]# date 2019年 01月 05日 星期六 01:40:53 CST [root@centos-04 logs]#
查看iptables (pkts有多少個數據包,bytes有多少字節)
[root@centos-04 tmp]# iptables -nvL Chain 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:80 Chain 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 -nvL Chain 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-unreachable 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 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:80 Chain 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/bash block_ip { t1=`date -d "-1 min" +%Y:%H:%M` log/data/logs/access_log egrep "$t1:[0-9]+" $log > /tmp/tmp_last_min.log awk '{print $1}' /tmp/tmp_last_min.log |sort -n |uniq -c|sort -n |awk '$1>100 {print $2}' > /tmp/bad_ip.list n=`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 done fi } 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_ip else block_ip fi
[root@centos-04 tmp]# vim 14.sh
將該腳本放到計劃任務中每分鐘執行一次
[root@centos-04 tmp]# vim 15.sh
#!/bin/bash
x=10
y=21
for 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
10
31
53
77
105
141
193
277
425
701
1233
2277
4345
8461
16673
33077
獲取linux版本
[root@centos-04 tmp]# awk -F 'release ' '{print $2}' /etc/redhat-release |cut -d '.' -f1 7 [root@centos-04 tmp]#
[root@centos-04 tmp]# vim 16.sh #!/bin/bash v=`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/nologin user1:x:1000:1000::/home/user1:/bin/bash user_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/%//' 已用 36 0 0 2 0 34 36 0 36 0 36 0 0 [root@centos-04 tmp]#
去除標頭、解決有小數點的狀況
[root@centos-04 tmp]# df|sed '1d' |awk '{print $5}'|sed 's/%//'|cut -d '.' -f1 36 0 0 2 0 34 36 0 36 0 36 0 0 [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/bash dir=/tmp/disk d=`date +%F` m=123@123.com [ -d $dir ] || mkdir $dir df >> $dir/$d.log df -i >> $dir/$d.log df|sed '1d' |awk -F ' +|%' '$5>=85 {print $7}' > $dir/df.tmp df -i|sed '1d' |awk -F ' +|%' '$5>=85 {print $7}' > $dir/df_i.tmp n1=`wc -l $dir/df.tmp|awk '{print $1}'` n2=`wc -l $dir/df_i.tmp|awk '{print $1}'` tag=0 if [ $n1 -gt 0 ] then if [ $n2 -gt 0 ] then tag=11 else tag=10 fi else if [ $n2 -gt 0 ] then tag=01 else tag=00 fi fi case $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/bash basedir=/tmp/ t=`date +%Y%m%d%H%M` find $basedir/ -type f -mmin -5 > /tmp/file.list n=`wc -l /tmp/file.list|awk '{print $1}'` if [ $n -gt 0 ] then mv /tmp/file.list /tmp/$t.list fi
[root@centos-04 tmp]# sh 18.sh
[root@centos-04 tmp]# ls
10.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.sql
11.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" +%H 08 [root@centos-04 tmp]# date +%H 00 [root@centos-04 tmp]#
[root@centos-04 tmp]# vim 20.sh #!/bin/bash dir=/tmp/log_stat t=`date +%d%H` t1=`date +%H` logdir=/data/log [ -f $dir/$t.log ] && rm -f $dir/$t.log [ -d $dir ] || mkdir $dir if [ $t == "00" -o $t == "12" ] then for f in `find $logdir/ -type f` do > $f done else for f in `find $logdir/ -type f` do du -sh $f >> $dir/$t.log done fi
-e的用法
[root@centos-04 tmp]# for i in `echo -e "123\nabc 123"`; do echo $i; done 123 abc 123 [root@centos-04 tmp]#
[root@centos-04 tmp]# wc -l 20 2018-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% /dev tmpfs 910M 0 910M 0% /dev/shm tmpfs 910M 30M 881M 4% /run tmpfs 910M 0 910M 0% /sys/fs/cgroup /dev/sda1 497M 167M 331M 34% /boot tmpfs 182M 0 182M 0% /run/user/0 overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/merged shm 64M 0 64M 0% /var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shm overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/merged overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/merged shm 64M 0 64M 0% /var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shm shm 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% /dev tmpfs 910M 0 910M 0% /dev/shm tmpfs 910M 30M 881M 4% /run tmpfs 910M 0 910M 0% /sys/fs/cgroup /dev/sda1 497M 167M 331M 34% /boot tmpfs 182M 0 182M 0% /run/user/0 overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/ffa13b95ae63f2954be362511c25724d5b854201e405eb4913b54b80e9cf6617/merged shm 64M 0 64M 0% /var/lib/docker/containers/f42d989248587138ac2094003ae274467518b1a15d8ead51664cc03ea0c94e59/shm overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/53a9f09976bd64507e995cffd443f1e151fddd88c266afc416d0fb90cb90de14/merged overlay 18G 6.2G 12G 36% /var/lib/docker/overlay2/35bdef03d5af944aa5b87ee4d0ca692a6d4b6394f94633f26ebc78e664ca3150/merged shm 64M 0 64M 0% /var/lib/docker/containers/e6060a8f50ed0c2455e55ae466f101226d2668a28d8e19070bf4f6b2b3c6dd73/shm shm 64M 0 64M 0% /var/lib/docker/containers/c58be577ba9f3351c23c5d1d1ec9661f129aa109735d42046a9e9e465a787306/shm [root@centos-04 tmp]#
[root@centos-04 tmp]# vim 21.sh #!/bin/bash sum=0 while read line do line_n=`echo $line|sed 's/[^0-9]//g'|wc -L` echo $line_n sum=$[$sum+$line_n] done < $1 echo "sum:$sum" [root@centos-04 tmp]# sh 21.sh 2018-12-26.log 0 8 8 8 9 8 12 9 53 52 47 47 48 50 sum: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.txt 1 2 3 [root@centos-04 tmp]#
#!/bin/bash dir=/data/web [ -f /tmp/md5.list ] && rm -f /tmp/md5.list find $dir/ -type f > /tmp/file.list while read line do md5sum $line >> /tmp/md5.list done < /tmp/file.list scp /tmp/md5.list B:/tmp/ [ -f /tmp/check_md5.sh ] && rm -f /tmp/check_md5.sh cat >/tmp/check_md5.sh << EOF #!/bin/bash dir=/data/web n=\`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." fi done EOF scp /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 5
Linux 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/s
07:19:10 PM lo 5.00 5.00 0.50 0.50 0.00 0.00 0.00
07:19:10 PM eth0 13.00 6.00 0.77 0.42 0.00 0.00 0.00
07:19:10 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
07:19:11 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:19:11 PM eth0 12.00 6.00 0.71 0.73 0.00 0.00 0.00
07:19:11 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
07:19:12 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:19:12 PM eth0 12.00 5.00 0.72 0.68 0.00 0.00 0.00
07:19:12 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
07:19:13 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:19:13 PM eth0 16.83 5.94 1.00 0.74 0.00 0.00 0.00
07:19:13 PM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
07:19:14 PM lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00
07:19:14 PM eth0 8.00 3.00 0.48 0.56 0.00 0.00 0.00
Average: IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s
Average: lo 1.00 1.00 0.10 0.10 0.00 0.00 0.00
Average: 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/bash logdir=/tmp/sar_log file=$logdir/`date +%d$H`.log t=`date +"%F %H:%M"` [ -d $logdir ] || mkdir -p $logdir LANG=en sar -n DEV 1 5 |grep eth0 |grep "Average" > /tmp/sar.tmp exec >>$file echo "$t" awk '{print "input:",$5*8000"bps""\n""output:",$6*8000"bps"}' /tmp/sar.tmp echo "###################" [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:48 input: 160bps output: 80bps ################### [root@centos-04 tmp]#
[root@centos-04 tmp]# vim 24.sh #!/bin/bash for pid in `ps aux|grep clearnen.sh|awk '{print $2}'`; do echo $pid; kill -9 $pid; done
[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/bash n=`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]#
[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/bash mysql = "/usr/local/mysql/bin/mysql -uroot -p123456" if ! $mysql -e "show processlist" >/dev/null 2>/dev/null then echo "MySql service is down." exit else $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 fi fi
[root@centos-04 tmp]# vim 27.sh #!/bin/bash if [ $# -eq 0 ] || [ $# -gt 2 ] then echo "Wrong, use bash $0 --add username, or bash $0 --del username or bash $0 --help" exit fi ex_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/bash sum=0 for i in `seq 1 100` do j=$[$i%3] if [ $j -eq 0 ] then sum=$[$sum+$i] fi done echo $sum
[root@centos-04 tmp]# vim 29.sh #!/bin/bash is_nu() { n=`echo $1 |sed 's/[0-9]//g'` if [ -n "$n" ] then echo "給出的參數必須是正整數" exit fi } if [ $# -ne 2 ] then echo "必需要輸入兩個參數" exit else is_nu $1 is_nu $2 fi big() { 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 $2 jian $1 $2 cheng $1 $2 chu $1 $2
[root@centos-04 tmp]# vim 30.sh #!/bin/bash while : 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 fi done
[root@centos-04 tmp]# ip add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}' lo ens33 docker0 vethd17e886@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/bash ip add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}' > /tmp/eth.list while : 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 fi done if_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/bash if [ $# -eq 0 ] then ls -l . else for d in $@ do echo "There are these directorys in $d:" find $d -type d done fi [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-unix There 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/bash if [ $# -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 done fi
[root@centos-04 tmp]# vim 33.sh #!/bin/bash if [ $# -ne 2 ] then echo "你必需要輸入兩個參數,第一個參數是網址,第二個參數是目錄。" exit 1 fi if [ ! -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 done else cd $2 wget $1 if [ $? -eq 0 ] then exit 0 else echo "下載失敗" exit 52 fi fi
1.返回隨機數
[root@centos-04 tmp]# echo $RANDOM 15845 [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/bash n=$[$RANDOM%101] while : do read -p "請輸入一個0-100的數字:" n1
if [ -z "$n1" ]
then
echo "必須輸入一個數字"
continue
fi
n2=`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 fi done [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/bash f=/tmp/user_number.txt while : 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 fi done [root@centos-04 tmp]# sh 35.sh Please input a username:user1 用戶user1對應的數字是:66 Please input a username:user2 用戶user2對應的數字是:45 Please input a username:
[root@centos-04 tmp]# vim 35.sh #!/bin/bash f=/tmp/user_number.txt jude_n() { #!/bin/bash f=/tmp/user_number.txt j_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 fi done
[root@centos-04 tmp]# echo "aaaaa1bbbbb" |sed "s/[^0-9]//g" 1 [root@centos-04 tmp]# echo "aaaaa1bbbbb" |sed "s/[^0-9]//g" |wc -L 1 [root@centos-04 tmp]#
[root@centos-04 tmp]# vim 1.txt 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 222222222222 fasf333 [root@centos-04 tmp]# vim 36.sh #!/bin/bash while read line do n=`echo $line |sed 's/[^0-9]//g'|wc -L` if [ $n -eq 1 ] then echo $line fi done < 1.txt
想要實現的效果,當有日誌文件1.log.5時把1.log.5文件刪除,將1.log.4文件移到爲1.log.5,以此類推。
#!/bin/bash cd /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/bash cd /data/logs log=1.log mv_log() { [ -f $1 ] && mv $1 $2 } [ -f $log.5 ] && rm -f $log.5 for i in `seq 4 -1 1` do j=$[$i+1] mv_log $log.$i $log.$j done mv 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]# ls 1.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]# ls 1.log.1 1.log.2 [root@centos-04 logs]# cat 1.log.1 000 [root@centos-04 logs]# cat 1.log.2 111 [root@centos-04 logs]#
[root@centos-04 tmp]# vim 38.sh #!/bin/bash for 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 不通。" fi done
1.-n 檢查腳本錯誤
2.演示語法錯誤,咱們故意給for前加一個i
[root@centos-04 tmp]# vim 38.sh
#!/bin/bash
ifor 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 不通。"
fi
done
[root@centos-04 tmp]# sh -n 38.sh 38.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.sh sh: 1.sh: 沒有那個文件或目錄 請輸入q|Q退出腳本。q [root@centos-04 tmp]# ls 1.txt 31.sh 33.sh 35.sh 37.sh 39.sh proxy.log user_number.txt 2.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/bash ifor 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 不通。" fi done "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/bash n=`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 12345 12,345 [root@centos-04 tmp]# sh 40.sh 123 123 [root@centos-04 tmp]# sh 40.sh 1235555 1,235,555
獲取10小時以前的時間
[root@centos-04 tmp]# d=`date -d "-10 hours" +%H` [root@centos-04 tmp]# echo $d 17
[root@centos-04 tmp]# vim 41.sh #!/bin/bash d=`date +%H` if [ $d -ge 0 -a $d -lt 7 ] then tag=1 elif [ $d -ge 7 -a $d -lt 12 ] then tag=2 elif [ $d -ge 12 -a $d -lt 18 ] then tag=3 else tag=4 fi case $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/bash
PS3="Please input your choice:" 爲了去掉結果中的#?
select i in w ls pwd quit
do
case $i in
w)
w
;;
ls)
ls
;;
pwd)
pwd
;;
quit)
exit
;;
*)
echo "please input 1-3."
;;
esac
done
"42.sh" [新] 21L, 179C 已寫入
[root@centos-04 tmp]# sh 4
40.sh 41.sh 42.sh 456.log
[root@centos-04 tmp]# sh 42.sh
1) w
2) ls
3) pwd
4) quit
#? 1
03:48:09 up 4 days, 8:11, 1 user, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.242.1 21:44 1.00s 0.26s 0.01s w
#? 2
1.txt 31.sh 33.sh 35.sh 37.sh 39.sh 41.sh 456.log sh.err
2.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) w 2) ls 3) pwd 4) [root@centos-04 tmp]# [root@centos-04 tmp]# vim 42-2.sh #!/bin/bash echo -e "1) w\n2) ls\n3) pwd\n4) quit" while : do read -p "Please input your choice(1-4):" c case $c in 1) w ;; 2) ls ;; 3) pwd ;; 4) exit ;; *) echo "Please input 1-4" ;; esac done "42-2.sh" 23L, 219C 已寫入 [root@centos-04 tmp]# sh 42-2.sh 1) w 2) ls 3) pwd 4) quit Please input your choice(1-4):1 04:42:46 up 4 days, 9:06, 1 user, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.242.1 21:44 6.00s 0.27s 0.00s w Please input your choice(1-4):2 1.txt 31.sh 33.sh 35.sh 37.sh 39.sh 41.sh 42.sh proxy.log user_number.txt 2.txt 32.sh 34.sh 36.sh 38.sh 40.sh 42-2.sh 456.log sh.err Please input your choice(1-4): Please input 1-4 Please input your choice(1-4):3 /tmp Please input your choice(1-4):4 [root@centos-04 tmp]#
思路:經過w命令獲取第一列的用戶
[root@centos-04 tmp]# vim 43.sh #!/bin/bash while : do if w|sed '1'd|awk '{print $1}'|grep -qw "$1" then echo "用戶$1已經登陸系統" exit fi sleep 300 done [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/bash pp=$$ ps -elf |sed '1'd > /tmp/pid.txt for pid in `awk -v ppn=$pp '$5!=ppn {print $4}' /tmp/pid.txt` do if ! [ -d /proc/$pid ] then echo "系統中並無pid爲$pid的目錄,須要檢查。" fi done [root@centos-04 tmp]# sh 44.sh
[root@centos-04 tmp]# vim 45.sh #!/bin/bash n=1 cat $1 |while read line do 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 123 456 78 3333[root@centos-04 tmp]#
~就是表示用來匹配後面的正則表達式,告訴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}' lo ens33 [root@centos-04 tmp]#
[root@centos-04 tmp]# vim 46.sh #!/bin/bash ip add |awk -F ': ' '$1 ~ "^[1-9]" {print $2}' > /tmp/ifs.txt get_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 fi done > /tmp/if_ip.txt if [ $# -ne 2 ] then echo "請輸入正確的格式: bash $0 -i 網卡 或者 bash $0 -I ip" exit fi if [ $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 fi elif [ $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 fi else 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]#
[root@centos-04 tmp]# echo $RANDOM 26768 [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/bash get_number() { for i in `seq 0 2` do a[$i]=$[$RANDOM%10] done echo ${a[@]}|sed s'/ //g' } if [ $# -eq 0 ] then get_number elif [ $# -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 |xargs else 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 10 898 422 695 369 417 402 573 800 957 614 [root@centos-04 tmp]#
查看是否安裝了包,未安裝的包echo $? 會輸出1,安裝的包輸出0
[root@centos-04 tmp]# rpm -q httpd httpd-2.4.6-88.el7.centos.x86_64 [root@centos-04 tmp]# yum list|grep httpd Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast httpd.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/bash if_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 httpd if [ $? -eq 0 ] then if ! pgrep httpd >/dev/null then service httpd start fi else yum install -y httpd fi if_install mysql-server if [ $? -eq 0 ] then if ! pgrep mysqld >/dev/null then service mysqld start fi else yum install -y mysql-server fi
[root@centos-04 tmp]# sh 48.sh httpd已經安裝 Redirecting to /bin/systemctl start httpd.service Job 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 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [root@centos-04 tmp]#
[root@centos-04 tmp]# cal 02 2010 二月 2010 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 [root@centos-04 tmp]# cal 02 2010|grep -w 28 28 [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 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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/bash if [ $# -ne 1 ] || [ ${#1} -ne 8 ] then echo 'please input sh $0 yyyymmdd' exit 1 fi y=`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//'` fi if cal $m $y > /dev/null 2>/dev/null then if ! cal $m $y |grep -qw "$d" then echo "你給的日期是不合法的" else echo "日期合法" fi else 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.00 01時42分33秒 ens33 1.01 1.01 0.06 0.22 0.00 0.00 0.00 01時42分34秒 ens33 0.99 0.99 0.06 0.21 0.00 0.00 0.00 01時42分35秒 ens33 1.01 1.01 0.06 0.22 0.00 0.00 0.00 01時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/bash LANG=en sar -n DEV 1 10|grep -w "$1" > /tmp/sar.tmp in=`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 $1 fi [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 --:--:-- --:--:-- --:--:-- 0
HTTP/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.cn code=`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`" exit elif [ $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文件放上
$HOME變量能夠代替用戶的家目錄,當前登陸的是哪一個用戶變量就指向這個用戶的家目錄。
[root@centos-04 tmp]# date +%F 2019-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/bash t=`date +%F` cd $HOME tar 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 ~]# ls 2019-03-10.tar.gz clouddemo.py [root@centos-04 ~]# tar -tf 2019-03-10.tar.gz (-tf查看壓縮包裏的文件列表)
[root@centos-04 ~]# iptables -nvL INPUT Chain 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 INPUT Chain 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}' 1 2 [root@centos-04 tmp]#
[root@centos-04 tmp]# vim 53.sh #!/bin/bash iptables -nvL INPUT --line-numbers |grep -w 'dpt:22' |awk '$4 ~/REJECT|DROP/ {print $1}' > /tmp/iptables.log n=`cat /tmp/iptables.log |wc -l` if [ $n -gt 0 ] then for n in `tac /tmp/iptables.log` do iptables -D INPUT $n done fi [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:%T 11/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-22 export LANG=en log="/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: " n please: 4 [root@centos-04 tmp]# echo $n 4 [root@centos-04 tmp]#
[root@centos-04 tmp]# for i in `seq 1 $n`; do echo $i; done 1 2 3 4 [root@centos-04 tmp]#
[root@centos-04 tmp]# echo $n 4 [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-22 read -p "Please input a number: " n n1=`echo $n |sed 's/[0-9]//g'` if [ -n "$n1" ] then echo "Please input a number." exit fi for i in `seq 1 $n` do echo $i done read -p "If continue? y/n" c case $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: 5 1 2 3 4 5 If continue? y/ny Please input a number: 8 6 7 8 [root@centos-04 tmp]#
建立一個文件寫入一、二、三、四、五、6
[root@centos-04 tmp]# vi 1.txt 1 2 3 4 5 6
在第五列後面添加文本123456789
[root@centos-04 tmp]# sed '5a123456789' 1.txt
1
2
3
4
5
123456789
6
添加\n就是回車換行加上-i參數直接修改文件
[root@centos-04 tmp]# sed -i '5a12345\n6789' 1.txt 1 2 3 4 5 12345 6789 6 [root@centos-04 tmp]#
[root@centos-04 tmp]# vim 56.sh #!/bin/bash #這個腳本用來爲文件增長行 #做者:star #日期:2019-03-25 n=0 cat 1.txt |while read line do 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 fi done [root@centos-04 tmp]# sh 56.sh 1 2 3 4 5 # This is a test file. # Test insert line into this file. 6 [root@centos-04 tmp]#
[root@centos-04 tmp]# date +%d 11 [root@centos-04 tmp]# date +%y 19 [root@centos-04 tmp]# date +%Y 2019 [root@centos-04 tmp]# date +%y%m%d 190311 [root@centos-04 tmp]# date +%Y%m%d 20190311 [root@centos-04 tmp]#
[root@centos-04 tmp]# date +%F 2019-03-11 [root@centos-04 tmp]#
自動添加shell腳本註釋
[root@centos-04 tmp]# vim add_title.sh #!/bin/bash d=`date +%F` cat >$1<<EOF #!/bin/bash #這個腳本用來 #做者:SYJ #日期:$d EOF [root@centos-04 tmp]# sh add_title.sh 57.sh [root@centos-04 tmp]# vim 57.sh #!/bin/bash #這個腳本用來 #做者:SYJ #日期:2019-03-11
[root@centos-04 tmp]# vim 57.sh #!/bin/bash #這個腳本用來備份/etc/目錄 #做者:SYJ #日期:2019-03-11 d1=`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]# ls 190301_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 bash bin x bin bin sbin nologin
將字母放到一列
[root@centos-04 tmp]# for w in `cat /etc/passwd|sed 's/[^a-zA-Z]/ /g'`; do echo $w; done root x root root bin bash bin x bin
[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-11 for w in `sed 's/[^a-zA-Z]/ /g' $1` do echo $w done |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 aaaaaaa bbbb ccccc dddddddd eeeeeeeeeee ffffff gggggggg hhhhhhhhh
須要安裝bc命令,bc命令是linux下面的計算器
[root@centos-04 tmp]# yum install -y bc
計算一個字符cksum的值(是一串數字)
[root@centos-04 tmp]# echo adfasdf |cksum 3506188467 8 [root@centos-04 tmp]#
[root@centos-04 tmp]# echo -e "1\n2\n3\n4\n5"|shuf (shuf打亂列順序) 3 2 5 4 1 [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}'` #根據姓名計算該用戶所在小組的id get_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.txt done shuf $f |while read name do #計算用戶所在小組的id g=`get_n $name` #將人員追加寫入到他對應的小組裏 echo $name >> n_$g.txt done #定義計算文件行數的函數 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` done else #定義初始最大值 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` done fi for 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 組成員有: aaaaaaa eeeeeeeeeee ffffff 2 組成員有: hhhhhhhhh bbbb 3 組成員有: dddddddd gggggggg ccccc [root@centos-04 tmp]#
bc命令返回1表明成立,返回0表明不成立
[root@centos-04 tmp]# x=10.1; y=11 [root@centos-04 tmp]# echo "$x > $y"|bc 0 [root@centos-04 tmp]# echo "$x < $y"|bc 1 [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 $1 if_number $2 n1=`echo "$1>$2"|bc` if [ $n1 -eq 1 ] then echo "$1 > $2" else if [ "$1" == "$2" ] then echo "$1=$2" else echo "$1 < $2" fi fi
[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]#