我把shell经常遇到的内容在这里总结一下
监控磁盘使用率的脚本
[root@kk ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/vda1 50G 8.5G 39G 19% / devtmpfs 1.9G 0 1.9G 0% /dev tmpfs 1.9G 24K 1.9G 1% /dev/shm tmpfs 1.9G 452K 1.9G 1% /run tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup tmpfs 379M 0 379M 0% /run/user/0
如何把根目录的Use%数据拿出来
- 方法一:用int
[root@kk ~]# df -h|grep /$|awk '{print int($(NF-1))}' 19
- 方法二:用变量的删除
[root@kk ~]# kk=`df -h|grep /$|awk '{print $(NF-1)}'` [root@kk ~]# echo ${kk%\%} 19
监控磁盘使用率
[root@kk ~]# vim disk.sh #!/bin/bash disk=`df -h|grep /$|awk '{print int($(NF-1))}'` if [ $disk -gt 80 ];then echo "磁盘使用率已经超过80%,当前$disk" else echo "磁盘正常" fi
监控内存使用率的脚本
[root@kk ~]# free -m total used free shared buff/cache available Mem: 3789 412 138 23 3 238 3058 Swap: 0 0 0
如何把根目录的Use%数据拿出来
[root@kk ~]# free -m|awk 'NR==2{print $3/$2*100}' 10.8736
脚本编写
[root@kk ~]# vim mem.sh #!/bin/bash mem=`free -m|awk 'NR==2{print $3/$2*100}'` if [ ${mem#*.} -gt 80 ];then echo "内存空间不够,当前: $mem" else echo "内存空间够,当前: $mem" fi [root@kk ~]# sh mem.sh 内存空间够,当前: 10.9
批量创建任意个用户
命令行
[root@web01 ~]# echo kk{1..10}|xargs -n1|sed -r 's#(.*)#useradd ;echo "123"|passwd --stdin #g' useradd kk1;echo "123"|passwd --stdin kk1 useradd kk2;echo "123"|passwd --stdin kk2 useradd kk3;echo "123"|passwd --stdin kk3 useradd kk4;echo "123"|passwd --stdin kk4 useradd kk5;echo "123"|passwd --stdin kk5 useradd kk6;echo "123"|passwd --stdin kk6 useradd kk7;echo "123"|passwd --stdin kk7 useradd kk8;echo "123"|passwd --stdin kk8 useradd kk9;echo "123"|passwd --stdin kk9 useradd kk10;echo "123"|passwd --stdin kk10
脚本的方式
#!/bin/bash read -p "请输入用户名: " x if [ -z $x ];then echo "不能为空" exit fi read -p "请输入创建几个用户: " xx if [[ ! $xx =~ ^[0-9]+$ ]];then echo "请输入整数" exit fi for i in `seq $xx` do user=${x}$i useradd $user;echo "123"|passwd --stdin $user >/dev/null if [ $? -eq 0 ];then echo "ok" else echo "no" fi done
猜数字游戏
要求:随机输入一个数1-100,如果比随机数小则提示比随机数小,如果比随机数大,则提示比随机数大,相同则退出,错误继续死循环,最后统计猜对次数
随机1-100的数
[root@web01 ~]# echo $((RANDOM%100+1))
编写脚本
[root@web01 ~]# vim yx.sh break跳出本次循环 #!/bin/bash ll=`echo $((RANDOM%100+1))` i=1 while true do read -p "请输入: " x if [ $x -gt $ll ];then echo "你输入的大了" elif [ $x -lt $ll ];then echo "你输入的小了" else echo "你猜对了" break fi let i++ done echo "你总共猜了$i"
如何显示默认的环境变量
env
统计Nginx日志中每个IP的访问量是多少,独立IP数和PV
[root@Kk ~]# awk '{print $1}' /var/log/nginx/access.log|sort|uniq -c|sort -n 。。。。。 2 34.210.187.72 2 36.110.199.43 2 40.77.167.94 2 47.92.74.243 3 157.55.39.22 3 45.136.108.42 3 66.249.79.164 4 66.249.79.167 5 193.169.253.89 5 39.99.253.234 12 47.105.170.18 58 47.244.88.163 161 223.116.166.225
统计Linux系统所有进程占用内存的大小的和
[root@Kk ~]# ps aux|awk '{print $6}'|grep -v RSS|awk '{sum+=$1}END{print sum}' 1111248
练习一:
1.在/backup下创建10个.txt的文件。找到/backup目录下所有后缀名为.txt的文件
2.把所有的.bak文件打包压缩为123.tar.gz
3.批量还原文件的名字,及把增加的.bak再删除
#!/bin/bash [ -d /backup ]|| mkdir /backup cd /backup touch {1..10}.txt rename .txt .txt.bak *.txt tar zcf 123.tar.gz *.bak for i in `ls *.bak` do mv $i ${i%.*} done [root@web01 backup]# ll total 4 -rw-r--r-- 1 root root 0 Jan 29 18:48 10.txt -rw-r--r-- 1 root root 175 Jan 29 18:48 123.tar.gz -rw-r--r-- 1 root root 0 Jan 29 18:48 1.txt -rw-r--r-- 1 root root 0 Jan 29 18:48 2.txt -rw-r--r-- 1 root root 0 Jan 29 18:48 3.txt -rw-r--r-- 1 root root 0 Jan 29 18:48 4.txt -rw-r--r-- 1 root root 0 Jan 29 18:48 5.txt -rw-r--r-- 1 root root 0 Jan 29 18:48 6.txt -rw-r--r-- 1 root root 0 Jan 29 18:48 7.txt -rw-r--r-- 1 root root 0 Jan 29 18:48 8.txt -rw-r--r-- 1 root root 0 Jan 29 18:48 9.txt [root@web01 backup]# tar -tf 123.tar.gz 10.txt.bak 1.txt.bak 2.txt.bak 3.txt.bak 4.txt.bak 5.txt.bak 6.txt.bak 7.txt.bak 8.txt.bak 9.txt.bak
练习二:
把一个文档中,前五行包含字母的行删掉,同时把6-10行的字母删除
[root@web01 backup]# cat /etc/passwd|sed -n '1,5p'|sed '/[a-Z]/d' [root@web01 backup]# cat /etc/passwd|sed -n '6,10p'|sed 's#[a-Z]##g' ::5:0::/:// ::6:0::/:// ::7:0::/:// ::8:12::///:// ::11:0::/://
练习三:
打印一句话中字符小于3的字符 i am hewenfu teacher i am 18
[root@web01 backup]# baba=i am hewenfu teacher i am 18 [root@web01 backup]# echo $baba|xargs -n1|awk '{if(length<3)print}' i am i am 18
练习三:
写一个脚本,计算100以为所有能被3整除的数相加的和
[root@zabbix ~]# vim kk.sh #!/bin/bash sum=0 for i in `seq 100` do num=$(($i%3)) %就是整除 if [ $num -eq 0 ];then sum=$(($i+$sum)) 这个就除以3等于0的数给到sum,sum进行累加 fi done echo $sum
练习四:
There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do.
1.按单词出现频率降序排序!
因为段落中有标点符号,首先用sed命令进行替换为空格
sed 's#[,;.!]# #g'
[root@zabbix ~]# echo "There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do."|sed 's#[,;.!]# #g' There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real Dream what you want to dream go where you want to go be what you want to be because you have only one life and one chance to do all the things you want to do
把这些进行降序排序统计
[root@zabbix ~]# echo "There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do."|sed 's#[,;.!]# #g'|xargs -n1|sort|uniq -c|sort -rh 7 you 6 to 5 want 2 what 2 them 2 one 2 life 2 go 2 do 2 be 2 and 1 your 1 where 1 when 1 things 1 There 1 the 1 that 1 someone 1 so 1 real 1 pick 1 only 1 much 1 moments 1 miss 1 just 1 in 1 hug 1 have 1 from 1 for 1 dreams 1 Dream 1 dream 1 chance 1 because 1 are 1 all
2.将结果导入文件,让第一列与第二列位置互换,对第二列进行排序
[root@zabbix ~]# echo "There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do."|sed 's#[,;.!]# #g'|xargs -n1|sort|uniq -c|sort -rn >xx.txt [root@zabbix ~]# cat xx.txt 7 you 6 to 5 want 2 what 2 them 2 one 2 life 2 go 2 do 2 be 2 and 1 your 1 where 1 when 1 things 1 There 1 the 1 that 1 someone 1 so 1 real 1 pick 1 only 1 much 1 moments 1 miss 1 just 1 in 1 hug 1 have 1 from 1 for 1 dreams 1 Dream 1 dream 1 chance 1 because 1 are 1 all [root@zabbix ~]# cat xx.txt |awk '{print $2,$1}' you 7 to 6 want 5 what 2 them 2 one 2 life 2 go 2 do 2 be 2 and 2 your 1 where 1 when 1 things 1 There 1 the 1 that 1 someone 1 so 1 real 1 pick 1 only 1 much 1 moments 1 miss 1 just 1 in 1 hug 1 have 1 from 1 for 1 dreams 1 Dream 1 dream 1 chance 1 because 1 are 1 all 1
3.后因为追了信息,导致排序发生变化,再次进行对第二列排序
进行追加信息
[root@zabbix ~]# cat xx.txt |awk '{print $2,$1}' you 7 to 6 want 5 what 2 them 2 one 2 life 2 go 2 do 2 be 2 and 2 your 1 where 1 when 1 things 1 There 1 the 1 that 1 someone 1 so 1 real 1 pick 1 only 1 much 1 moments 1 miss 1 just 1 in 1 hewenfu 66 hug 1 have 1 from 1 for 1 dreams 1 Dream 1 dream 1 chance 1 because 1 are 1 all 1 baba 54
对第二列进行排序
[root@zabbix ~]# cat xx.txt |sort -rk2 you 7 hewenfu 66 to 6 baba 54 want 5 what 2 them 2 one 2 life 2 go 2 do 2 be 2 and 2 your 1 where 1 when 1 things 1 There 1 the 1 that 1 someone 1 so 1 real 1 pick 1 only 1 much 1 moments 1 miss 1 just 1 in 1 hug 1 have 1 from 1 for 1 dreams 1 Dream 1 dream 1 chance 1 because 1 are 1 all 1
(我发现它只能对识别个位数,不能识别个位数以上的,不用了,就用第一列排序,简单,完事!!!)
[root@zabbix ~]# cat xx.txt|awk '{print $2,$1}'|sort -rn 66 hewenfu 54 baba 7 you 6 to 5 want 2 what 2 them 2 one 2 life 2 go 2 do 2 be 2 and 1 your 1 where 1 when 1 things 1 There 1 the 1 that 1 someone 1 so 1 real 1 pick 1 only 1 much 1 moments 1 miss 1 just 1 in 1 hug 1 have 1 from 1 for 1 dreams 1 Dream 1 dream 1 chance 1 because 1 are 1 all
4.按字母出现频率降序排序!grep -o '[a-Z]'
[root@zabbix ~]# echo "There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do."|grep -o '[a-Z]'|sort|uniq -c|sort -rn 27 o 26 e 21 t 19 a 16 n 14 h 12 u 11 m 10 r 9 y 9 w 9 s 6 l 6 i 6 d 5 c 4 g 4 f 3 b 1 v 1 T 1 p 1 k 1 j 1 D
写个shell统计一下系统中有多少普通用户
方法一:
[root@web01 backup]# cat /etc/passwd|grep "bash" root:x:0:0:root:/root:/bin/bash lo:x:2000:2000::/home/lo:/bin/bash kk:x:1009:1009::/home/kk:/bin/bash [root@web01 backup]# cat /etc/passwd|grep "bash"|wc -l 3
方法二:
[root@web01 backup]# awk -F: '$3>1000' /etc/passwd nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin lo:x:2000:2000::/home/lo:/bin/bash kk:x:1009:1009::/home/kk:/bin/bash
写一个shell脚本看看使用最多的命令是哪个
[root@web01 backup]# history|awk '{print $2}'|sort|uniq -c|sort -rn|head
189 vim
89 systemctl
86 ll
58 echo
51 cd
46 sh
40 yum
33 nginx
32 ls
24 rpm
如何批量停止进程
[root@zabbix ~]# ps aux|grep '\.sh$' root 120608 0.0 0.1 113128 1368 pts/1 S 11:34 0:00 sh kk.sh [root@zabbix ~]# ps aux|grep '\.sh$'|awk '{print $2}' 120608 [root@zabbix ~]# ps aux|grep '\.sh$'|awk '{print $2}'|xargs kill -9 [1]+ Killed sh kk.sh
如何找出一个文件里,只有一个数字的行
我们先定义一个文件
root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin dasd dasd 2asdada nobody:x:99:99:Nobody:/:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
然后写脚本
#!/bin/bash file=/etc/passwd.x sum=`wc -l $file|awk '{print $1}'` for i in `seq $sum` do bb=`sed -n "${i}s#[^0-9]##gp" $file|wc -L` #bb=`sed -n "${i}s#[^0-9]##gp" $file|xargs -i expr length {}` if [ $bb -eq 1 ];then sed -n ${i}p $file fi done
[^0-9] 再括号里的^表示取反,除了0-9以外【但是这个无法统计纯数字的行,不知道为什么】
s#[^0-9]##g 除了0-9其它都替换为空【但是这个无法统计纯数字的行,不知道为什么】
[root@web01 ~]# sh ss.sh dasd dasd 2asdada
通过curl命令返回的状态码来判断所访问的网站是否正常,比如当前状态码200才算正常
curl -I www.hebbao.com ----------显示头部信息
curl -s ----------------浸没输出
curl -w %{http_code}---------引用模块
curl -o /dev/null ----------多余信息导入垃圾箱
[root@kk ~]# curl -I www.hebbao.com HTTP/1.1 302 Moved Temporarily Server: nginx/1.16.1 Date: Sat, 01 Feb 2020 08:56:09 GMT Content-Type: text/html Content-Length: 145 Connection: keep-alive Location: https://www.hebbao.com/
[root@kk ~]# curl -Is -w "%{http_code}\n" -o /dev/null www.hebbao.com 302 [root@kk ~]# vim ww.sh #!/bin/bash bb=`curl -Is -w "%{http_code}\n" -o /dev/null www.hebbao.com` [[ $bb =~ 200|301|302 ]];echo $? [root@kk ~]# sh -x ww.sh ++ curl -Is -w '%{http_code}\n' -o /dev/null www.hebbao.com + bb=302 + [[ 302 =~ 200|301|302 ]] + echo 0 0
1-99之间输入用户名进行随机抓阄,结果排序
#!/bin/bash file=xx.txt while true do ran=`echo $((RANDOM%99+1))` exis=`grep -w $ran $file|wc -l` if [ $exis -eq 1 ];then continue fi read -p "请输入姓名: " name if [[ $name = "exit" ]];then echo "抓阄结果如下" break else echo -e "$name\t$ran"|tee -a $file fi done sort -rnk2 $file [root@web01 ~]# sh while.sh 请输入姓名: qwe qwe 86 请输入姓名: asd asd 27 请输入姓名: zxc zxc 59 请输入姓名: cvb cvb 35 请输入姓名: exit 抓阄结果如下 qwe 86 zxc 59 cvb 35 asd 27
防止恶意IP脚本
iptables:
- 登陆服务器日志/var/log/secure;
- 检查日志中认证失败的行并打印其IP地址;
- 将IP地址写入至防火墙;
- 禁止该IP访问服务器SSH 22端口;
- 将脚本加入Crontab实现自动禁止恶意IP;
#!/bin/bash file=/root/black.txt secure=/var/log/secure iptables=/etc/sysconfig/iptables cat less $secure|grep -v "pam_systemd"|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|sort -rn >$file echo cat<<EOF ++++++++++++++welcome to use ssh login drop failed ip+++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++------------------------------------++++++++++++++++++ EOF echo for i in `cat $file` do cat $iptables |grep $i >/dev/null if [ $? -eq 0 ];then sed -i "/lo/a -A INPUT -s $i -m state --state NEW -m tcp -p tcp --dport 22 -j DROP" $iptables else echo "This is $i is exist in iptables,please exit ..." fi done /etc/init.d/iptables restart
firewalld:
#!/bin/bash file=/root/black.txt secure=/var/log/secure FIREWALL_CONF=/etc/firewalld/zones/public.xml cat less $secure|grep -v "pam_systemd"|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|sort -rn >$file echo cat<<EOF ++++++++++++++welcome to use ssh login drop failed ip+++++++++++++++++ ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++------------------------------------++++++++++++++++++ EOF echo systemctl status firewalld > /dev/null 2>&1 if [ $? -eq 0 ];then firewall-cmd --reload > /dev/null 2>&1 echo "firewalld is running" >>$file 2>&1 else echo "Firewalld looks like not running, trying to start..." >> $file 2>&1 systemctl start firewalld > /dev/null 2>&1 systemctl start firewalld > /dev/null 2>&1 if [ $? -eq 0 ]; then echo "Firewalld start successfully..." >> $file 2>&1 else echo "Failed to start firewalld" >> $file 2>&1 fi fi for i in `seq $file` do cat $file |grep $i &>/dev/null if [ $? -eq 0 ];then firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address="$i/32" port port="$SSH_PORT" protocol=tcp drop" &>/dev/null echo `date +"%Y-%m-%d %H:%M:%S $i 此ip增加到防火墙拉黑"` >> /root/drop_ip.txt fi done firewall-cmd --reload >/dev/null 2>&1
实现服务器IP、主机名自动修改及配置
#!/bin/bash ETHCONF=/etc/sysconfig/network-scripts/ifcfg-eth0 HOSTS=/etc/hosts NETWORK=/etc/sysconfig/network DIR=/data/backup/`date +%F` NETMASK=255.255.255.0 echo "----------------------------" judge_ip(){ read -p "Please enter ip Address,example 192.168.0.11 ip": IPADDR echo $IPADDR|grep -v "[Aa-Zz]"|grep --color -E "([0-9]{1,3}\.){3}[0-9]{1,3}" } count_ip(){ count=(`echo $IPADDR|awk -F. '{print $1,$2,$3,$4}'`) IP1=${count[0]} IP2=${count[1]} IP3=${count[2]} IP4=${count[3]} } ip_check() { judge_ip while [ $? -ne 0 ] do judge_ip done count_ip while [ "$IP1" -lt 0 -o "$IP1" -ge 255 -o "$IP2" -ge 255 -o "$IP3" -ge 255 -o "$IP4" -ge 255 ] do judge_ip while [ $? -ne 0 ] do judge_ip done count_ip done } change_ip() { if [ ! -d $DIR ];then mkdir -p $DIR fi echo "The Change ip address to Backup Interface eth0" cp $ETHCONF $DIR grep "dhcp" $ETHCONF if [ $? -eq 0 ];then read -p "Please enter ip Address:" IPADDR sed -i 's/dhcp/static/g' $ETHCONF echo -e "IPADDR=$IPADDR\nNETMASK=$NETMASK\nGATEWAY=`echo $IPADDR|awk -F. '{print $1"."$2"."$3}'`.2" >>$ETHCONF echo "The IP configuration success. !" else echo -n "Static IP has been configured,please confirm whether to modify,yes or No": read i fi if [ "$i" == "y" -o "$i" == "yes" ];then ip_check sed -i -e '/IPADDR/d' -e '/NETMASK/d' -e '/GATEWAY/d' $ETHCONF echo -e "IPADDR=$IPADDR\nNETMASK=$NETMASK\nGATEWAY=`echo $IPADDR|awk -F. '{print $1"."$2"."$3}'`.2" >>$ETHCONF echo "The IP configuration success. !" else echo "Static IP already exists,please exit." exit $? fi } change_hosts() { if [ ! -d $DIR ];then mkdir -p $DIR fi cp $HOSTS $DIR ip_check host=` echo $IPADDR|sed 's/\./-/g'|awk '{print "BJ-IDC-"$0"-hebbao"}'` cat $HOSTS |grep "$host" if [ $? -ne 0 ];then echo "$IPADDR $host" >> $HOSTS echo "The hosts modify success " fi grep "$host" $NETWORK if [ $? -ne 0 ];then sed -i "s/^HOSTNAME/#HOSTNAME/g" $NETWORK echo "NETWORK=$host" >>$NETWORK hostname $host;su fi } PS3="Please Select configuration ip or configuration host:" select i in "modify_ip" "modify_hosts" "exit" do case $i in modify_ip) change_ip ;; modify_hosts) change_hosts ;; exit ;; *) echo -e "1) modify_ip\n2) modify_ip\n3)exit" esac done

评论