从KVM宿主机配置KVM虚拟机IP

目的

KVM创建虚机后,想从宿主机去配置IP地址。本文介绍两种方式,能够通过shell脚本配置虚拟机IP地址

方法一

guestmount工具
guestmount 能够将虚拟机的磁盘挂载到宿主机上面,再通过修改磁盘里配置文件的方式达到更改IP目的,宿主机需要安装软件包libguestfs和libguestfs-tools
此方法不需要知道虚机账号密码也可操作,除了改IP还能改其他的,别瞎整哦

#!/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

方法二

expect工具
expect是一个免费的编程工具,能用于shell自动的交互式任务,要用的话需要先手动安装,配置过程百度很多
此方法需要你知道虚机账号密码

#!/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

你可能感兴趣的:(bash脚本,tcp/ip,网络,运维,bash)