(收集整理)本页所列脚本只做学习之用,请自行测试。盲目使用产生后果与作者无关。
Linux实用运维脚本 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 30 31 32 33 34 35 lsof -i du -ak target_dir | sort -nrk 1 | head -n 4cat message.txt | wall screen find . \( -name "*.txt" -o -name "*.pdf" \) -print split -b 10k data.file comm A.txt B.txt -3 | sed 's/^\t//' sed '/^$/d' file
mysql备份 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 #!/bin/bash set -e USER="backup" PASSWORD="backup" DATA_DIR="/data/mysql" BIN_INDEX=$DATA_DIR "/mysql-bin.index" BACKUP_DIR="/data/backup/mysql" BACKUP_LOG="/var/log/mysql/backup.log" DATE=`date +"%Y%m%d" ` TIME=`date +"%Y%m%d%H" ` LOG_TIME=`date +"%Y-%m-%d %H:%M:%S" ` DELETE_BINLOG_TIME="7 day" INCREMENT_INTERVAL="3 hour" note () { printf "[$LOG_TIME ] note: $*\n" >> $BACKUP_LOG ; }warning () { printf "[$LOG_TIME ] warning: $*\n" >> $BACKUP_LOG ; }error () { printf "[$LOG_TIME ] error: $*\n" >> $BACKUP_LOG ; exit 1; }full_backup () { local dbs=`ls -l $DATA_DIR | grep "^d" | awk -F " " '{print $9}' ` for db in $dbs do local backup_dir=$BACKUP_DIR "/full/" $db local filename=$db "." $DATE local backup_file=$backup_dir "/" $filename ".sql" if [ ! -d $backup_dir ] then mkdir -p $backup_dir || { error "创建数据库 $db 全量备份目录 $backup_dir 失败" ; continue ; } note "数据库 $db 全量备份目录 $backup_dir 不存在,创建完成" ; fi note "full backup $db start ..." mysqldump --user=${USER} --password=${PASSWORD} --flush-logs --skip-lock-tables --quick $db > $backup_file || { warning "数据库 $db 备份失败" ; continue ; } cd $backup_dir tar -cPzf $filename ".tar.gz" $filename ".sql" rm -f $backup_file chown -fR mysql:mysql $backup_dir note "数据库 $db 备份成功" ; note "full backup $db end." done }increment_backup () { local StartTime=`date "-d $INCREMENT_INTERVAL ago" +"%Y-%m-%d %H:%M:%S" ` local DELETE_BINLOG_END_TIME=`date "-d $DELETE_BINLOG_TIME ago" +"%Y-%m-%d %H:%M:%S" ` local dbs=`ls -l $DATA_DIR | grep "^d" | awk -F " " '{print $9}' ` mysql -u$USER -p$PASSWORD -e "purge master logs before '$DELETE_BINLOG_END_TIME '" && note "delete $DELETE_BINLOG_TIME days before log" ; filename=`cat $BIN_INDEX | awk -F "/" '{print $2}' ` for i in $filename do for db in $dbs do local backup_dir=$BACKUP_DIR "/increment/" $db local filename=$db "." $TIME local backup_file=$backup_dir "/" $filename ".sql" if [ ! -d $backup_dir ] then mkdir -p $backup_dir || { error "创建数据库 $db 增量备份目录 $backup_dir 失败" ; continue ; } note "数据库 $db 增量备份目录 $backup_dir 不存在,创建完成" ; fi note "increment backup $db form time $StartTime start ..." mysqlbinlog -d $db --start-datetime="$StartTime " $DATA_DIR /$i >> $backup_file || { warning "数据库 $db 备份失败" ; continue ; } note "increment backup $db end." done done for db in $dbs do local backup_dir=$BACKUP_DIR "/increment/" $db local filename=$db "." $TIME local backup_file=$backup_dir "/" $filename ".sql" cd $backup_dir tar -cPzf $filename ".tar.gz" $filename ".sql" rm -f $backup_file note "数据库 $db 备份成功" ; done }case "$1 " in full) full_backup ;; increment) increment_backup ;; *) exit 2 ;;esac exit 1
目录备份 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 30 31 32 33 #!/bin/bash DATE=$(date '+%Y-%m-%d_%H_%M_%S' ) BACKUPDIR="/home/backups" SORFILE=/opt DESFILE=/home/backups/$SORFILE .$(date '+%Y-%m-%d_%H_%M_%S' ).zip [ ! -d $BACKUPDIR ] && mkdir -p $BACKUPDIR cd $BACKUPDIR echo "start backup $SORFILE ..." sleep 3 zip -r $DESFILE $SORFILE &>/dev/nullif [ "$?" == "0" ]then echo $(date +%Y-%m-%d)" zip sucess" >>backup.logelse echo $(date +%Y-%m-%d)" zip failed" >>backup.log exit 0fi find $BACKUPDIR -type f -ctime +3 | xargs rm -rf
PING查询 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #!/bin/bash for ip in 192.168.0.{1..255}do ( ping $ip -c 2 &> /dev/null if [ $? -eq 0 ];then echo $ip is alive fi )&done wait
磁盘IO检查 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 30 31 32 lsof ls /proc/pid/fd sar -pd 10 3 iotopcat /proc/meminfo cat /proc/sys/vm/nr_pdflush_threadscat /sys/block/[disk]/queue/scheduler $ echo deadline > /sys/block/sdX/queue/scheduler $ echo 4096 > /sys/block/sdX/queue/nr_requests
性能相关
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 iostat iostat 2 3 iostat -d sda1 iostat -t iostat -m iostat -d -k 1 1 iostat -d -x -k 1 1 iostat -c 1 3 pidstat pidstat -d 1 pidstat -u -r -d -t 1uptime top htop mpstat -P ALL 1 iostat -xkdz 1 vmstat 1 free nicstat -z 1 dstat 1 sar netstat -s pidstat 1 pidstat -d 1 strace -tttT -p 12670 strace -c dd if =/dev/zero of=/dev/null bs=512 count=1024k tcpdump -nr /tmp/out.tcpdump btrace /dev/sdb iotop -bod5 slabtop -sc sysctl -a perf stat gzip file1 perf record -a -g -F 997 sleep 10
进程相关
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 ps ps aux ps -f -u www-data ps -C apache2 ps aux --sort =-pcpu | head -5 ps -f --forest -C apache2 ps -o pid,uname ,comm -C apache2 ps --ppid 2359 ps -p 3150 -L ps -e -o pid,comm ,etime watch -n 1 'ps -e -o pid,uname,cmd,pmem,pcpu --sort=-pmem,-pcpu | head -15' jobs jobs -pjobs -nkill 12kill -2 123kill -9 123kill -lkill -l KILLkill -u peidalinuxkill -9 $(ps -ef | grep peidalinux) fg 123bg 123nohup command > myout.file 2>&1 & at 12:00(时间) //at命令设定12:00执行一项操作 atq atrm 1 //删除计划任务1 pstreenice [-n <优先等级>][--help ][--version][命令]nice -n 5 ls date ;sleep 1m;date renice -5 -p 5200 pmap 20367
javadump.sh 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 #!/bin/sh DUMP_PIDS=`ps --no-heading -C java -f --width 1000 |awk '{print $2}' `if [ -z "$DUMP_PIDS " ]; then echo "The server $HOST_NAME is not started!" exit 1;fi DUMP_ROOT=~/dumpif [ ! -d $DUMP_ROOT ]; then mkdir $DUMP_ROOT fi DUMP_DATE=`date +%Y%m%d%H%M%S` DUMP_DIR=$DUMP_ROOT /dump-$DUMP_DATE if [ ! -d $DUMP_DIR ]; then mkdir $DUMP_DIR fi for PID in $DUMP_PIDS ; do $JAVA_HOME /bin/jstack $PID > $DUMP_DIR /jstack-$PID .dump 2>&1 echo -e ".\c" $JAVA_HOME /bin/jinfo $PID > $DUMP_DIR /jinfo-$PID .dump 2>&1 echo -e ".\c" $JAVA_HOME /bin/jstat -gcutil $PID > $DUMP_DIR /jstat-gcutil-$PID .dump 2>&1 echo -e ".\c" $JAVA_HOME /bin/jstat -gccapacity $PID > $DUMP_DIR /jstat-gccapacity-$PID .dump 2>&1 echo -e ".\c" $JAVA_HOME /bin/jmap $PID > $DUMP_DIR /jmap-$PID .dump 2>&1 echo -e ".\c" $JAVA_HOME /bin/jmap -heap $PID > $DUMP_DIR /jmap-heap-$PID .dump 2>&1 echo -e ".\c" $JAVA_HOME /bin/jmap -dump:format=b,file=$DUMP_DIR /jmap-dump-$PID .dump $PID 2>&1 echo -e ".\c" if [ -r /usr/sbin/lsof ]; then /usr/sbin/lsof -p $PID > $DUMP_DIR /lsof-$PID .dump echo -e ".\c" fi done if [ -r /usr/bin/sar ]; then /usr/bin/sar > $DUMP_DIR /sar.dumpecho -e ".\c" fi if [ -r /usr/bin/uptime ]; then /usr/bin/uptime > $DUMP_DIR /uptime.dumpecho -e ".\c" fi if [ -r /usr/bin/free ]; then /usr/bin/free -t > $DUMP_DIR /free.dumpecho -e ".\c" fi if [ -r /usr/bin/vmstat ]; then /usr/bin/vmstat > $DUMP_DIR /vmstat.dumpecho -e ".\c" fi if [ -r /usr/bin/mpstat ]; then /usr/bin/mpstat > $DUMP_DIR /mpstat.dumpecho -e ".\c" fi if [ -r /usr/bin/iostat ]; then /usr/bin/iostat > $DUMP_DIR /iostat.dumpecho -e ".\c" fi if [ -r /bin/netstat ]; then /bin/netstat > $DUMP_DIR /netstat.dumpecho -e ".\c" fi echo "OK!"
常用工具安装 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 #!/usr/bin/env bash BLACK="\033[1;30m" RED="\033[1;31m" GREEN="\033[1;32m" YELLOW="\033[1;33m" BLUE="\033[1;34m" PURPLE="\033[1;35m" CYAN="\033[1;36m" RESET="$(tput sgr0) " printf "${BLUE} \n" cat << EOF ################################################################################### # 安装常用命令工具 # 命令工具清单如下: # 核心工具:df、du、chkconfig # 网络工具:ifconfig、netstat、route、iptables # IP工具:ip、ss、ping、tracepath、traceroute # DNS工具:dig、host、nslookup、whois # 端口工具:lsof、nc、telnet # 下载工具:curl、wget # 编辑工具:emacs、vim # 流量工具:iftop、nethogs # 抓包工具:tcpdump # 压缩工具:unzip、zip # 版本控制工具:git、subversion # ################################################################################### EOF printf "${RESET} \n" printf "\n${GREEN} >>>>>>>>> 安装常用命令工具开始${RESET} \n" printf "\n${CYAN} >>>> install coreutils(df、du)${RESET} \n" yum install -y coreutilsprintf "\n${CYAN} >>>> install chkconfig${RESET} \n" yum install -y chkconfigprintf "\n${CYAN} >>>> install net-tools(ifconfig、netstat、route)${RESET} \n" yum install -y net-toolsprintf "\n${CYAN} >>>> install iptables${RESET} \n" yum install -y iptablesprintf "\n${CYAN} >>>> install iputils(ping、tracepath)${RESET} \n" yum install -y iputilsprintf "\n${CYAN} >>>> install traceroute${RESET} \n" yum install -y tracerouteprintf "\n${CYAN} >>>> install iproute(ip、ss)${RESET} \n" yum install -y iprouteprintf "\n${CYAN} >>>> install lsof${RESET} \n" yum install -y lsofprintf "\n${CYAN} >>>> install nc${RESET} \n" yum install -y ncprintf "\n${CYAN} >>>> install netstat${RESET} \n" yum install -y netstatprintf "\n${CYAN} >>>> install bind-utils(dig、host、nslookup)${RESET} \n" yum install -y bind-utilsprintf "\n${CYAN} >>>> install whois${RESET} \n" yum install -y whoisprintf "\n${CYAN} >>>> install curl${RESET} \n" yum install -y curlprintf "\n${CYAN} >>>> install wget${RESET} \n" yum install -y wgetprintf "\n${CYAN} >>>> install emacs${RESET} \n" yum install -y emacsprintf "\n${CYAN} >>>> install vim${RESET} \n" yum install -y vimprintf "\n${CYAN} >>>> install iftop${RESET} \n" yum install -y iftopprintf "\n${CYAN} >>>> install nethogs${RESET} \n" yum install -y nethogsprintf "\n${CYAN} >>>> install tcpdump${RESET} \n" yum install -y tcpdumpprintf "\n${CYAN} >>>> install unzip${RESET} \n" yum install -y unzipprintf "\n${CYAN} >>>> install git${RESET} \n" yum install -y gitprintf "\n${CYAN} >>>> install subversion${RESET} \n" yum install -y subversionprintf "\n${GREEN} <<<<<<<< 安装常用命令工具结束${RESET} \n"
常用lib库安装 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 #!/usr/bin/env bash BLACK="\033[1;30m" RED="\033[1;31m" GREEN="\033[1;32m" YELLOW="\033[1;33m" BLUE="\033[1;34m" PURPLE="\033[1;35m" CYAN="\033[1;36m" RESET="$(tput sgr0) " printf "${BLUE} \n" cat << EOF ################################################################################### # 安装常见 lib # 如果不知道命令在哪个 lib,可以使用 yum search xxx 来查找 # lib 清单如下: # gcc gcc-c++ kernel-devel libtool # openssl openssl-devel # zlib zlib-devel # pcre ################################################################################### EOF printf "${RESET} \n" printf "\n${GREEN} >>>>>>>>> 安装常见 lib 开始${RESET} \n" printf "\n${CYAN} >>>> install gcc gcc-c++ kernel-devel libtool${RESET} \n" yum -y install make gcc gcc-c++ kernel-devel libtoolprintf "\n${CYAN} >>>> install openssl openssl-devel${RESET} \n" yum -y install make openssl openssl-develprintf "\n${CYAN} >>>> install zlib zlib-devel${RESET} \n" yum -y install make zlib zlib-develprintf "\n${CYAN} >>>> install pcre${RESET} \n" yum -y install pcreprintf "\n${GREEN} <<<<<<<< 安装常见 lib 结束${RESET} \n"
系统检查脚本 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 #!/usr/bin/env bash C_RESET="$(tput sgr0) " C_BLACK="\033[1;30m" C_RED="\033[1;31m" C_GREEN="\033[1;32m" C_YELLOW="\033[1;33m" C_BLUE="\033[1;34m" C_PURPLE="\033[1;35m" C_CYAN="\033[1;36m" C_WHITE="\033[1;37m" printf "${C_PURPLE} " cat << EOF ################################################################################### # 系统信息检查脚本 ################################################################################### EOF printf "${C_RESET} " [[ $(id -u) -gt 0 ]] && echo "请用root用户执行此脚本!" && exit 1 sysversion=$(rpm -q centos-release | cut -d- -f3) double_line="===============================================================" line="----------------------------------------------" printHeadInfo () { cat << EOF +---------------------------------------------------------------------------------+ | 欢迎使用 【系统信息检查脚本】 | +---------------------------------------------------------------------------------+ EOF }printFootInfo () { cat << EOF +---------------------------------------------------------------------------------+ | 脚本执行结束,感谢使用!| +---------------------------------------------------------------------------------+ EOF } options=( "获取系统信息" "获取服务信息" "获取CPU信息" "获取系统网络信息" "获取系统内存信息" "获取系统磁盘信息" "获取CPU/内存占用TOP10" "获取系统用户信息" "输出所有信息" "退出" )printMenu () { printf "${C_BLUE} " printf "主菜单:\n" for i in "${!options[@]} " ; do index=`expr ${i} + 1` val=`expr ${index} % 2` printf "\t(%02d) %-30s" "${index} " "${options[$i]} " if [[ ${val} -eq 0 ]]; then printf "\n" fi done printf "${C_BLUE} 请输入需要执行的指令:\n" printf "${C_RESET} " }get_systatus_info () { sys_os=$(uname -o) sys_release=$(cat /etc/redhat-release) sys_kernel=$(uname -r) sys_hostname=$(hostname) sys_selinux=$(getenforce) sys_lang=$(echo $LANG ) sys_lastreboot=$(who -b | awk '{print $3,$4}' ) sys_runtime=$(uptime | awk '{print $3,$4}' | cut -d, -f1) sys_time=$(date ) sys_load=$(uptime | cut -d: -f5) cat << EOF 【系统信息】 系统: ${sys_os} 发行版本: ${sys_release} 系统内核: ${sys_kernel} 主机名: ${sys_hostname} selinux状态: ${sys_selinux} 系统语言: ${sys_lang} 系统当前时间: ${sys_time} 系统最后重启时间: ${sys_lastreboot} 系统运行时间: ${sys_runtime} 系统负载: ${sys_load} EOF }get_cpu_info () { Physical_CPUs=$(grep "physical id" /proc/cpuinfo | sort | uniq | wc -l) Virt_CPUs=$(grep "processor" /proc/cpuinfo | wc -l) CPU_Kernels=$(grep "cores" /proc/cpuinfo | uniq | awk -F ': ' '{print $2}' ) CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq ) CPU_Arch=$(uname -m) cat << EOF 【CPU信息】 物理CPU个数:$Physical_CPUs 逻辑CPU个数:$Virt_CPUs 每CPU核心数:$CPU_Kernels CPU型号:$CPU_Type CPU架构:$CPU_Arch EOF }get_service_info () { port_listen=$(netstat -lntup | grep -v "Active Internet" ) kernel_config=$(sysctl -p 2> /dev/null) if [[ ${sysversion} -gt 6 ]]; then service_config=$(systemctl list-unit-files --type =service --state=enabled | grep "enabled" ) run_service=$(systemctl list-units --type =service --state=running | grep ".service" ) else service_config=$(/sbin/chkconfig | grep -E ":on|:启用" | column -t) run_service=$(/sbin/service --status-all | grep -E "running" ) fi cat << EOF 【服务信息】 ${service_config} ${line} 运行的服务: ${run_service} ${line} 监听端口: ${port_listen} ${line} 内核参考配置: ${kernel_config} EOF }get_mem_info () { check_mem=$(free -m) MemTotal=$(grep MemTotal /proc/meminfo | awk '{print $2}' ) MemFree=$(grep MemFree /proc/meminfo | awk '{print $2}' ) let MemUsed=MemTotal-MemFree MemPercent=$(awk "BEGIN {if($MemTotal ==0){printf 100}else{printf \"%.2f\",$MemUsed *100/$MemTotal }}" ) report_MemTotal="$((MemTotal/1024) )" "MB" report_MemFree="$((MemFree/1024) )" "MB" report_MemUsedPercent=$(free | sed -n '2p' | gawk 'x = int(( $3 / $2 ) * 100) {print x}' | sed 's/$/%/' ) cat << EOF 【内存信息】 内存总容量(MB): ${report_MemTotal} 内存剩余量(MB):${report_MemFree} 内存使用率: ${report_MemUsedPercent} EOF }get_net_info () { pri_ipadd=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}' ) pub_ipadd=$(curl ifconfig.me -s) gateway=$(ip route | grep default | awk '{print $3}' ) mac_info=$(ip link | egrep -v "lo" | grep link | awk '{print $2}' ) dns_config=$(egrep -v "^$|^#" /etc/resolv.conf) route_info=$(route -n) cat << EOF 【网络信息】 系统公网地址:${pub_ipadd} 系统私网地址:${pri_ipadd} 网关地址:${gateway} MAC地址:${mac_info} 路由信息: ${route_info} DNS 信息: ${dns_config} EOF }get_disk_info () { disk_info=$(fdisk -l | grep "Disk /dev" | cut -d, -f1) disk_use=$(df -hTP | awk '$2!="tmpfs"{print}' ) disk_percent=$(free | sed -n '2p' | gawk 'x = int(( $3 / $2 ) * 100) {print x}' | sed 's/$/%/' ) disk_inode=$(df -hiP | awk '$1!="tmpfs"{print}' ) cat << EOF 【磁盘信息】 ${disk_info} 磁盘使用: ${disk_use} 磁盘使用百分比: ${disk_percent} inode信息: ${disk_inode} EOF }get_sys_user () { login_user=$(awk -F: '{if ($NF=="/bin/bash") print $0}' /etc/passwd) ssh_config=$(egrep -v "^#|^$" /etc/ssh/sshd_config) sudo_config=$(egrep -v "^#|^$" /etc/sudoers | grep -v "^Defaults" ) host_config=$(egrep -v "^#|^$" /etc/hosts) crond_config=$(for cronuser in /var/spool/cron/*; do ls ${cronuser} 2> /dev/null | cut -d/ -f5; egrep -v "^$|^#" ${cronuser} 2> /dev/null; echo "" ; done ) cat << EOF 【用户信息】 系统登录用户: ${login_user} ${line} ssh 配置信息: ${ssh_config} ${line} sudo 配置用户: ${sudo_config} ${line} 定时任务配置: ${crond_config} ${line} hosts 信息: ${host_config} EOF }get_process_top_info () { top_title=$(top -b n1 | head -7 | tail -1) cpu_top10=$(top -b n1 | head -17 | tail -11) mem_top10=$(top -b n1 | head -17 | tail -10 | sort -k10 -r) cat << EOF 【TOP10】 CPU占用TOP10: ${cpu_top10} 内存占用TOP10: ${top_title} ${mem_top10} EOF }show_dead_process () { printf "僵尸进程:\n" ps -al | gawk '{print $2,$4}' | grep Z }get_all_info () { get_systatus_info echo ${double_line} get_service_info echo ${double_line} get_cpu_info echo ${double_line} get_net_info echo ${double_line} get_mem_info echo ${double_line} get_disk_info echo ${double_line} get_process_top_info echo ${double_line} get_sys_user }main () { while [[ 1 ]] do printMenu read option local index=$[ ${option} - 1 ] case ${options[${index} ]} in "获取系统信息" ) get_systatus_info ;; "获取服务信息" ) get_service_info ;; "获取CPU信息" ) get_cpu_info ;; "获取系统网络信息" ) get_net_info ;; "获取系统内存信息" ) get_mem_info ;; "获取系统磁盘信息" ) get_disk_info ;; "获取CPU/内存占用TOP10" ) get_process_top_info ;; "获取系统用户信息" ) get_sys_user ;; "输出所有信息" ) get_all_info > sys.log printf "${C_GREEN} 信息已经输出到 sys.log 中。${C_RESET} \n\n" ;; "退出" ) exit ;; *) clear echo "抱歉,不支持此选项" ;; esac done } printHeadInfo main printFootInfoprintf "${C_RESET} "
sed进阶 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 30 31 32 33 34 #!/bin/bash sed '{ :start /^\n*$/{$d; N; b start} }' test echo "The cat sleeps in his hat" | sed 's/.at/"&"/g' echo "The System Administrator manual" | sed 's/\(System\) Administrator/\1 user/' echo "1234567" | sed '{:start; s/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/; t start}' sed '=' test | sed 'N; s/\n/ /'