设为首页 收藏本站
查看: 412|回复: 0

[经验分享] 收集的几个比较常用的Linux脚本

[复制链接]
累计签到:34 天
连续签到:2 天
发表于 2018-4-19 15:24:04 | 显示全部楼层 |阅读模式
1、编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: systeminfo.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
echo “The CPU of the system is:” `lscpu |grep “name” |tr -s ” ” |cut -d ” ” -f 5`
echo “The memory size of the system is:” `free -h |grep “Swap” |tr -s ” ” |cut -d” ” -f 2`
echo “The size of the system’s hard disk is :” `lsblk |grep -w “sda” |tr -s ” ” |cut -d ” ” -f 4`
echo “The system version is :” `cat /etc/centos-release |grep -o ” [0-9]”`
echo “The kernel version is :” `uname -r`
echo “The name of the system host :” `hostname`
echo “The IP of the system is :” `ifconfig ens33 |sed -n “2p” |sed “s/.*inet//” |sed “s/net.*//”`



2、编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: backup.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
echo -e “\e[1;31mStart a backup…\e[0m”
sleep 5
cp -av /etc/ /data/etc`date “+%F”`
echo -e “\e[1;33mBackup completion\e[0m”



3、编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: disk.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
echo “The maximum value of the hard disk is :” `df |grep “sda” |tr -s ” ” |cut -d” ” -f 5 |sort -nr |head -n1`



4.编写脚本/root/bin/sumid.sh,计算/etc/passwd文件中的第10个用户和第
20用户的ID之和

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: sumid.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
UID1=`cat /etc/passwd |head -n10 | tail -n1 |cut -d: -f 3`
UID2=`cat /etc/passwd |head -n20 | tail -n1 |cut -d: -f 3`
let UID3=UID1+UID2
echo $UID3



5. 编写脚本/root/bin/sumspace.sh,传递两个文件路径作为参数给脚本,计
算这两个文件中所有空白行之和

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: sumspace.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
blabk1=`cat /$1 |grep -c “^$”`
blabk2=`cat /$2 |grep -c “^$”`
let blabk=blabk1+blabk2
echo $blabk



6、编写脚本/root/bin/sumfile.sh,统计/etc, /var, /usr目录中共有多少个一级
子目录和文件

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: sumfile.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
file1=`ls -l /etc |grep -c -e “^d” -e “^-“`
file2=`ls -l /var |grep -c -e “^d” -e “^-“`
file3=`ls -l /usr |grep -c -e “^d” -e “^-“`
let file4=file1+file2+file3
echo $file4



7.编写脚本/root/bin/hostping.sh,接受一个主机的IPv4地址做为参数,测
试是否可连通。如果能ping通,则提示用户“该IP地址可访问” ;如果不可
ping通,则提示用户“该IP地址不可访问”

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: hostping.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
[[ $1 =~ “\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>” ]] && echo “Please enter a correct IP” && exit
ping -c1 $1>/dev/null && echo “The IP can be accessed” || echo “The IP is not accessible”



8.编写脚本/root/bin/checkdisk.sh,检查磁盘分区空间和inode使用率,如
果超过80%,就发广播警告空间将满

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: checkdisk.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
disk=`df |grep “sda”|tr -s ” ” “%” |cut -d”%” -f 5 |sort -nr |head -n1`
[ “$disk” -ge 40 ] && echo “The use rate of disk partitions is now :” $disk && wall Space will be full



9. 编写脚本/root/bin/excute.sh ,判断参数文件是否为sh后缀的普通文件,如
果是,添加所有人可执行权限,否则提示用户非脚本文件

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: per.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
[ $# -ne 1 ] && echo “You have to enter one” && exit
[ ! -r $1 -a -w $1 ] && echo “The document is unreadable and cannot be written.” || echo “Read and write permissions to the file”



10. 编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统

创建nologin文件

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: nologin.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
[ -a /etc/nologin ] && echo “The nologin file exists! “|| (touch /etc/nologin; echo -e “\e[1;31mThe nologin file is created!\e[0m “;)

删除nologin文件

#!/bin/bash
#
#********************************************************************
#Author: zhouyafei
#Date: 2018-04-18
#FileName: login.sh
#Description: The test script
#Copyright (C): 2018 All rights reserved
#********************************************************************
[ -a /etc/nologin ] && echo -e `rm -f /etc/nologin` -e “\e[1;31mThe file has been deleted!\e[0m” || echo “The file does not exist”

11. 让所有用户的PATH环境变量的值多出一个路径,例如:/usr/local/apache/bin

echo $PATH |sed -r “s@(/usr/local/sbin:)@/usr/local/apache/bin:\1@”


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-449127-1-1.html 上篇帖子: centos6 截图 CutyCapt 下篇帖子: centos7 zabbix3.4.6显示中文乱码问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表