Shell 实现系统初始化 修改IP
p是prompt的缩写,即由这个开百关项给出提示信息,例如:read -p "please input you choice:" input 回车度后屏幕会显示:问
please input you choice: 那么之后你输入的东西就答会被保存到input变量中了
[root@www ~]# read -p "please input you choice:" input
please input you choice:yes
[root@www ~]# echo $input
yes
在cp指令前面加反斜杠可以不弹出是否覆盖的询问而直接覆盖!
[root@www ~]# cp a.txt /tmp/
cp: overwrite ‘/tmp/a.txt’? yes
[root@www ~]# \cp a.txt /tmp/
[root@www ~]# echo $?
0
系统初始化
此脚本用于新装Linux的相关配置工作,比如更换默认yum源,优化系统内核、停掉一些没必要启动的系统服务等。此脚本尤其适合大批新安装的CentOS系列的服务器。适用于Centos7
shell>vim cenots_7_system_init.sh
#!/bin/bash
# Filename: centos7-init.sh
# Author: test
#判断是否为root用户
if [ `whoami` != "root" ];then
echo " only root can run it"
exit 1
fi
#执行前提示
echo -e "\033[31m 这是centos7系统初始化脚本,将更新系统内核至最新版本,请慎重运行!\033[0m"
read -s -n1 -p "Press any key to continue or ctrl+C to cancel"
echo "Your inputs: $REPLY"
#1.定义配置yum源的函数
yum_config(){
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all && yum makecache
}
#2.定义配置NTP的函数
ntp_config(){
yum –y install chrony
systemctl start chronyd && systemctl enable chronyd
timedatectl set-timezone Asia/Shanghai && timedatectl set-ntp yes
}
#3.定义关闭防火墙的函数
close_firewalld(){
systemctl stop firewalld.service &> /dev/null
systemctl disable firewalld.service &> /dev/null
}
#4.定义关闭selinux的函数
close_selinux(){
setenforce 0
sed -i 's/enforcing/disabled/g' /etc/selinux/config
}
#5.定义安装常用工具的函数
yum_tools(){
yum install –y vim wget curl curl-devel bash-completion lsof iotop iostat unzip bzip2 bzip2-devel
yum install –y gcc gcc-c++ make cmake autoconf openssl-devel openssl-perl net-tools
source /usr/share/bash-completion/bash_completion
}
#6.定义升级最新内核的函数
update_kernel (){
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
yum --enablerepo=elrepo-kernel install -y kernel-ml
grub2-set-default 0
grub2-mkconfig -o /boot/grub2/grub.cfg
}
#执行脚本
main(){
yum_config;
ntp_config;
close_firewalld;
close_selinux;
yum_tools;
update_kernel;
}
main
修改动态IP->静态IP或者静态IP->静态IP
[root@www ~]# cat auto_change_ip.sh
#!/bin/bash
eth_ip="$1"
eth_mask="255.255.255.0"
eth_gateway="192.168.179.2"
eth_name="ens33"
eth_dns1="114.114.114.114"
eth_dns2="8.8.8.8"
eth_dir="/etc/sysconfig/network-scripts"
eth_bak="/data/backup/`date +%F`"
function change_ip(){
cat >> $eth_dir/ifcfg-$eth_name <<EOF
TYPE="Ethernet"
BOOTPROTO="static"
NAME="$eth_name"
IPADDR="$eth_ip"
GATEWAY="$eth_gateway"
NETMASK="$eth_mask"
DNS1="$eth_dns1"
DNS2="$eth_dns2"
EOF
cat $eth_dir/ifcfg-$eth_name
echo -e "\033[32myThe ip change succeded!\033[0m"
}
if [ $# -eq 0 ];then
echo -e "\033[32m----------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0 192.168.179.100|192.168.179.101|help}\033[0m}"
exit
fi
grep "dhcp" $eth_dir/ifcfg-$eth_name
if [ $? -eq 0 ];then
mkdir -p $eth_bak
\cp $eth_dir/ifcfg-$eth_name $eth_bak
ls -l $eth_bak
change_ip
else
read -p "The static ipaddr already exit!!ensure change another static ip?" input
if [ $input=="yes" -o $input=="y" -o $input="Y" ];then
mkdir -p $eth_bak
\cp $eth_dir/ifcfg-$eth_name $eth_bak
ls -l $eth_bak
change_ip
else
exit
fi
fi
read -p "the server ip change succeded,do you want to restart network?" input
if [ $input=="yes" -o $input=="Y" -o $input=="y" ];then
systemctl restart network.service
fi
目录 返回
首页