实验要求:
实验内容
设计与实现一个系统配置的 Shell 脚本。功能模块如下:
1 配置主机名、输出当前主机名、根据用户输入设置主机名
2 配置网络模块、备份当前的网络配置、自动配置网络为静态 IP 配置;
3 配置防火墙 l输出当前防火墙状态
根据用户选择配置防火墙:0为关闭防火墙;1 为开启防火墙;
4 本地光盘 yum 源 l备份当前所有的 yum 源配置;自动配置光盘为 yum 源;
5 重启模块 l所有配置完成后,自动重启系统使所有配置生效;
实验要求
1.配置备份存储在/root/backup-0707目录下;
2.每个模块分别通过一个函数实现;
3.详细给出函数和重要代码的说明;
4.给出测试过程及结果说明;
注意,此次涉及网卡为 eth0,请根据自身情况修改
Controller.sh如下
sh Hostname.shsh Network.shsh Firewalld.shsh Yum.shsh Reboot.sh
Hostname.sh如下
echo "@@@@@@@@@@@@@@@ This Is Hostname Set @@@@@@@@@@@@@@@"echo $HOSTNAMEread -p "Please set hostname:" hostnamehostnamectl set-hostname $hostnamebash
Network.sh如下
echo "################ This Is Network Backup ####################"#echo "Please Input Your Interface name:"#read -p "Please Input Your Interface name:" itngrep "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:" iPecho "IPADDR=$iP" >> /etc/sysconfig/network-scripts/ifcfg-eth0read -p "Please Input Your GATEWAY:" gTecho "GATEWAY=$gT" >> /etc/sysconfig/network-scripts/ifcfg-eth0read -p "Please Input Your NETMASK:" nTecho "NETMASK=$nT" >> /etc/sysconfig/network-scripts/ifcfg-eth0read -p "Please Input Your DNS:" dNecho "DNS1=$dN" >> /etc/sysconfig/network-scripts/ifcfg-eth0echo "Restarting Networking......"systemctl restart network
Firewalld.sh如下
echo "############ This Is Firewalld Set ###################"systemctl status firewalldecho "************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:" stateecho -e "\n"if [ $state -eq 0 ];thensystemctl stop firewalldsystemctl status firewalldelif [ $state -eq 1 ];thensystemctl start firewalldsystemctl status firewalldecho -e "\n"elseecho "Error!"fi
Yum.sh如下
echo "^^^^^^^^^^^^^^^^this is yum backup test^^^^^^^^^^^^^^"mkdir -p /root/backup-0707/yums/cp /etc/yum.repos.d/* /root/backup-0707/yums/rm -rf //etc/yum.repos.d/*echo "[centos]" > /etc/yum.repos.d/local.repoecho "name=centos" >> /etc/yum.repos.d/local.repoecho "baseurl=file:///opt/centos" >> /etc/yum.repos.d/local.repoecho "gpgcheck=0" >> /etc/yum.repos.d/local.repoecho "enabled=1" >> /etc/yum.repos.d/local.repomount /dev/cdrom /mnt/mkdir -p /opt/centoscp /mnt/* /opt/centosyum 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:" rbif [ $rb -eq 1 ];thenrebootelif [ $rb -eq 2 ];thenecho "Give up reboot,Think........"else echo "Error Input Number"fi
如果觉得《Shell脚本个例二》对你有帮助,请点赞、收藏,并留下你的观点哦!