查看磁盘/当前使用状态,如果使用率超过80%则报警发邮件
//怎么看磁盘使用率,怎么获取对应的值
$ cat disk_use.sh
#!/bin/sh
DATE=$(date +%F)
Disk_Use=$(df -h|grep '/$'|awk '{print $5}'|awk -F% '{print $1}')
if [ $Disk_Use -ge 10 ]
then
echo "Disk Is use ${Disk_Use}%" >/tmp/${DATE}-diskuse.txt
fi
内存超过百分80报警输出
$ cat memory_use.sh
#!/bin/sh
Mem_Use=$(free -m|grep ^M|awk '{print $3/$2*100}')
if [ ${Mem_Use%.*} -ge 30 ]
then
echo "Memory IS ERROR ${Mem_Use%.*}%"
else
echo "Memory IS OK ${Mem_Use%.*}%"
fi
自己写一个监控负载状态的脚本
ab -n 200000 -c 2000 http://127.0.0.1/index.html
自己用脚本打印出 系统版本 内核版本平台 虚拟平台 静态主机名 eth0网卡IP地址 lo网卡IP地址 当前外网IP地址
curl icanhazip.com
$ vim hostnamectl.sh
#!/bin/bash
System=$(hostnamectl |grep System|awk '{print $3,$4,$5}')
Kernel=$(hostnamectl |grep Kernel|awk '{print $2,$3}')
Vt=$(hostnamectl |grep Virtualization|awk '{print $2}')
Statichostname=$(hostnamectl |grep Static|awk '{print $3}')
Eth0=$(ifconfig eth0|awk 'NR==2{print $2}')
Lo=$(ifconfig lo|awk 'NR==2{print $2}')
#Network_T=$(curl -s icanhazip.com)
echo "当前系统版本是: $System"
echo "当前系统内核是: $Kernel"
echo "当前虚拟平台是: $Vt"
echo "当前静态主机名是: $Statichostname"
echo "当前eth0IP是: $Eth0"
echo "当前lo地址是: $Lo"
#echo “当前外网地址是: $Network_T”
$ sh hostnamectl.sh
当前系统版本是: CentOS Linux 7
当前系统内核是: Linux 3.10.0-862.el7.x86_64
当前虚拟平台是: vmware
当前静态主机名是: master1
当前eth0IP是: 10.0.0.166
当前lo地址是: 127.0.0.1
## 公司服务器较多,把这个脚本放到坏境变量下
$ cat num.sh
#!/bin/bash
read -p "please input num: " num
if
[[ ! "$num"=~^[0-9]+$ ]];then
echo "请输入数字"
exit 1
fi
echo "Number Is $num"
#!/bin/sh
read -p "Please Input number " num
if [[ ! $num =~ ^[0-9]+$ ]]
then
echo "Please Input 数字"
exit 1
fi
read -p "Please Input prefix " prefix
if [ -z "$#prefix" ]
then
echo "Please Input 前缀"
exit 2
fi
for i in `seq "$num"`
do
USER=$prefix$i
useradd $USER
echo "123"|passwd --stdin $USER &>/dev/null
if [ $? -eq 0 ];then
echo "$USER Is create ok"
fi
done
随机输出一个1-100的数字
要求用户输入数字(数字处加判断)
如果比随机数小则提示比随机数小了 大则提示比随机数大了
正确则退出 错误则继续死循环
最后统计猜了多少次(猜对的情况
#!/bin/sh
num=$(echo $((RANDOM%100+1)))
i=1
while true
do
read -p "请输入随机数字1-100: " num1
if [[ ! $num1=~^[0-9]+$ ]];then
echo "请输入数字1-100"
continue
fi
if [ $num1 -lt $num ];then
echo "输入的数字太小了"
elif [ $num1 -gt $num ];then
echo "你输入放入数字太大了"
elif [ $num1 -eq $num ];then
echo "猜中了"
break
fi
let i++
done
echo "你一共猜了$i次"
#!/bin/sh
#Version v1.0
#Author lzy
os_version=$(cat /etc/redhat-release|awk '{print $4}')
if [ "$os_version" = "(Final)" ]
then
os_version=$(cat /etc/redhat-release|awk '{print $3}')
fi
if [ ${os_version%%.*} -eq 7 ]
then
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
elif [ ${os_version%%.*} -eq 6 ]
then
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
yum makecache
elif [ ${os_version%%.*} -eq 5 ]
then
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
else
echo "Please check version"
fi
#!/usr/bin/bash
#install php
install_php56() {
yum install epel-release
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof
}
install_php70() {
echo "install php7.0......"
}
install_php71() {
echo "install php7.1......"
}
while :
do
echo "################################"
echo -e "\t1 php-5.6"
echo -e "\t2 php-7.0"
echo -e "\t3 php-7.1"
echo -e "\tq exit"
echo "################################"
read -p "version[1-3]: " version
if [ "$version" = "1" ];then
install_php56
elif [ "$version" = "2" ];then
install_php70
elif [ "$version" = "3" ];then
install_php71
elif [ "$version" = "q" ];then
exit
else
echo "error"
fi
done
#!/bin/bash
read -p "请输入你要删除用户的前缀,以及用户的位数: " delname delnum
echo "你将要删除如下账户
用户的前缀: $delname
用户的个数: $delnum
"
read -p "你确定要删除吗[y|yes|n|no]?" reday
for i in $(seq $delnum);
do
userfull=$delname$i
case $reday in
y|yes)
id $userfull &>/dev/null
if [ $? -eq 0 ];then
userdel $userfull &>/dev/null
echo "userdel is ok $userfull...."
else
echo "$userfull" no such user
fi
;;
n|no)
exit 1
;;
*)
read -p "你确定要删除吗[y|yes|n|no]?" reday
esac
done
trap
信号说明
HUP(1) 挂起,通常因终端掉线或用户退出而引发
INT(2) 中断,通常因按下Ctrl+C组合键而引发
QUIT(3) 退出,通常因按下Ctrl+组合键而引发
ABRT(6) 中止,通常因某些严重的执行错误而引发
ALRM(14) 报警,通常用来处理超时
TERM(15) 终止,通常在系统关机时发送
SIGTSTP 停止进程 终端来的停止信号
[root@web02 ~]# cat /home/lizhenya/jumpserver.sh
#!/usr/bin/bash
#jumpServer
Mysql_master=10.0.0.10
Mysql_slave1=10.0.0.11
Mysql_slave2=10.0.0.12
Nginx_Up=10.0.0.6
Nginx_WEB1=10.0.0.7
Nginx_WEB2=10.0.0.8
meminfo(){
cat <<-EOF
-------------------------------
| 1) mysql-master |
| 2) mysql-slave1 |
| 3) mysql-slave2 |
| 4) Nginx-Upstream |
| 5) Nginx-WebNode1 |
| 6) Nginx-WebNode2 |
| h) help |
---------------------------------
EOF
}
#调用函数打印菜单
meminfo
#控制不让输入ctrl+c,z
trap "" HUP INT TSTP
while true
do
read -p "请输入要连接的主机编号: " num
case $num in
1|mysql-master)
ssh root@$Mysql_master
;;
2|Mysql_slave1)
ssh root@$Mysql_slave1
;;
3|Mysql_slave2)
ssh root@$Mysql_slave2
;;
h|help)
clear
meminfo
;;
#退出脚本后门, 不要让其他人知道
exec)
break
;;
esac
done
[root@lizhenya ~]# cat start_nginx.sh
#!/usr/bin/bash
# manager Nginx start stop restart reload
source /etc/init.d/functions
act=$1
te(){
if [ $? -eq 0 ];then
action "Nginx Is $act" /bin/true
else
action "Nginx Is $act" /bin/false
fi
}
start(){
/usr/sbin/nginx &>/dev/null
te
}
stop(){
/usr/sbin/nginx -s stop &>/dev/null
te
}
reload(){
/usr/sbin/nginx -s reload
te
}
status(){
Ngx_status=$(ps aux|grep "[n]ginx"|grep master|awk '{print $2}')
Nginx_Status_Port=$(netstat -lntp|grep nginx|awk '{print $4}')
echo "Nginx_status_Pid: $Ngx_status"
echo "Nginx_status_Port: $Nginx_Status_Port"
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 1
start
;;
reload)
reload
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|status|restart|reload|}"
esac
案列13 (批量获取在线主机, 进行秘钥批量分发) |
cat for_ip.sh
#!/usr/bin/bash
#setup1 拿到IP地址
>ip.txt
for i in {1..10}
do
ip=10.0.0.$i
{
ping -c1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip" >> ip.txt
fi
}&
done
#2.生成对应的密钥
if [ ! -f ~/.ssh/id_rsa ];then
ssh-keygen -P "" -f ~/.ssh/id_rsa
fi
#3.批量分发密钥
while read line
do
/usr/bin/expect <<-EOF
set pass 1
set timeout 2
spawn ssh-copy-id $line -f
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "1\r"}
}
expect eof
EOF
done<ip.txt
案列14 ( for循坏ping 主机)
#!/bin/bash
for i in {1..10}
do
{
ip=10.0.0.$i
ping -c 4 $ip &>/dev/null
if [ $? -eq 0 ];then
echo "$ip is ok " >>/root/ip.txt
else
echo "$ip is err" >>/root/ip.txt
fi
}&
done
wait
echo "获取在线ip完成"
$ vim user.txt
lisi
zhangsan
$ vim user.sh
#!/bin/sh
for i in `cat user.txt`
do
id $i &>/dev/null
if [ $? -ne 0 ];then
useradd $i
echo "123"|passwd --stdin $i &>/dev/null
echo "Create $i OK"
else
echo "useradd: $i already exists"
fi
done
#!/bin/sh
while read line
do
id $line &>/dev/null
if [ $? -eq 0 ];then
echo "useradd: user $line already exists"
else
useradd $line
if [ $? -eq 0 ];then
echo "create $line success"
fi
fi
done<user.txt
-----------------------------------------------------------------------------
##文件中存在用户和密码
#!/bin/sh
while read user
do
u=$(echo $user|awk '{print $1}')
p=$(echo $user|awk '{print $2}')
id $u &>/dev/null
if [ $? -ne 0 ];then
useradd $u
echo $p|passwd --stdin $u &>/dev/null
echo "Create $u is ok"
else
echo "useradd: $u already exists"
fi
done<user.txt
$ cat read.sh
#!/bin/sh
File=/etc/passwd
function count(){
local i=0
while read line
do
let ++i
done<$File
echo $i
}
$ cat fun3.sh
file=/etc/ttt # 定义文件
t_file(){ # 函数判断
if [ -f $file ];then
return 0
else
return 1
fi
}
# 调用函数
t_file
#根据函数返回状态码进行输出
if [ $? -eq 0 ];then
echo "该文件存在 $file"
else
echo "该文件不存在 $file"
fi
$ cat fun3.sh
t_file(){ # 函数判断
if [ -f $1 ];then
echo "$1 exists"
else
return 1
fi
}
# 调用函数
t_file $1
$ cat fun.sh
#!/bin/sh
# 定义一个函数
count01(){
num=10
for i in `seq 10`
do
total=$[$i + $num]
done
echo "计算结果是: $total"
}
count01 # 调用函数
$ sh fun.sh
计算结果是: 20
$ cat fun.sh
#!/bin/sh
count01(){
for i in `seq 10`
do
total=$[$i + $num]
done
echo "计算结果是: $total"
}
num=10
count01
$ sh fun.sh
计算结果是: 20
$ cat fun.sh
#!/bin/sh
count01(){
num=$1
for i in `seq $num`
do
total=$[$i + $num]
done
echo "计算结果是: $total"
}
count01 $1
count01 $2
count01 $3
$ sh fun.sh 10 20 30
计算结果是: 20
计算结果是: 40
计算结果是: 60
传参使用场景:
$ cat nginx.sh
source /etc/init.d/functions
Test() {
if [ $? -eq 0 ];then
action "Nginx $1 is ok" /bin/true
else
action "Nginx $1 is error" /bin/false
fi
}
case $1 in
start)
nginx
Test $1
;;
stop)
nginx -s stop
Test $1
;;
reload)
nginx -s reload
Test $1
;;
*)
echo "USAGE: $0 { start|stop|reload }"
esac
函数的返回值
echo 返回函数返回值
return 返回指定函数退出状态码
---
$ cat fun2.sh
fun2() {
echo 100
return 1
}
result=`fun2`
echo "函数的状态码是: $?"
echo "函数的返回值是: $result"
[root@lizhenya ~]# sh fun2.sh
函数的状态码是: 1
函数的返回值是: 100
## 思路
let array_pa[m]++
let array_pa[f]++
let array_pa[x]++
let array_pa[m]++
$ echo ${array_pa[*]}
2 1 1
$ echo ${!array_pa[*]} ##查看索引(看下标
m f x
## 脚本
[root@master1 scripts]#cat count_sex.sh
#!/bin/sh
declare -A sex
while read line
do
type=$(echo $line|awk '{print $2}')
let sex[$type]++
done<sex.txt
#<table><tr><td bgcolor=Aqua>遍历数组
for i in ${!sex[*]}
do
echo $i共有${sex[$i]}个
done
[root@master1 scripts]# # cat nginx_count.sh
#!/bin/sh
declare -A array_nginx
while read line
do
type=$(echo $line|awk '{print $1}')
let array_nginx[$type]++
done</var/log/nginx/access.log
for i in ${!array_nginx[*]}
do
echo "IP是 $i 共出现了${array_nginx[$i]}次"
done
[root@master1 scripts]# cat nginx_status.sh
#!/bin/sh
declare -A array_nginx
type=`ss -an|grep 80|awk '{print $2}'`
for i in $type
do
let array_nginx[$i]++
done
for n in ${!array_nginx[*]}
do
echo $n ${array_nginx[$n]}
done