今天给大家整理一些实用的 Linux Shell 脚本案例
希望可以帮到大家,让大家更熟悉 shell 编程
1.显示系统一些基本信息
显示信息如下:
系统版本
系统内核
虚拟平台
主机名
ip地址
开机信息有没有报错,有的话输出到屏幕
可以将该脚本加入到开机自启动里面,这样开机就会输出基本信息
#!/bin/bash
info(){
system=$(hostnamectl|grepSystem|awk'{print$3}')
kernel_release=$(hostnamectl|grepKernel|awk-F:'{print$2}')
Virtualization=$(hostnamectl|grepVirtualization|awk'{print$2}')
server_name=$(hostname)
ipaddr=$(hostname-I)
echo"当前系统版本是:${system}"
echo"当前系统内核是:${kernel_release}"
echo"当前虚拟平台是:${Virtualization}"
echo"当前主机名是:${server_name}"
echo"当前ip地址:${ipaddr}"
}
checkerrror(){
error_info=$(dmesg|greperror)
if[-e${error_info}]
then
echo"无错误日志!"
else
ehcho${error_info}
fi
}
info
checkerrror

2.关闭系统防火墙和SELinux
检查防火墙状态,是否安装防火墙,如果安装则关闭 关闭SELinux 清空iptables规则
#!/bin/bash
close_firewalld(){
code=$(systemctlstatusfirewalld)
if[${code}-eq0]
then
systemctlstopfirewalld
fi
}
close_selinux(){
sed-i'/^SELINUX/s/=.*/=disabled/'/etc/selinux/config
setenforce0
}
close_iptables(){
iptables-F
serviceiptablessave
serviceiptablesrestart
}
close_firewalld
close_selinux
close_iptables
3.定时任务计划:归档备份
打包压缩/var/log/nginx目录下所有内容,存放在/tmp/nginx目录里
压缩文件命名规范:yymmdd_logs.tar.gz,只保存七天内的文件,超过七天的文件会进行清理
#!bin/bash date="$(date+%Y%m%d)" dir='/tmp/nginx' backupfile='yymmdd_logs.tar.gz' #查看/tmp/nginx是否存在,不存在则创建 checkbak(){ if[!-e${dir}] then mkdir${dir} fi } #压缩文件 backup(){ tar-zcvf${dir}/${backupfile}/var/log/nginx/>/dev/null2>&1 echo"${backupfile}Compressed and packaged successfully !" } #清除七天过期文件 cleanup(){ find${dir}-typef-mtime+7|xagrsrm-rf if[$?-eq0] then echo"Cleaned up successfully!" else echo"datacleaningfailederror,pleasepayattentionintime" fi } checkbak backup cleanup
4.自动批量创建用户
批量创建user1、user2、user3.....
#!/bin/bash
#检查用户是否存在,不存在则创建
checkuser(){
foriin$(seq120)
do
iduser${i}>/dev/null2>&1
if[$?-eq0]
then
echo"user${i}已存在!"
else
useradduser${i}&&echo"user${i}"|passwd--stdinuser${i}>/dev/null2>&1
fi
done
}
checkuser
5.通过位置参数创建用户
$1 是执行脚本的第一个参数
$2 是执行脚本的第二个参数
#!/bin/bash
checkuser(){
id${1}>/dev/null2>&1
if[$?-eq0]
then
echo"${1}已存在!"
else
useradd"$1"
echo"$2"|passwd‐‐stdin"$1"
fi
}
6.批量删除用户
批量删除user1...user20
#!/bin/bash
#检查用户是否存在,存在则删除
checkuser(){
foriin$(seq120)
do
iduser${i}>/dev/null2>&1
if[$?-eq0]
then
userdel-ruser${i}
else
echo"user${i}不存在!"
fi
done
}
checkuser
7.更新系统时间,并写入硬件时间里
查看是否安装ntpdate工具
创建上海时区文件的软链接
更新时间并写入到硬件时间里
#!/bin/bash
package="ntpdate"
info=$(rpm-q${package})
check_pkgs(){
if[!-e${info}]
then
echo"ntpdate already exists!"
else
echo"start installation!"
yumcleanall>/dev/null2>&1
fi
yumupdate-y&&yuminstall-y${package}>/dev/null2>&1
fi
}
modify_time(){
echo"开始修改时间"
rm-rf/etc/localtime&&ln-s/usr/share/zoneinfo/Asia/Shanghai/etc/localtime
/usr/sbin/ntpdatecn.pool.ntp.org>/dev/null2>&1&&hwclock-w
}
check_pkgs
modify_time
8.检查服务运行状态
检查某一服务是否正常运行,执行脚本的时候第一个参数为服务名
#!/bin/bash result=$(pidof$1|wc-l) echo${result} if[${result}-eq0] then echo"service does not exist !" else echo"Service is running normally !" fi

9.对目标主机进行心跳检测
ping目标主机看是否ping得通,三次ping通表示主机正常运行
将目标主机的ip地址作为第一个参数传进去
#!/bin/bash
ipaddr=$1
echo${ipaddr}
ping_status(){
ifping-c1${ipaddr}>/dev/null2>&1
then
echo"ping${ipaddr}issuccessful!"
continue
fi
}
foriin$(seq13)
do
ping_status
echo"ping${ipaddr}isfailure!"
done

进阶版:对ip地址池里的主机分别进行心跳检测
ipaddr=(192.168.149.131192.168.149.130192.168.149.132192.168.149.133)
foriin${ipaddr[*]}
do
echo"....begintoping${i}....."
ifping-c3${i}>/dev/null2>&1
then
echo"ping${i}issuccessful!"
else
echo"ping${i}isfailure!"
fi
done

10.系统磁盘内存容量告警
根分区剩余空间小于20%(即使用空间大于80%) 输出告警信息 内存使用空间大于80% 输出告警信息 配合crond每5分钟检查一次
#!/bin/bash disk_letfspace=$(df-Th|grep-w/|awk'{print$6}'|cut-d%-f1) mem_used=$(free-m|grepMem|awk'{print$3}') mem_total=$(free-m|grepMem|awk'{print$2}') mem_letfspace=$[${mem_used}*100/${mem_total}] if[${disk_letfspace}-gt80] then echo"Diskfreespaceislessthan20%!" else echo"${disk_letfspace}%ofdiskspaceleft" fi if[${mem_letfspace}-gt80] then echo"memoryspaceislessthan20%!" else echo"${mem_letfspace}%ofmemoryspaceleft" fi
crontab-l */5****/root/check_space.sh

审核编辑:刘清
-
Linux
+关注
关注
87文章
11298浏览量
209382 -
磁盘
+关注
关注
1文章
376浏览量
25203 -
Shell
+关注
关注
1文章
365浏览量
23360
原文标题:【干货】10个实用 Linux Shell 脚本案例
文章出处:【微信号:释然IT杂谈,微信公众号:释然IT杂谈】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
如何使用Shell脚本在 Linux 服务器寻找攻击证据
如何利用Shell脚本掩藏Linux服务器使用痕迹
如何创建和执行一个简单的Linux shell脚本
34个Linux Shell脚本汇总
通过Shell脚本掩盖Linux系统上的操作痕迹
Linux Shell脚本经典案例分享

整理一些实用的Linux Shell脚本案例
评论