Shell script: Do not log on to the KVM virtual machine, modify the virtual machine NIC IP address

Source: Internet
Author: User

# 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.