Shell脚本个例二

本文涉及的产品
云防火墙,500元 1000GB
简介: Shell脚本个例二

实验要求:
实验内容
设计与实现一个系统配置的 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


目录
相关文章
|
19小时前
|
Shell
蓝易云 - 简单shell脚本的编写教程
以上就是编写一个基本Shell脚本的步骤。当然,Shell脚本可以做的远不止这些,你可以使用变量,控制结构(如if语句和循环),以及各种Shell命令和功能来编写更复杂的脚本。
3 0
|
1天前
|
机器学习/深度学习 Unix Java
技术笔记:Linux之Shell脚本编程(一)
技术笔记:Linux之Shell脚本编程(一)
|
4天前
|
Shell
Shell脚本之条件语句if总结
Shell脚本之条件语句if总结
|
11天前
|
监控 Shell Linux
Linux的Shell脚本详解
Linux的Shell脚本详解
|
15天前
|
Shell
shell脚本
shell脚本
16 2
|
23天前
|
Ubuntu Java Linux
Linux centos7 ubuntu 一键安装Java JDK 脚本 shell 脚本
Linux centos7 ubuntu 一键安装Java JDK 脚本 shell 脚本
35 2
|
23天前
|
Ubuntu Shell Linux
linux shell 后台执行脚本的方法 脚本后台运行 后台运行程
linux shell 后台执行脚本的方法 脚本后台运行 后台运行程
14 0
|
23天前
|
监控 Shell Linux
shell linux中用shell写一个占用CPU的脚本
shell linux中用shell写一个占用CPU的脚本
24 0
|
23天前
|
Shell Linux
shell linux中shell脚本编写俄罗斯方块
shell linux中shell脚本编写俄罗斯方块
21 1
|
24天前
|
Shell Linux Perl
Linux shell脚本sed使用
Linux shell脚本sed使用
15 1