Last active
January 13, 2016 07:40
-
-
Save willianantunes/c8e4ad230c30297c4a1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
# Script to set network parameters from 'vmtoolsd --cmd "info-get guestinfo.ovfenv"'. It is designed for SLES 11 SP3 | |
# Author: Willian Antunes | |
# Date of creation: September 03, 2014 | |
import os, sys, time, subprocess, re | |
from xml.dom.minidom import parseString | |
def findXmlSection(dom, sectionName): | |
sections = dom.getElementsByTagName(sectionName) | |
return sections[0] | |
def getPropertyMap(ovfEnv): | |
dom = parseString(ovfEnv) | |
section = findXmlSection(dom, "PropertySection") | |
propertyMap = {} | |
for property in section.getElementsByTagName("Property"): | |
key = property.getAttribute("oe:key") | |
value = property.getAttribute("oe:value") | |
propertyMap[key] = value | |
dom.unlink() | |
return propertyMap | |
print "Issuing the command echo `vmtoolsd --cmd \"info-get guestinfo.ovfenv\"` to get the vApp configuration..." | |
ovfEnv = subprocess.Popen("echo `vmtoolsd --cmd \"info-get guestinfo.ovfenv\"`", shell=True, stdout=subprocess.PIPE).stdout.read() | |
propertyMap = getPropertyMap(ovfEnv) | |
ip = propertyMap["ip"] | |
netmask = propertyMap["netmask"] | |
gateway = propertyMap["gateway"] | |
dns1 = propertyMap["dns1"] | |
dns2 = propertyMap["dns2"] | |
hostname = propertyMap["hostname"] | |
domain = propertyMap["domain"] | |
ntp = propertyMap["ntp"] | |
print "The following properties will be used to set network parameters: " | |
print "--- IP: ", ip | |
print "--- Netmask: ", netmask | |
print "--- Gateway: ", gateway | |
print "--- Preferred DNS server: ", dns1 | |
print "--- Alternate DNS server: ", dns2 | |
print "--- Hostname: ", hostname | |
print "--- domain: ", domain | |
print "--- ntp: ", ntp | |
print "Cleaning up the device list..." | |
# It will prevent the case when the VM has two NICs. Generally it occurs when a new deploy is made | |
print subprocess.Popen("yast2 lan delete id=0 verbose", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("yast2 lan delete id=1 verbose", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print "Obtaining device name..." | |
p = subprocess.Popen('yast2 lan list', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
returnCode = p.wait() | |
stdout = p.stdout.read() | |
deviceName = re.search(r'\s.*,', stdout).group().strip()[:-1] | |
print "Device name: ", deviceName | |
print "Configuring NTP parameters..." | |
print subprocess.Popen("yast2 ntp-client add server=%s" % (ntp), shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("yast2 ntp-client enable", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print "Configuring IP parameters..." | |
print subprocess.Popen("echo \"default %s - -\" > /etc/sysconfig/network/routes" % (gateway), shell=True, stdout=subprocess.PIPE).stdout.read() | |
print "Creating the file ifcfg-eth0..." | |
print subprocess.Popen("echo \"BOOTPROTO='static'\" > /etc/sysconfig/network/ifcfg-eth0", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("echo \"BROADCAST=''\" >> /etc/sysconfig/network/ifcfg-eth0", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("echo \"ETHTOOL_OPTIONS=''\" >> /etc/sysconfig/network/ifcfg-eth0", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("echo \"IPADDR='%s/%s'\" >> /etc/sysconfig/network/ifcfg-eth0" % (ip, netmask), shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("echo \"MTU=''\" >> /etc/sysconfig/network/ifcfg-eth0", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("echo \"NAME='%s'\" >> /etc/sysconfig/network/ifcfg-eth0" % (deviceName), shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("echo \"NETWORK=''\" >> /etc/sysconfig/network/ifcfg-eth0", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("echo \"REMOTE_IPADDR=''\" >> /etc/sysconfig/network/ifcfg-eth0", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("echo \"STARTMODE='auto'\" >> /etc/sysconfig/network/ifcfg-eth0", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("echo \"USERCONTROL='no'\" >> /etc/sysconfig/network/ifcfg-eth0", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print "File created!" | |
print "Setting the current NIC to use 'eth0' as its name..." | |
print subprocess.Popen("head -n -1 /etc/udev/rules.d/70-persistent-net.rules | cat > /tmp/70-persistent-net.rules", shell=True, stdout=subprocess.PIPE).stdout.read() | |
netRules = subprocess.Popen("tail -n 1 /etc/udev/rules.d/70-persistent-net.rules", shell=True, stdout=subprocess.PIPE).stdout.read() | |
newNameValue = ", NAME=\"eth0\"" | |
netRulesNew = netRules[:-(len(netRules)-netRules.rindex(','))] + newNameValue | |
# The command bellow will allow python to pass double quotes to the shell | |
netRulesNew = netRulesNew.replace("\"", "\\\"") | |
print subprocess.Popen("echo \"{0}\" >> /tmp/70-persistent-net.rules".format(netRulesNew), shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("mv /tmp/70-persistent-net.rules /etc/udev/rules.d/70-persistent-net.rules", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print "Configuring DNS parameters..." | |
print subprocess.Popen("sed -i \"s/NETCONFIG_DNS_STATIC_SERVERS=.*/NETCONFIG_DNS_STATIC_SERVERS=\\\"%s %s\\\"/g\" /etc/sysconfig/network/config" % (dns1, dns2), shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("head -n -1 /etc/hosts | cat > /tmp/hosts", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("echo \"%s %s.%s %s\" >> /tmp/hosts" % (ip, hostname, domain, hostname), shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("mv /tmp/hosts /etc/hosts", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("echo \"{0}.{1}\" > /etc/HOSTNAME".format(hostname, domain), shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("echo \"nameserver {0}\" >> /etc/resolv.conf".format(dns1), shell=True, stdout=subprocess.PIPE).stdout.read() | |
print subprocess.Popen("echo \"nameserver {0}\" >> /etc/resolv.conf".format(dns2), shell=True, stdout=subprocess.PIPE).stdout.read() | |
print "Restarting the network service..." | |
print subprocess.Popen("/etc/init.d/network restart", shell=True, stdout=subprocess.PIPE).stdout.read() | |
print "The configuration has been applied" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment