通过vmware tools来为克隆出来的虚拟机配置IP地址

Windows和Linux配置IP地址都很简单,但前提是你能直接访问它们。

如果是一个刚完成克隆的虚拟机,如何配置IP地址呢?我们不能总是人工去做,但没配IP之前,系统自身的一切接口都不能用,只能从vmware tools上想办法了。上一篇里提到vmware tools可以在没网络的情况下传输文件,其实它还可以在没网络的情况下登录到操作系统中执行指定命令。



以下是通过pysphere来调vmware tools,进入虚拟机中执行命令的代码,目前可以在ubuntu/RedHat/Windows2003上使用。其实只要定制相关的脚本或命令,vmwaretools只是起一个通道的功能。


def ChangeVM_IP(vm,vm_os,vm_ip,vm_netmask,vm_gateway,vm_main_dns,vm_passwd=None):
                                                                
    if CommonDefinition.simulation:
        return True
    if vm_os=='ubuntu':
        cmd_path='/bin/echo'
        #echo ces | sudo -S /opt/ecloud/reconfig_ubuntu_network.sh'
        cmd_args=[vm_passwd,'|','sudo','-S','/opt/ecloud/reconfig_ubuntu_network.sh',vm_ip,vm_netmask,vm_gateway,vm_main_dns]
        try:
            pid=vm.start_process(cmd_path,args=cmd_args)
            time.sleep(10)
            return True
        except Exception,e:
            msg='Error in executing change ip command for %s.' % vm.get_property('name')
            myloger.error(msg)
            myloger.debug(trace_back())
            return False
                                                                
    if vm_os=='rhel5':
        cmd_path='/opt/ecloud/reconfig_network.sh'
        cmd_args=[vm_ip,vm_netmask,vm_gateway,vm_main_dns]
        try:
            pid=vm.start_process(cmd_path,args=cmd_args)
            time.sleep(10)
            return True
        except Exception,e:
            msg='Error in executing change ip command for %s.' % vm.get_property('name')
            myloger.error(msg)
            myloger.debug(trace_back())
            return False
    if vm_os=='windows2003':
        myloger.debug('config windows ip:')
        windows2003_eth_connection_name=VMware_CommonDefinition.windows2003_eth_connection_name
        cmd_path='C:\\WINDOWS\\system32\\netsh.exe'
        cmd_args_str='interface ip set address name="%s" source=static addr=%s mask=%s gateway=%s gwmetric=1' % (windows2003_eth_connection_name,vm_ip,vm_netmask,vm_gateway)
        cmd_args=cmd_args_str.split(' ')
        try:
            pid=vm.start_process(cmd_path,args=cmd_args)
            time.sleep(30)
            """
            real_vm_ip=str(vm.get_property('ip_address'))
            myloger.debug('real ip: %s' % real_vm_ip)
            if vm_ip!=str(vm.get_property('ip_address')):
                pid=vm.start_process(cmd_path,args=cmd_args)
                time.sleep(60)
                if vm_ip!=str(vm.get_property('ip_address')):
                    myloger.error('VM %s ip config error. Can not change ip to %s' % (vm.get_property('name'),vm_ip))
                    return False
                                                                            
            """
        except Exception,e:
            msg='Error in executing change ip command for %s.' % vm.get_property('name')
            myloger.error(msg)
            myloger.debug(trace_back())
            return False
                                                                    
        #config dns
        cmd_args_str='interface ip set dns name="%s" source=static addr=%s register=PRIMARY' % (windows2003_eth_connection_name,vm_main_dns)
        cmd_args=cmd_args_str.split(' ')
        try:
            time.sleep(5)
            pid=vm.start_process(cmd_path,args=cmd_args)
            time.sleep(10)
            return True
        except Exception,e:
            msg='Error in executing change dns command for %s.' % vm.get_property('name')
            myloger.error(msg)
            myloger.debug(trace_back())
            return False


相关脚本:

ubuntu:

echo "">/etc/network/interfaces
sed -i -e "$ i\auto lo \n\
iface lo inet loopback\n\
auto eth0\n\
iface eth0 inet static\n\
address $1\n\
netmask $2\n\
gateway $3\n\
dns-nameservers $4" /etc/network/interfaces
/etc/init.d/networking restart


RedHat5:

#!/bin/bash
#for redhat5
echo "">/etc/sysconfig/network-scripts/ifcfg-eth0
sed -i -e "$ i\DEVICE=eth0 \n\
BOOTPROTO=static\n\
NM_CONTROLLED=yes\n\
IPADDR=$1\n\
NETMASK=$2\n\
GATEWAY=$3\n\
ONBOOT=yes\n\
TYPE=Ethernet" /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i -e "s/\(nameserver \)\S*/\1$4/" /etc/resolv.conf
service network restart


你可能感兴趣的:(windows,虚拟机,ubuntu,操作系统,IP地址)