常用工具安装:tool-install.sh

#!/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"

# 核心工具
if ! command -v df &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 coreutils (df、du)${RESET}\n"
yum install -y coreutils
else
printf "\n${GREEN}>>>> coreutils (df、du) 已安装${RESET}\n"
fi

if ! command -v chkconfig &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 chkconfig${RESET}\n"
yum install -y chkconfig
else
printf "\n${GREEN}>>>> chkconfig 已安装${RESET}\n"
fi

# 网络工具
if ! command -v ifconfig &> /dev/null || ! command -v netstat &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 net-tools (ifconfig、netstat、route)${RESET}\n"
yum install -y net-tools
else
printf "\n${GREEN}>>>> net-tools (ifconfig、netstat、route) 已安装${RESET}\n"
fi

if ! command -v iptables &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 iptables${RESET}\n"
yum install -y iptables
else
printf "\n${GREEN}>>>> iptables 已安装${RESET}\n"
fi

# IP工具
if ! command -v ping &> /dev/null || ! command -v tracepath &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 iputils (ping、tracepath)${RESET}\n"
yum install -y iputils
else
printf "\n${GREEN}>>>> iputils (ping、tracepath) 已安装${RESET}\n"
fi

if ! command -v traceroute &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 traceroute${RESET}\n"
yum install -y traceroute
else
printf "\n${GREEN}>>>> traceroute 已安装${RESET}\n"
fi

if ! command -v ip &> /dev/null || ! command -v ss &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 iproute (ip、ss)${RESET}\n"
yum install -y iproute
else
printf "\n${GREEN}>>>> iproute (ip、ss) 已安装${RESET}\n"
fi

# 端口工具
if ! command -v lsof &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 lsof${RESET}\n"
yum install -y lsof
else
printf "\n${GREEN}>>>> lsof 已安装${RESET}\n"
fi

if ! command -v nc &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 nc${RESET}\n"
yum install -y nc
else
printf "\n${GREEN}>>>> nc 已安装${RESET}\n"
fi

if ! command -v netstat &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 netstat${RESET}\n"
yum install -y netstat
else
printf "\n${GREEN}>>>> netstat 已安装${RESET}\n"
fi

# DNS工具
if ! command -v dig &> /dev/null || ! command -v host &> /dev/null || ! command -v nslookup &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 bind-utils (dig、host、nslookup)${RESET}\n"
yum install -y bind-utils
else
printf "\n${GREEN}>>>> bind-utils (dig、host、nslookup) 已安装${RESET}\n"
fi

if ! command -v whois &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 whois${RESET}\n"
yum install -y whois
else
printf "\n${GREEN}>>>> whois 已安装${RESET}\n"
fi

# 下载工具
if ! command -v curl &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 curl${RESET}\n"
yum install -y curl
else
printf "\n${GREEN}>>>> curl 已安装${RESET}\n"
fi

if ! command -v wget &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 wget${RESET}\n"
yum install -y wget
else
printf "\n${GREEN}>>>> wget 已安装${RESET}\n"
fi

# 编辑工具
if ! command -v emacs &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 emacs${RESET}\n"
yum install -y emacs
else
printf "\n${GREEN}>>>> emacs 已安装${RESET}\n"
fi

if ! command -v vim &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 vim${RESET}\n"
yum install -y vim
else
printf "\n${GREEN}>>>> vim 已安装${RESET}\n"
fi

# 流量工具
if ! command -v iftop &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 iftop${RESET}\n"
yum install -y iftop
else
printf "\n${GREEN}>>>> iftop 已安装${RESET}\n"
fi

if ! command -v nethogs &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 nethogs${RESET}\n"
yum install -y nethogs
else
printf "\n${GREEN}>>>> nethogs 已安装${RESET}\n"
fi

# 抓包工具
if ! command -v tcpdump &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 tcpdump${RESET}\n"
yum install -y tcpdump
else
printf "\n${GREEN}>>>> tcpdump 已安装${RESET}\n"
fi

# 压缩工具
if ! command -v unzip &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 unzip${RESET}\n"
yum install -y unzip
else
printf "\n${GREEN}>>>> unzip 已安装${RESET}\n"
fi

# 版本控制工具
if ! command -v git &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 git${RESET}\n"
yum install -y git
else
printf "\n${GREEN}>>>> git 已安装${RESET}\n"
fi

if ! command -v svn &> /dev/null; then
printf "\n${CYAN}>>>> 正在安装 subversion${RESET}\n"
yum install -y subversion
else
printf "\n${GREEN}>>>> subversion 已安装${RESET}\n"
fi

printf "\n${GREEN}<<<<<<<< 安装常用命令工具结束${RESET}\n"

常用lib库安装:lib-install.sh

#!/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 libtool

printf "\n${CYAN}>>>> install openssl openssl-devel${RESET}\n"
yum -y install make openssl openssl-devel

printf "\n${CYAN}>>>> install zlib zlib-devel${RESET}\n"
yum -y install make zlib zlib-devel

printf "\n${CYAN}>>>> install pcre${RESET}\n"
yum -y install pcre

printf "\n${GREEN}<<<<<<<< 安装常见 lib 结束${RESET}\n"

系统信息查询:systeminfo.sh

#!/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"
##############################################################################

# 检查系统版本
check_version() {
if [ -f /etc/os-release ]; then
source /etc/os-release
if [[ "$NAME" == "CentOS Stream" ]]; then
if [ "$VERSION_ID" -lt 7 ]; then
echo -e "${C_RED}此脚本仅支持 CentOS 7 及以上版本。请升级系统或使用合适的脚本版本。${C_RESET}"
exit 1
fi
else
echo -e "${C_RED}未能识别系统版本,请确保使用 CentOS 系统。${C_RESET}"
exit 1
fi
else
echo -e "${C_RED}未能找到操作系统信息文件,请确保使用 CentOS 系统。${C_RESET}"
exit 1
fi
}

printf "${C_PURPLE}"
cat << EOF
###################################################################################
# 系统信息检查脚本 #
###################################################################################
EOF
printf "${C_RESET}"

check_version

[[ $(id -u) -ne 0 ]] && echo -e "${C_RED}请用root用户执行此脚本!${C_RESET}" && exit 1

double_line="==============================================================="
line="----------------------------------------------"

# 打印头部信息
print_head_info() {
cat << EOF
+---------------------------------------------------------------------------------+
| 欢迎使用系统信息检查脚本 |
+---------------------------------------------------------------------------------+
EOF
}

# 打印尾部信息
print_foot_info() {
cat << EOF
+---------------------------------------------------------------------------------+
| 脚本执行结束,感谢使用! |
+---------------------------------------------------------------------------------+
EOF
}

# 菜单选项
options=( "获取系统信息" "获取服务信息" "获取CPU信息" "获取系统网络信息" "获取系统内存信息" "获取系统磁盘信息" "获取CPU/内存占用TOP10" "获取系统用户信息" "输出所有信息" "退出" )

print_menu() {
printf "${C_BLUE}主菜单:\n"
for i in "${!options[@]}"; do
index=$((i + 1))
val=$((index % 2))
printf "\t(%02d) %-30s" "${index}" "${options[$i]}"
if [ $val -eq 0 ]; then
printf "\n"
fi
done
printf "${C_BLUE}请输入需要执行的指令 (1-10):"
printf "${C_RESET}"
}

# 获取系统信息
get_system_info() {
sys_os=$(uname -o)
sys_release=$(cat /etc/os-release | grep PRETTY_NAME | cut -d= -f2 | tr -d '"')
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_service_info() {
if command -v systemctl &> /dev/null; 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
echo -e "${C_RED}未检测到 systemctl 命令,请确保您使用的是 CentOS 7 或更高版本。${C_RESET}"
exit 1
fi
port_listen=$(netstat -lntup | grep -v "Active Internet")
kernel_config=$(sysctl -p 2> /dev/null)

cat << EOF
【服务信息】
${service_config}
${line}
运行的服务:
${run_service}
${line}
监听端口:
${port_listen}
${line}
内核参考配置:
${kernel_config}
EOF
}

# 获取CPU信息
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_mem_info() {
check_mem=$(free -m)
MemTotal=$(grep MemTotal /proc/meminfo | awk '{print $2}') #KB
MemFree=$(grep MemFree /proc/meminfo | awk '{print $2}') #KB
let MemUsed=MemTotal-MemFree
MemPercent=$(awk "BEGIN {if($MemTotal==0){printf 100}else{printf \"%.2f\",$MemUsed*100/$MemTotal}}")
report_MemTotal="$((MemTotal/1024)) MB" #内存总容量(MB)
report_MemFree="$((MemFree/1024)) MB" #内存剩余(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=$(who | awk '{print $1}' | sort | uniq)
ssh_config=$(grep -v '^#' /etc/ssh/sshd_config | grep -v '^$')
sudo_config=$(grep -v '^#' /etc/sudoers | grep -v '^$')
host_config=$(grep -v '^#' /etc/hosts | grep -v '^$')
crond_config=$(grep -v '^#' /etc/crontab | grep -v '^$')

cat << EOF
【系统用户信息】
登录用户:
${login_user}
${line}
SSH 配置:
${ssh_config}
${line}
sudo 配置:
${sudo_config}
${line}
hosts 配置:
${host_config}
${line}
定时任务配置:
${crond_config}
EOF
}

# 获取CPU/内存占用TOP10
get_cpu_mem_top() {
top_cpu=$(ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%cpu | head -n 11)
top_mem=$(ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head -n 11)
cat << EOF
【CPU/内存 TOP10】
CPU占用TOP10:
${top_cpu}
${line}
内存占用TOP10:
${top_mem}
EOF
}

# 输出所有信息
output_all_info() {
get_system_info
get_service_info
get_cpu_info
get_net_info
get_mem_info
get_disk_info
get_sys_user
get_cpu_mem_top
}

# 主菜单逻辑
while true; do
print_menu
read -r choice
case "$choice" in
1) get_system_info ;;
2) get_service_info ;;
3) get_cpu_info ;;
4) get_net_info ;;
5) get_mem_info ;;
6) get_disk_info ;;
7) get_cpu_mem_top ;;
8) get_sys_user ;;
9) output_all_info ;;
10) print_foot_info; exit 0 ;;
*) echo -e "${C_RED}无效的选项,请重新选择。${C_RESET}" ;;
esac
done

云野 » 运维实用脚本

发表回复