实验要求:
实验内容
设计与实现一个系统配置的 Shell 脚本。功能模块如下:
1 配置主机名、输出当前主机名、根据用户输入设置主机名
2 配置网络模块、备份当前的网络配置、自动配置网络为静态 IP 配置;
3 配置防火墙 l输出当前防火墙状态
根据用户选择配置防火墙:0为关闭防火墙;1 为开启防火墙;
4 本地光盘 yum 源 l备份当前所有的 yum 源配置;自动配置光盘为 yum 源;
5 重启模块 l所有配置完成后,自动重启系统使所有配置生效;
实验要求
1.配置备份存储在/root/backup-20190707目录下;
2.每个模块分别通过一个函数实现;
3.详细给出函数和重要代码的说明;
4.给出测试过程及结果说明;
注意,此次涉及网卡为 eth0,请根据自身情况修改
Controller.sh如下
sh Hostname.sh
sh Network.sh
sh Firewalld.sh
sh Yum.sh
sh Reboot.sh
Hostname.sh如下
echo "@@@@@@@@@@@@@@@ This Is Hostname Set @@@@@@@@@@@@@@@"
echo $HOSTNAME
read -p "Please set hostname:" hostname
hostnamectl set-hostname $hostname
bash
Network.sh如下
echo "################ This Is Network Backup ####################"
#echo "Please Input Your Interface name:"
#read -p "Please Input Your Interface name:" itn
grep "BOOTPROTO" /etc/sysconfig/network-scripts/ifcfg-eth0 ##此处eth0为我的网卡名,请根据自身实际情况修改
state=`grep "BOOTPROTO" /etc/sysconfig/network-scripts/ifcfg-eth0`
echo "This is your Interface state:$state"
echo "如果你的网卡为stastatic模式,请手动设置,切勿使用脚本"
echo "Please Input Your IP/GATEWAY/NETMASK/DNS"
read -p "Please Input Your IP:" iP
echo "IPADDR=$iP" >> /etc/sysconfig/network-scripts/ifcfg-eth0
read -p "Please Input Your GATEWAY:" gT
echo "GATEWAY=$gT" >> /etc/sysconfig/network-scripts/ifcfg-eth0
read -p "Please Input Your NETMASK:" nT
echo "NETMASK=$nT" >> /etc/sysconfig/network-scripts/ifcfg-eth0
read -p "Please Input Your DNS:" dN
echo "DNS1=$dN" >> /etc/sysconfig/network-scripts/ifcfg-eth0
echo "Restarting Networking......"
systemctl restart network
Firewalld.sh如下
echo "############ This Is Firewalld Set ###################"
systemctl status firewalld
echo "************this is firewalld state set(only input 1 char,like 1 or 0)"
echo "0:Stop the firewalld"
echo "1:Start the firewalld"
read -n 2 -p "Please choose your Firewalld state:" state
echo -e "\n"
if [ $state -eq 0 ];then
systemctl stop firewalld
systemctl status firewalld
elif [ $state -eq 1 ];then
systemctl start firewalld
systemctl status firewalld
echo -e "\n"
else
echo "Error!"
fi
Yum.sh如下
echo "^^^^^^^^^^^^^^^^this is yum backup test^^^^^^^^^^^^^^"
mkdir -p /root/backup-20190707/yums/
cp /etc/yum.repos.d/* /root/backup-20190707/yums/
rm -rf //etc/yum.repos.d/*
echo "[centos]" > /etc/yum.repos.d/local.repo
echo "name=centos" >> /etc/yum.repos.d/local.repo
echo "baseurl=file:///opt/centos" >> /etc/yum.repos.d/local.repo
echo "gpgcheck=0" >> /etc/yum.repos.d/local.repo
echo "enabled=1" >> /etc/yum.repos.d/local.repo
mount /dev/cdrom /mnt/
mkdir -p /opt/centos
cp /mnt/* /opt/centos
yum list
Reboot.sh如下
echo "%%%%%%%%%%%%%% This is Reboot Program %%%%%%%%%%%%%"
echo "Please input your choose:"
echo "1:Reboot your System , and make your change Take Effect"
echo "2:Give Up Reboot"
read -n 2 -p "Please input your choose:" rb
if [ $rb -eq 1 ];then
reboot
elif [ $rb -eq 2 ];then
echo "Give up reboot,Think........"
else
echo "Error Input Number"
fi