1.编写脚本/root/bin/systeminfo.sh,显示当前主机系统信息,包括主机名,IPv4地址,操作系统版本,内核版本,cpu型号,内存大小,硬盘大小。
#!/bin/bash
# 显示电脑硬件信息
echo "主机名: $(hostname)"
echo "IPv4地址: $(ifconfig | head -n2 |tail -n1 |tr -s " " ":"| cut -d":" -f3)"
echo "操作系统版本:$(cat /etc/redhat-release)"
echo "内核版本: $(uname -r)"
echo "CPU型号: $(cat /proc/cpuinfo|grep "model name"|uniq -c|cut -d":" -f2)"
echo "内存大小: $(free -m | head -n2 |tail -n1|tr -s " " ":"|cut -d":" -f2)"
echo "硬盘大小: $(fdisk -l | grep "GB"|cut -d":" -f2|cut -d"," -f1)"
2.编写脚本/root/bin/backup.sh,可实现每日将/etc/目录备份到/root/etcYYYY-mm-dd中
#!/bin/bash
cp -a /etc/ /root/etc"$(date +%F)"
3.编写脚本/root/bin/disk.sh,显示当前硬盘分区中空间利用率最大的值
#!/bin/bash
max=$(df |tr -s " " ":"|cut -d ":" -f5|egrep -o "[[:digit:]]*"|sort -nr|head -n1)
echo "当前硬盘分区中空间利用率最大的值为: $max"
unset max
4.编写脚本/root/bin/links.sh,显示正连接本主机的每个远程主机的IPv4地址和连接数,并按连接数从大到小排序
#!/bin/bash
links=$(netstat -tan| grep "^[t]\|[u]"|tr -s " " ":"|cut -d":" -f6|uniq -c|sort -nr|egrep "\<(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"|tail -n1)
echo "连接数和IP地址分别为:$links"
unset links
exit 0
5.编写脚本/root/bin/sumid.sh,计算etc/passwd文件中的第十个用户个第二十个用户的ID之和
#!/bin/bash
usera=$(cat -n /etc/passwd | head |tail -n1|cut -d":" -f3)
echo "id为10的uid:$usera"
userb=$(cat -n /etc/passwd | head -n20|tail -n1|cut -d":" -f3)
echo "id为20的uid:$userb"
# userall=$usera+$userb
let usera=$usera+$userb
echo "id和为: $usera"
unset usera userb
exit 0
6、编写脚本/root/bin/nologin.sh和login.sh,实现禁止和充许普通用户登录系统
login
#!/bin/bash
[ -f "/etc/nologin" ] && rm -rf /etc/nologin && echo "User Can Login" ||echo "User Already Login"
nologin
#!/bin/bash
[ -f "/etc/nologin" ] && echo "Other User Cannot Login System" ||{ touch /etc/nologin; echo "Other User Cannot Login System"; }