shell脚本修改主机名、配置IP、使用本地YUM仓库

环境: 1、已安装KVM的CentOS6宿主机器

           2、宿主机已安装httpd,IP为192.168.4.254,光盘挂载在/var/www/html/RHEL6下

           3、需要修改主机名、IP搭建YUM仓库的虚拟机

V2:

[root@**** data]# cat clone.sh
#!/bin/bash
####By:PlatoWG Date:20180401 Version:2####
####check tools and mount point####
which guestmount
if [ $? != 0 ];then
	yum -y install libguestfs.x86_64 libguestfs-tools.x86_64
fi
if [ -e /mnt/vdisk ];then
	echo "mount point exist!";else
	mkdir /mnt/vdisk
fi
####define variables####
vdisk=/mnt/vdisk
read -p "please input a image file:" img
echo $img
read -p "please input new hostname:" nhostname
echo $nhostname
read -p "please input new server IP:" nIP
echo $nIP
#######################
umount $vdisk &> /dev/null
guestmount -a $img -i $vdisk 
sed -i '/HOSTNAME/d' $vdisk/etc/sysconfig/network
sed -i '$aHOSTNAME='"${nhostname}"'' $vdisk/etc/sysconfig/network
sed -i "/BOOTPROTO/d" $vdisk/etc/sysconfig/network-scripts/ifcfg-eth0
sed -i '/IPADDR/d' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth0
sed -i '/INETMASK/d' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth0
sed -i -e '$aBOOTPROTO=static' -e '$aIPADDR='"${nIP}"'' -e '$aNETMASK=255.255.255.0' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth0
echo "[rhel-6]
name=Linux NSD
baseurl=http://192.168.4.254/RHEL6
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" > $vdisk/etc/yum.repos.d/rhel6.repo
IP=`sed -n '/IPADDR/p' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth0`
HOSTNAME=`sed -n '/HOSTNAME/p' $vdisk/etc/sysconfig/network`
umount $vdisk
echo "A new server had created!"
echo "Ner server information:"
echo "${HOSTNAME}"
echo "${IP}"

V3:

[root@**** data]# cat clone.sh
#!/bin/bash
####By:PlatoWG Date:20180408 Version:3####
####check tools and mount point####
which guestmount
if [ $? != 0 ];then
	yum -y install libguestfs.x86_64 libguestfs-tools.x86_64
fi
if [ -e /mnt/vdisk ];then
	echo "mount point exist!";else
	mkdir /mnt/vdisk
fi
####define variables####
vdisk=/mnt/vdisk
read -p "please input a image file:" img
read -p "please input new hostname:" nhostname
read -p "please input new server IP:" nIP
echo "Please confirm the NEW SERVER's informations"
if [ -e $img ];then
	echo "Your img file is $img";else
	echo "Your img file does not exist!";exit;
fi
echo "Your hostname is $nhostname"
echo "Your IP is $nIP"
read -p "Please enter "y" for confirmation:" confir
if [ $confir ! = 'y' ];then
	exit 100;
fi
#######################
umount $vdisk &> /dev/null
guestmount -a $img -i $vdisk 
sed -i '/HOSTNAME/d' $vdisk/etc/sysconfig/network
sed -i '$aHOSTNAME='"${nhostname}"'' $vdisk/etc/sysconfig/network
sed -i "/BOOTPROTO/d" $vdisk/etc/sysconfig/network-scripts/ifcfg-eth0
sed -i '/IPADDR/d' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth0
sed -i '/INETMASK/d' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth0
sed -i -e '$aBOOTPROTO=static' -e '$aIPADDR='"${nIP}"'' -e '$aNETMASK=255.255.255.0' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth0
echo "[rhel-6]
name=Linux NSD
baseurl=http://192.168.4.254/RHEL6
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" > $vdisk/etc/yum.repos.d/rhel6.repo
IP=`sed -n '/IPADDR/p' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth0`
HOSTNAME=`sed -n '/HOSTNAME/p' $vdisk/etc/sysconfig/network`
umount $vdisk
echo "A new server had created!"
echo "Ner server information:"
echo "${HOSTNAME}"
echo "${IP}"

V4:添加进度显示功能,修改四块网卡的ip(输入主机位即可),判断要修改的主机运行状态

#!/bin/bash
####By:PlatoWG@163.com Date:20180509 Version:4####
####check tools and mount point####
which guestmount
if [ $? != 0 ];then
        yum -y install libguestfs.x86_64 libguestfs-tools.x86_64
fi
if [ -e /mnt/vdisk ];then
        echo "mount point exist!";else
        mkdir /mnt/vdisk
fi
####define variables####
vdisk=/mnt/vdisk
read -p "please input a image file:" img
read -p "please input new hostname:" nhostname
read -p "please input new server Host Bit:" IP_host
echo "Please confirm the NEW SERVER's informations"
####check the image information,and domain running status####
if [ -e $img ];then
        echo "Your img file is $img";else
        echo "Your img file does not exist!";exit;
fi

domainname=`ls $img |cut -d"/" -f 6|cut -d"." -f 1`
run_status=`virsh list|grep -w "$domainname"|wc -l`
if [ $run_status -eq 1 ];then
        echo -e "\e[31m$domainname is running,Please turn off first.\e[0m"
        exit
fi
####confirm the modification informations####
echo "Your hostname is $nhostname"
echo -e "Your IP is:\n192.168.4.$IP_host\n192.168.2.$IP_host\n201.1.1.$IP_host\n201.1.2.$IP_host"
read -p "Please enter "y" for confirmation:" confir
if [ $confir != y ];then
        exit
fi
####define progress function####
function jindu {
    while [ 1 ];do
        echo -n "#"
        sleep 1
    done
}
jindu &
jindu_pid=$!
#######################
umount $vdisk &> /dev/null
guestmount -a $img -i $vdisk
sed -i '/HOSTNAME/d' $vdisk/etc/sysconfig/network
sed -i '$aHOSTNAME='"${nhostname}"'' $vdisk/etc/sysconfig/network
sed -i "/BOOTPROTO/d" $vdisk/etc/sysconfig/network-scripts/ifcfg-eth{0,1,2,3}
sed -i '/IPADDR/d' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth{0,1,2,3}
sed -i '/NETMASK/d' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth{0,1,2,3}
sed -i -e '$aBOOTPROTO=static' -e '$aIPADDR=192.168.4.'"${IP_host}"'' -e '$aNETMASK=255.255.255.0' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth0
sed -i -e '$aBOOTPROTO=static' -e '$aIPADDR=192.168.2.'"${IP_host}"'' -e '$aNETMASK=255.255.255.0' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth1
sed -i -e '$aBOOTPROTO=static' -e '$aIPADDR=201.1.1.'"${IP_host}"'' -e '$aNETMASK=255.255.255.0' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth2
sed -i -e '$aBOOTPROTO=static' -e '$aIPADDR=201.1.2.'"${IP_host}"'' -e '$aNETMASK=255.255.255.0' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth3
echo "[rhel-6]
name=Linux NSD
baseurl=http://192.168.4.254/RHEL6
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release" > $vdisk/etc/yum.repos.d/rhel6.repo
IP=`sed -n '/IPADDR/p' $vdisk/etc/sysconfig/network-scripts/ifcfg-eth{0,1,2,3}`
HOSTNAME=`sed -n '/HOSTNAME/p' $vdisk/etc/sysconfig/network`
umount $vdisk;rm -rf $vdisk
kill $jindu_pid
####revise the modification information####
echo 
echo "A new server had created!"
echo "New server information:"
echo "${HOSTNAME}"
echo "${IP}"




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值