Shell脚本个例二
【摘要】
实验要求: 实验内容 设计与实现一个系统配置的 Shell 脚本。功能模块如下: 1 配置主机名、输出当前主机名、根据用户输入设置主机名
2 配置网络模块、备份当前的网络配置、自动配置网络为静态 IP ...
实验要求:
实验内容
设计与实现一个系统配置的 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
- 1
- 2
- 3
- 4
- 5
Hostname.sh如下
echo "@@@@@@@@@@@@@@@ This Is Hostname Set @@@@@@@@@@@@@@@"
echo $HOSTNAME
read -p "Please set hostname:" hostname
hostnamectl set-hostname $hostname
bash
- 1
- 2
- 3
- 4
- 5
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
文章来源: blog.csdn.net,作者:指剑,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/fly1574/article/details/95388775
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)