# Author: The Adventures of Tintin (Jacob)
# The script uses the Guestmount tool, Centos7.2 installed libguestfs-tools-c can get Guestmount tool
# The script modifies the virtual machine's IP address information without logging on to the virtual machine
# In some environments, the virtual machine does not have an IP or IP address and the real host is not in a network segment
# Real host in the absence of Virt-mange graphics, remote connection of virtual machines is cumbersome
# This script can solve a similar problem
#!/bin/bash#author: The Adventures of Tintin (Jacob) #该脚本使用guestmount工具, Centos7.2 install libguestfs-tools-c can get Guestmount tool # Script without logging on to the virtual machine, To modify the virtual machine's IP address information # In some environments, virtual machines do not have IP or IP addresses and real hosts are not in a network segment # Real hosts in the absence of Virt-mange graphics, remote connection to the virtual machine is cumbersome # The script can solve a similar problem read -p " Please enter the name of the virtual machine: " nameif virsh domstate $name |grep -q running ;then echo "To modify virtual machine NIC data, you need to shut down the virtual machine" virsh destroy $namefimountpoint = "/media/virtimage" [ ! -d $mountpoint]&& mkdir $mountpointecho "Please later ..." if mount | grep -q "$mountpoint" ;then umount $mountpointfiguestmount -d $name -i $ mountpointread -p "Please enter the name of the network adapter that needs to be modified:" devread -p "Please enter the IP address:" addr# determine if there is an IP address in the NIC configuration file, Just modify the IP, no, add a new IP address if grep -q "ipaddr" $mountpoint/etc/sysconfig/network-scripts/ifcfg-$ Dev;then sed -i "/ipaddr/s/=.*/= $addr/" $mountpoint/etc/sysconfig/network-scripts/ifcfg-$develse echo "Ipaddr= $addr" >> $mountpoint/etc/sysconfig/network-scripts/ ifcfg-$devfi # If there is a client-configured IP address in the NIC configuration file, the script prompts to modify IP completion awk -f= -v x= $addr ' $2==x{print ' Complete ... "} ' $mountpoint/etc/sysconfig/network-scripts/ifcfg-$dev
This article is from the "Ding Ding Adventures" blog, please be sure to keep this source http://manual.blog.51cto.com/3300438/1961344
Shell script: Do not log on to the KVM virtual machine, modify the virtual machine NIC IP address