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
發佈了149 篇原創文章 · 獲贊 64 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章