-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinux_ovaCustomized.py
67 lines (44 loc) · 2.18 KB
/
Linux_ovaCustomized.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import os
import sys
import subprocess
from xml.dom.minidom import parseString
def get_ovf_properties():
properties={}
ovfenv_cmd = "/usr/bin/vmtoolsd --cmd 'info-get guestinfo.ovfEnv'"
xml_parts=subprocess.Popen(ovfenv_cmd,shell=True,stdout=subprocess.PIPE).stdout.read()
raw_data=parseString(xml_parts)
for property in raw_data.getElementsByTagName('Property'):
key,value=[ property.attributes['oe:key'].value,property.attributes['oe:value'].value]
properties[key]=value
return properties
def network_seeting(ipaddr,prefix,gateway,hostname,dns):
sp=subprocess.Popen(["/usr/bin/hostnamectl","set-hostname",hostname],stderr=subprocess.PIPE,stdin=subprocess.PIPE)
sp.communicate()
sp=subprocess.Popen(["/usr/bin/nmcli connection modify ens160 ipv4.addresses "+ipaddr+"/"+prefix+" ipv4.gateway "+gateway+" ipv4.dns "+dns+" ipv4.method manual"],shell=True,stderr=subprocess.PIPE,stdin=subprocess.PIPE)
sp.communicate()
def change_root_passwd(root_password):
#subprocess.call(["echo "+root_password+ "| /usr/bin/passwd --stdin root"],shell=true)
sp=subprocess.Popen(["/usr/sbin/chpasswd"],stderr=subprocess.PIPE,stdin=subprocess.PIPE)
sp.communicate(input='root:%s' % root_password)
sp.wait()
if sp.returncode != 0:
print("Failed to set new for root")
def replace_line(filepath):
fin=open(filepath,"rt")
data=fin.read()
data.replace('/etc/rc.d/getOVF.py','')
fin.close()
fin=open(filepath,"wt")
fin.write(data)
fin.close()
properties = get_ovf_properties()
if len(properties['hostname'])>0 and len(properties['ipaddr'])>0 and len(properties['prefix'])>0 and len(properties['gateway'])>0 and len(properties['dns_server'])>0:
network_seeting(properties['ipaddr'], properties['prefix'], properties['gateway'], properties['hostname'], properties['dns_server'])
if len(properties['root_password'])>0:
change_root_passwd(properties['root_password'])
replace_line("/etc/rc.d/rc.local")
#subprocess.call("/usr/bin/sed '$ d' /etc/rc.d/rc.local",shell=True)
subprocess.call("/usr/bin/chmod u-x /etc/rc.d/rc.local",shell=True)
dir = os.getcwd()
os.remove(dir+'/%s' % sys.argv[0])