Linux 开机脚本

点击查看教程

Debian 开机脚本

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
# Linux通用 开机显示‮脚本
sudo touch /etc/profile.d/linux-check.sh && sudo chmod +x /etc/profile.d/linux-check.sh && sudo ln -sf /etc/profile.d/linux-check.sh /usr/local/bin/m && sudo cat > /etc/profile.d/linux-check.sh <<'EOF'
#!/bin/bash
#
#**************************************************
#FileName: linux-check.sh
## -------------------粗体-----------------------
## \E[0m 重置颜色
## \E[1;31m 红色
## \E[1;32m 绿色
## \E[1;33m 黄色
## \E[1;34m 蓝色
## \E[1;35m 紫色
## \E[1;36m 青色
## \E[1;37m 白色
#**************************************************
echo -e "\e[1;32m-----------系统信息----------\e[0m"
echo -e "\e[1;34m主机名称 : \e[1;31m`hostname`\e[0m"

echo -e "\e[1;34m系统版本 : \e[1;31m`cat /etc/os-release | grep -w "PRETTY_NAME" | cut -d= -f2 | tr -d '"' | sed 's/\s*(\([^)]*\))//g'| sed 's/Stream //g'| sed 's/Linux //g'`\e[0m"

echo -e "\e[1;34m内核版本 : \e[1;31m`uname -r`\e[0m"

echo -e "\e[1;34m编码格式 : \e[1;31m${LANG}\e[0m"

echo -e "\e[1;32m-----------CPU 信息----------\e[0m"
echo -e "\e[1;34mCPU 架构 : \e[1;31m`uname -m`\e[0m"

echo -e "\e[1;34mCPU 型号 :\e[1;31m`cat /proc/cpuinfo | grep "model name" | head -1 | awk -F: '{print $2}'`\e[0m"

echo -e "\e[1;34mCPU 核心 : \e[1;31m`cat /proc/cpuinfo | grep processor | wc -l | awk '{print $1" 核"}'`\e[0m"

echo -e "\e[1;34mCPU 负载 :\e[1;31m $(awk '{printf "%.2f", $3}' /proc/loadavg)\e[0m"

echo -e "\e[1;34mCPU 占用 : \e[1;31m`top -bn1 | grep "Cpu(s)" | awk '{print 100 - $8}' | cut -c 1-2 | awk '{printf("%.2f%%\n", $1/100*100)}'`\e[0m"

echo -e "\e[1;32m-----------网络信息----------\e[0m"
echo -e "\e[1;34mIPV4内网 : \e[1;31m`hostname -I | awk '{print $1}'`\e[0m"

#echo -e "\e[1;34mIPV4公网 : \e[1;31m$(curl -4 -s ifconfig.co)\e[0m"
echo -e "\e[1;34mIPV4公网 : \e[1;31m$(curl -4 -sL http://ipinfo.io/ip 2>/dev/null)\e[0m"

echo -e "\e[1;34m默认网关 : \e[1;31m$(ip route show default | awk '/default/ {print $3}')\e[0m"

if (ping -c2 -w2 www.baidu.com &>/dev/null);then
echo -e "\e[1;34m网络连通 : \e[1;31m是\e[0m"
else
echo -e "\e[1;34m网络连通 : \e[1;31m否\e[0m"
fi

echo -e "\e[1;32m-----------磁盘信息----------\e[0m"
echo -e "\e[1;34m磁盘占用 : \e[1;31m`df -h | awk '$NF=="/"{printf "%s/%s (%s)", $3, $2, $5}'`\e[0m"
echo -e "\033[1;34mNFS 挂载 : \033[1;31m$(df -h | grep nfs)\033[0m"

# echo -e "\E[1;34mNFS 挂载 : \E[1;31m`echo "" && df -hT /mnt/* | grep "10.10.10.*:/*"`\E[0m"

# 获取当前时间
start_time=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "\e[1;34m当前时间 :\e[1;31m$start_time\e[0m"

echo -e "\e[1;34m运行时间 : \e[1;31m$(cat /proc/uptime | awk -F. '{run_days=int($1 / 86400);run_hours=int(($1 % 86400) / 3600);run_minutes=int(($1 % 3600) / 60); if (run_days > 0) printf("%d天 ", run_days); if (run_hours > 0) printf("%d时 ", run_hours); printf("%d分\n", run_minutes)}')\e[0m"

echo -e "\e[1;32m-----------------------------\e[0m"

EOF
1
sudo /etc/profile.d/linux-check.sh

FnOS 开机脚本

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
sudo touch /etc/profile.d/linux-check.sh && sudo chmod +x /etc/profile.d/linux-check.sh && sudo ln -sf /etc/profile.d/linux-check.sh /usr/local/bin/m  && sudo cat > /etc/profile.d/linux-check.sh <<'EOF'
#!/bin/bash

# 防止重复执行(仅在交互式登录Shell显示一次)
if [ -n "$LINUX_CHECK_SHOWN" ] || [ -z "$PS1" ]; then
return 0
fi
export LINUX_CHECK_SHOWN=1

# 获取IP更稳健的方式(兼容多网卡)
get_local_ip() {
local ip
ip=$(ip -o -4 addr show scope global | awk '{print $4}' | cut -d'/' -f1 | head -n1)
[ -z "$ip" ] && ip="N/A"
echo "$ip"
}

# 获取CPU占用(兼容不同top版本)
get_cpu_usage() {
local cpu_usage
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *$$[0-9.]*$$%* id.*/\1/" | awk '{printf "%.2f%%", 100 - $1}')
[ -z "$cpu_usage" ] && cpu_usage="N/A"
echo "$cpu_usage"
}

# 信息输出
echo -e "\e[1;32m-----------系统信息----------\e[0m"
echo -e "\e[1;34m主机名称 : \e[1;31m$(hostname)\e[0m"
echo -e "\e[1;34m内核版本 : \e[1;31m$(uname -r)\e[0m"

echo -e "\e[1;32m-----------CPU 信息----------\e[0m"
echo -e "\e[1;34mCPU 架构 : \e[1;31m$(uname -m)\e[0m"
echo -e "\e[1;34mCPU 占用 : \e[1;31m`top -bn1 | grep "Cpu(s)" | awk '{print 100 - $8}' | cut -c 1-2 | awk '{printf("%.2f%%\n", $1/100*100)}'`\e[0m"

echo -e "\e[1;32m-----------网络信息----------\e[0m"
echo -e "\e[1;34mIPV4内网 : \e[1;31m$(get_local_ip)\e[0m"
echo -e "\e[1;34m默认网关 : \e[1;31m$(ip route show default 2>/dev/null | awk '/default/ {print $3}' || echo "N/A")\e[0m"

echo -e "\e[1;32m-----------磁盘信息----------\e[0m"
echo -e "\e[1;34m磁盘占用 : \e[1;31m$(df -h / | awk 'NR==2 {print $3"/"$2 " ("$5")"}')\e[0m"

echo -e "\e[1;34m运行时间 : \e[1;31m$(cat /proc/uptime | awk -F. '{run_days=int($1 / 86400);run_hours=int(($1 % 86400) / 3600);run_minutes=int(($1 % 3600) / 60); if (run_days > 0) printf("%d天 ", run_days); if (run_hours > 0) printf("%d时 ", run_hours); printf("%d分\n", run_minutes)}')\e[0m"

echo -e "\e[1;32m-----------------------------\e[0m"
EOF
1
sudo /etc/profile.d/linux-check.sh

iStoreOS 开机脚本

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
touch /etc/profile.d/linux-check.sh && cat > /etc/profile.d/linux-check.sh <<'EOF'
#!/bin/bash

# 防止重复执行(仅在交互式登录Shell显示一次)
if [ -n "$LINUX_CHECK_SHOWN" ] || [ -z "$PS1" ]; then
return 0
fi
export LINUX_CHECK_SHOWN=1

# 获取IP更稳健的方式(兼容多网卡)
get_local_ip() {
local ip
ip=$(ip -o -4 addr show scope global | awk '{print $4}' | cut -d'/' -f1 | head -n1)
[ -z "$ip" ] && ip="N/A"
echo "$ip"
}

# 获取CPU占用(兼容不同top版本)
get_cpu_usage() {
local cpu_usage
cpu_usage=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *$$[0-9.]*$$%* id.*/\1/" | awk '{printf "%.2f%%", 100 - $1}')
[ -z "$cpu_usage" ] && cpu_usage="N/A"
echo "$cpu_usage"
}

# 信息输出
echo -e "\e[1;32m-----------系统信息----------\e[0m"
echo -e "\e[1;34m主机名称 : \e[1;31m$(cat /proc/sys/kernel/hostname)\e[0m"
echo -e "\e[1;34m内核版本 : \e[1;31m$(uname -r)\e[0m"

echo -e "\e[1;32m-----------CPU 信息----------\e[0m"
echo -e "\e[1;34mCPU 架构 : \e[1;31m$(uname -m)\e[0m"
echo -e "\e[1;34mCPU 占用 : \e[1;31m`top -bn1 | grep "Cpu(s)" | awk '{print 100 - $8}' | cut -c 1-2 | awk '{printf("%.2f%%\n", $1/100*100)}'`\e[0m"

echo -e "\e[1;32m-----------网络信息----------\e[0m"
echo -e "\e[1;34mIPV4内网 : \e[1;31m$(get_local_ip)\e[0m"
echo -e "\e[1;34m默认网关 : \e[1;31m$(ip route show default 2>/dev/null | awk '/default/ {print $3}' || echo "N/A")\e[0m"

echo -e "\e[1;32m-----------磁盘信息----------\e[0m"
echo -e "\e[1;34m磁盘占用 : \e[1;31m$(df -h / | awk 'NR==2 {print $3"/"$2 " ("$5")"}')\e[0m"

echo -e "\e[1;34m运行时间 : \e[1;31m$(cat /proc/uptime | awk -F. '{run_days=int($1 / 86400);run_hours=int(($1 % 86400) / 3600);run_minutes=int(($1 % 3600) / 60); if (run_days > 0) printf("%d天 ", run_days); if (run_hours > 0) printf("%d时 ", run_hours); printf("%d分\n", run_minutes)}')\e[0m"

echo -e "\e[1;32m-----------------------------\e[0m"
EOF

重新连接终端看效果

Linux 常用脚本

点击查看教程

install_common_packages.sh

  • Linux系统安装常用软件
1
bash <(curl -sL script.meimolihan.eu.org/sh/linux/install_common_packages.sh)
1
bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/linux/install_common_packages.sh)

ping_based_host_finder.sh

  • 测试 10.10.10.0/24 整个网段中哪些主机处于开机状态,哪些主机处于关机状态
1
bash <(curl -sL script.meimolihan.eu.org/sh/linux/ping_based_host_finder.sh)
1
bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/linux/ping_based_host_finder.sh)

time_based_greeting.sh

  • 根据系统时间显示问候语
1
bash <(curl -sL script.meimolihan.eu.org/sh/linux/time_based_greeting.sh)
1
bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/linux/time_based_greeting.sh)

file_stats.sh

  • 统计当前目录文件个数(普通文件和隐藏文件)
  • 统计当前目录文件大小
1
bash <(curl -sL script.meimolihan.eu.org/sh/linux/file_stats.sh)
1
bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/linux/file_stats.sh)

certbot-mobufan.eu.org.sh

  • SSL证书自动续期
1
bash <(curl -sL script.meimolihan.eu.org/sh/linux/certbot-mobufan.eu.org.sh)
1
bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/certbot-mobufan.eu.org.sh)

Linux 压缩脚本

点击查看教程

压缩脚本

压缩为 .zip 文件

遍历当前目录下的子目录压缩为.zip文件

1
bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/zip/backup-zip.sh)

压缩为 .gz 文件

遍历当前目录下的子目录压缩为 .tar.gz 文件

1
bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/zip/backup-gz.sh)

解压脚本

解压 .zip 文件

遍历当前目录下的 *.zip 文件,解压到当前目录下

1
bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/zip/unzip.sh)

解压 .tar.gz 文件

遍历当前目录下的 *.tar.gz,*.img.gz 文件,解压到当前目录下

1
bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/zip/untar.sh)

备份脚本

organize_zips.sh

/vol2/1000/backup/创建日期文件夹,将 /vol1/1000/home/ 下的子目录,压缩为:目录名_yyyymmdd.zip

1
bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/zip/organize_zips.sh)

daily_backup.sh

/vol1/1000/home 目录压缩为 compose_yyyymmdd.tar.gz 保存到 /vol2/1000/backup 目录下

1
bash <(curl -sL gitee.com/meimolihan/script/raw/master/sh/zip/daily_backup.sh)

本地脚本使用方法

1
2
3
4
5
6
cd /mnt/test && \
wget -c https://gitee.com/meimolihan/script/raw/master/sh/zip/backup-zip.sh && \
chmod +x backup-zip.sh && \
sudo apt install dos2unix && \
dos2unix backup-zip.sh && \
bash backup-zip.sh