一、编写系统脚本显示当前主机名、CPU、IPv4、内存,磁盘
1 #!/bin/bash
2 #
3 #*********************************
4 #Auther: xi
5 #Date: 2024-12-26
6 #FileName: syainfo.sh
7 #URL: http://www.xxx.com
8 #Description: test
9 #Copyright(C):2024 All right
10 #**********************************
11 echo "Hostname":`hostname`
12 echo "IPv4 address IPv4地址:" `ifconfig ens160 | grep -Eo '([0-9]{1,3}.){3}[0-9]{1,3}'| head -n1`
13 echo "OS version版本:" `cat /etc/redhat-release`
14 echo "KernelVersion内核版本:" `uname -r`
15 echo "CPU型号:" `lscpu | grep "^Model name" | tr -s ""| cut -d: -f2`
16 echo "Memery内存大小:" `cat /proc/meminfo | grep MemTotal`
17 echo "Diskpace 硬盘大小:" `lsblk | grep -E '^sda' | grep -Eo [0-9]+[[:upper:]]`
二、编写脚本实现每日将 /etc/ 目录备份到 /backup/etcYYYY-mm-dd中
脚本实现:需要另外添加crontab -e定时任务脚本
1 #!/bin/bash
2 #
3 #*********************************
4 #Auther: xi
5 #Date: 2024-12-26
6 #FileName: backup.sh
7 #URL: http://www.xxx.com
8 #Description: test
9 #Copyright(C):2024 All right
10 #**********************************
11 SOURCE_DIR="/etc"
12 BACKUP_DIR="/backup/etc$(date+%Y-%m-%d)"
13 mkdir -p "$BACKUP_DIR"
14 cp -av $SOURCE_DIR $BACKUP_DIR
定时任务:
三、编写脚本显示当前硬盘中最大使用
脚本实现:
1 #!/bin/bash
2 #
3 #*********************************
4 #Auther: xi
5 #Date: 2024-12-26
6 #FileName: disk.sh
7 #URL: http://www.xxx.com
8 #Description: test
9 #Copyright(C):2024 All right
10 #**********************************
11 df -h | grep -E '^/dev/(sd.*)' | tr -s ' ' | cut -d' ' -f1,5 | sort -t' ' -k2 -rn
四、编写脚本显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
1 #!/bin/bash
2 #
3 #*********************************
4 #Auther: xi
5 #Date: 2024-12-26
6 #FileName: links.sh
7 #URL: http://www.xxx.com
8 #Description: test
9 #Copyright(C):2024 All right
10 #**********************************
11 netstat -nt | awk '/^tcp\>/ {print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr