Configure the KVM virtual machine IP from the KVM host

Purpose

After KVM creates a virtual machine, you want to configure the IP address from the host machine. This article introduces two ways to configure the virtual machine IP address through shell scripts

method one

The guestmount tool
guestmount can mount the virtual machine's disk to the host, and then change the IP by modifying the configuration file in the disk. The host needs to install the software packages libguestfs and libguestfs-tools. This method does not require knowing the virtual machine account and password
. It’s also operable. In addition to changing the IP, you can also change other things. Don’t mess around.

#!/bin/bash
#author 对你無語
#传参:虚机名称,网卡接口名称,IP,掩码,网关
#需要安装guestmount工具
#在不登陆虚拟机的情况下,修改虚拟机的IP地址信息

#获取虚机名称,-p是提示符
read -p "please input the Virtual machine name:" name

#如果虚拟机状态为running,则强制关闭虚机,开机不能操作哟
#关机用destroy强制关机,用shutdown会很慢,甚至有时关不掉
if virsh domstate $name|grep -q running ;then
       echo "shutdown the Virtual machine ..."
       virsh destroy $name
fi

#宿主机上创建挂载点mountpoin目录,
mountpoint="/media/v_path"
[ ! -d $mountpoint ] && mkdir $mountpoint
echo "创建mountpoin目录"

#确认没有挂载mountpoin
if mount | grep -q "$mountpoint" ;then
        umount $mountpoint
        echo "已存在挂载点,卸载mountpoint"
fi

#安装guestmount,已存在也不会重复安装
if rpm -q libguestfs|grep -q not ;then
        yum install libguestfs libguestfs-tools -y
        echo "guestmount安装中"
fi

#挂载虚机全部磁盘
guestmount  -d $name -i $mountpoint

#用户输入虚拟机网卡信息,和想要配置的IP地址
read -p "请输入需要修改的网卡名称:" dev
read -p "请输入IP地址:" addr
read -p "请输入掩码地址:" netmask
read -p "请输入网关地址:" gateway

# 判断原本网卡配置文件中是否有IP地址,有,就修改该IP,没有,就添加一个新的IP地址
if grep -q "IPADDR" $mountpoint/etc/sysconfig/network-scripts/ifcfg-$dev;then
   sed -i "/IPADDR/s/=.*/=$addr/"  $mountpoint/etc/sysconfig/network-scripts/ifcfg-$dev
   sed -i "/NETMASK/s/=.*/=$netmask/"  $mountpoint/etc/sysconfig/network-scripts/ifcfg-$dev
   sed -i "/GATEWAY/s/=.*/=$gateway/"  $mountpoint/etc/sysconfig/network-scripts/ifcfg-$dev
   sed -i "/BOOTPROTO/s/=.*/=static/"  $mountpoint/etc/sysconfig/network-scripts/ifcfg-$dev

else
   sed -i "/BOOTPROTO/s/=.*/=static/"  $mountpoint/etc/sysconfig/network-scripts/ifcfg-$dev
   echo "IPADDR=$addr" >>  $mountpoint/etc/sysconfig/network-scripts/ifcfg-$dev
   echo "NETMASK=$netmask" >>  $mountpoint/etc/sysconfig/network-scripts/ifcfg-$dev
   echo "GATEWAY=$gateway" >>  $mountpoint/etc/sysconfig/network-scripts/ifcfg-$dev
fi
# 判断配置文件是否有此IP了
awk -F= -v ip=$addr '$2==ip{print "修改IP成功"}' $mountpoint/etc/sysconfig/network-scripts/ifcfg-$dev
# 配置完成后删除挂载点
umount $mountpoint
# 启动虚机
virsh start $name

Method Two

The expect tool
expect is a free programming tool that can be used for automatic shell interactive tasks. If you want to use it, you need to install it manually first. There are many configuration processes in Baidu. This
method requires you to know the virtual machine account password.

#!/usr/expect/bin/expect
# 获取虚拟机参数
set virshname  [lindex $argv 0]
set ipaddr [lindex $argv 1]
set NETMASK [lindex $argv 2]
set GATEWAY  [lindex $argv 3]

# 打开虚拟机控制台连接
spawn virsh console $virshname

# 等待一段时间以确保虚拟机启动
sleep 5
set timeout 10
send "\r"

# 匹配登录提示并发送用户名
expect {
    
    
    "login:" {
    
    
        send "root\r"
        exp_continue
    }
    timeout {
    
    
        # 在5秒超时后跳过登录步骤并执行下面的命令
    }
}

# 匹配密码提示并发送密码
expect {
    
    
    "Password:" {
    
    
        send "ENsafe@2022\r"
        exp_continue
    }
    timeout {
    
    
        # 在5秒超时后跳过登录步骤并执行下面的命令
    }
}

# 等待一段时间以确保登录完成
sleep 2

# 判断原本网卡配置文件中是否有IP地址,有,就修改该IP,没有,就添加一个新的IP地址
send "grep -q 'IPADDR' /etc/sysconfig/network-scripts/ifcfg-eth0 && echo '1111' || echo '0000'\r"

expect {
    
    
    "1111" {
    
    
      send  "echo 1111\r"
      send  "sed -i '/^BOOTPROTO/ s/.*/BOOTPROTO=static/' '/etc/sysconfig/network-scripts/ifcfg-eth0'\r"
      
      send  "sed -i '/^IPADDR/ s/.*/IPADDR=${ipaddr}/' '/etc/sysconfig/network-scripts/ifcfg-eth0'\r"
      
      send  "sed -i '/^NETMASK/ s/.*/NETMASK=${NETMASK}/' '/etc/sysconfig/network-scripts/ifcfg-eth0'\r"
      
      send  "sed -i '/^GATEWAY/ s/.*/GATEWAY=${GATEWAY}/' '/etc/sysconfig/network-scripts/ifcfg-eth0'\r"
     
    }
    "0000" {
    
    
        send  "echo 0000\r"
        
        send "echo 'IPADDR=${ipaddr}' >>  /etc/sysconfig/network-scripts/ifcfg-eth0\r"
        
        send  "sed -i '/^BOOTPROTO/ s/.*/BOOTPROTO=static/' '/etc/sysconfig/network-scripts/ifcfg-eth0'\r"

        send  "sed -i '/^NETMASK/ s/.*/NETMASK=${NETMASK}/' '/etc/sysconfig/network-scripts/ifcfg-eth0'\r"
        
        send  "sed -i '/^GATEWAY/ s/.*/GATEWAY=${GATEWAY}/' '/etc/sysconfig/network-scripts/ifcfg-eth0'\r"
    }
}

# 改了之后重启网络使之生效
send  "systemctl restart network.service\r"
expect "$ "

# 退出登录
send "exit\r"
exit

Guess you like

Origin blog.csdn.net/forlorn_mere/article/details/131572204