马哥博客作业第四周

1.编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,CPU型号,内存大小,硬盘大小

[root@centos8 bin]# cat systeminfo.sh

#!/bin/bash
hostname=`hostname`
ipv4=`ifconfig | sed -rn '2s/[^0-9]+([0-9.]+).*/\1/p'`
os=`cat /etc/redhat-release`
kernel_version=`cat /proc/version | cut -d' ' -f3`
cpu=`lscpu |grep 'Model name' || lscpu | grep 'cpu model'| tr -s ' '`
ram=`cat /proc/meminfo | sed -n '/MemTotal/p' | tr -s ' '|cut -d: -f2-3`
harddisk=`df -h / |tr -s ' '|cut -d ' ' -f2 | tail -n+2`
echo hostanme: $hostname
echo server_ip:$ipv4
echo os: $os
echo kernel_version: $kernel_version
echo cpu: $cpu
echo ram:$ram
echo hardisk: $harddisk

[root@centos8 bin]# sh systeminfo.sh
hostanme: centos8.1.magedu.org
server_ip:10.0.0.100
os: CentOS Linux release 8.1.1911 (Core)
kernel_version: 4.18.0-147.el8.x86_64
cpu: Model name: Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
ram: 1849564 kB
hardisk: 100G
[root@centos8 bin]#

 

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

#!/bin/bash
backup="/root/etc`date +%F`"
cp -aR /etc/&backup && echo "/etc/ backup to $backup done"

 

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

#!/bin/bash
top usage=`df -h | tr -s ' '|cut -d ' ' -f5-6 | sort -nr | head -1`
echo "top usage $top usage"

 

4.编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序

#!/bin/bash
connection=`netstat -tan | grep ESTAB* |tr -s ' ' ':'| cut -d: -f6 |uniq -c | sort -nr`
echo "$connection"

5.使用sed命令在test.txt文件每行后增加一空行


sed -n 's/$/& /p' test.txt

6.使用sed命令打印/etc/passwd的奇数行


sed -n '1~2p' /etc/passwd | wc -l

posted @ 2020-05-06 01:00  大卫不是很能吃  阅读(88)  评论(0编辑  收藏  举报