马哥博客作业第四周

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

CPU型号:
[17:46:41 root@localhost ~]#cat /proc/cpuinfo | grep 'model name' | tr -s ' ' | sed -rn 's/.*: (.*)/1/p'
Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz

显示主机名:
[17:46:52 root@localhost ~]#hostname
localhost.localdomain
IPv4地址:
[18:03:40 root@localhost ~]#ifconfig | sed -n '2s/^.*inet / /p'|sed -n 's/netmask.*/ /p'
192.168.100.201

[18:05:47 root@localhost ~]#ifconfig | sed -n '2s/^.*inet / / ;2s/ netmask.*/ /p'
192.168.100.201

[18:06:10 root@localhost ~]#ifconfig | sed -rn '2s/[^0-9]+([0-9.]+).*/1/p'
192.168.100.201

[18:08:53 root@localhost ~]#ifconfig | sed -rn '2s/(.*inet )([0-9].*)(netmask.*)/2/p'
192.168.100.201

内核版本:
[18:13:35 root@localhost ~]#cat /proc/version | cut -d' ' -f3
4.18.0-147.el8.x86_64

[18:37:44 root@localhost motd.d]#cat /proc/version | sed -rn 's/(^Linux version )(.*)((m.*)/2/p'
4.18.0-147.el8.x86_64

[18:42:14 root@localhost motd.d]#cat /proc/version | sed -rn 's/(.*version )(.*)(.m.*)/2/p'
4.18.0-147.el8.x86_64

操作系统版本:
[18:55:13 root@localhost motd.d]#lsb_release -a | sed -n '/Des.*/p'
Description: CentOS Linux release 8.1.1911 (Core)

[18:54:43 root@localhost motd.d]#lsb_release -a | grep Des
Description: CentOS Linux release 8.1.1911 (Core)

CPU型号:
[18:57:47 root@localhost motd.d]#lscpu | grep 型号名称 | tr -s ' '
型号名称: Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz

root@cisco-virtual-machine:~# lscpu | grep 'Model name' | tr -s ' '
Model name: Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz

lscpu |grep 'Model name' || lscpu | grep '型号名称'| tr -s ' '


内存大小:
[18:58:02 root@localhost motd.d]# cat /proc/meminfo | sed -n '/MemTotal/p'
MemTotal: 1849500 kB

硬盘大小:
root@cisco-virtual-machine:~# df -h /
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 6.3G 13G 34% /

[19:19:08 root@localhost motd.d]#df -h /
文件系统 容量 已用 可用 已用% 挂载点
/dev/nvme0n1p2 17G 4.4G 12G 27% /

脚本:
#!/bin/bash
hostname=`hostname`
ipv4=`ifconfig | sed -rn '2s/[^0-9]+([0-9.]+).*/1/p'`
cpu=`lscpu |grep 'Model name' || lscpu | grep '型号名称'| tr -s ' '`
mem=`cat /proc/meminfo | sed -n '/MemTotal/p' | tr -s ' '|cut -d: -f2-3`
disk=`df -h / |tr -s ' '|cut -d ' ' -f2 | tail -n+2`
os=`lsb_release -a | sed -n '/Des.*/p'`
core_version=`cat /proc/version | cut -d' ' -f3`
echo the hostanme is: $hostname
echo the server_ip is:$ipv4
echo the os is: $os
echo the core_version is:$core_version
echo the cpuinfo is: $cpu
echo the memery is:$mem
echo the hardisk size is: $disk

执行结果:
[21:00:51 root@localhost etc]#. systeminfo.sh
the hostanme is: localhost.localdomain
the server_ip is:192.168.100.201
the os is: Description: CentOS Linux release 8.1.1911 (Core)
the core_version is:4.18.0-147.el8.x86_64
the cpuinfo is: 型号名称: Intel(R) Core(TM) i5-4590 CPU @ 3.30GHz
the memery is: 1849500 kB
the hardisk size is: 17G

2.编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
mkdir -p /root/etc`date +%F`
cp -a /etc/ /root/etc2020-05-05/

配置脚本:
#!/bin/bash
backup_dir="/root/etc`date +%F`"
cp -aR /etc/ $backup_dir && echo "/etc/ is backup to $backup_dir successfull

执行结果:
[21:27:02 root@localhost etc2020-05-05]#bash test.sh
/etc/ is backup to /root/etc2020-05-05 successfull

 


3.编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
[20:02:23 root@localhost etc]#df -h | tr -s ' '|cut -d ' ' -f5-6 | sort -nr | head -1
27% /

配置脚本:
#!/bin/bash
disk_usg_max=`df -h | tr -s ' '|cut -d ' ' -f5-6 | sort -nr | head -1`
echo "disk is max used $disk_usg_max"

执行结果:
[21:32:35 root@localhost etc2020-05-05]#bash test1.sh
disk is max used 27% /

 


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

root@cisco-virtual-machine:~# netstat -tan | grep ESTAB* |tr -s ' ' ':'| cut -d: -f6 |uniq -c | sort -nr
1 192.168.100.100

配置脚本:
#!/bin/bash
disk_usg_max=`df -h | tr -s ' '|cut -d ' ' -f5-6 | sort -nr | head -1`
echo "disk is max used $disk_usg_max"

执行结果:
[21:37:04 root@localhost etc2020-05-05]#bash test1.sh
connect_ip is 3 192.168.100.100


5.使用sed命令在test.txt文件每行后增加一空行
sed -n 's/$/& /p' test.txt

6.使用sed命令打印/etc/passwd的奇数行
sed -n '1~2p' /etc/passwd | wc -l

原文地址:https://www.cnblogs.com/HRAS/p/12831141.html