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 "$ iauto lo 

iface lo inet loopback

auto eth0

iface eth0 inet static

address $1

netmask $2

gateway $3

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 "$ iDEVICE=eth0 

BOOTPROTO=static

NM_CONTROLLED=yes

IPADDR=$1

NETMASK=$2

GATEWAY=$3

ONBOOT=yes

TYPE=Ethernet" /etc/sysconfig/network-scripts/ifcfg-eth0
sed -i -e "s/(nameserver )S*/1$4/" /etc/resolv.conf
service network restart