#!/bin/python
#
#coding=gbk
import os,sys
import linecache
import uuid
def config():
'获取配置文件里面的mac和主机名'
fn = ('c:addconfig.cfg')
lines = [ x.split('n') for x in linecache.getlines(fn) if x.startswith('R')]
ip_mac_lines = [ x[0] for x in lines ]
return ip_mac_lines
def host_mac():
'获取客户机器的
mac地址'
# node = uuid.getnode()
# mac = uuid.UUID(int=node)
# adr_mac = mac.hex[-12:]
# return adr_mac
#KVM
虚拟机获取mac地址的方法
mac = os.popen('getmac').read()
mac_adr = mac.split('n')[3]
mac_adr1 = mac_adr.split(' ')[0].replace('-','')
return mac_adr1
def replace_host():
'根据mac地址获取出对应更改主机的主机名'
cg_host = config()
h_mac = host_mac().lower()
for x in cg_host:
if x.lower().split(' ')[-1] == h_mac:
return x.lower().split(' ')[0]
if os.name == 'nt':
repl_host = replace_host()
hostname = os.popen('hostname').read().replace('n','')
if hostname == repl_host:
#windows加域
os.system('netdom join {0} /domain:render.com /userd:renderadministrator /passwordd:nscc_123456 >C:addad.txt'.format(repl_host))
os.system('regedit /s C:adddenglu.reg')
os.system('shutdown -r -t 20')
os.system('del "C:ProgramDataMicrosoftWindowsStart MenuProgramsStartuprh.lnk"')
else:
#生成更改主机名的
注册表,在系统注册,然后重启系统
regedit1 = r'Windows Registry Editor Version 5.00'
regedit2 = r'[HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlComputerNameComputerName]'
regedit3 = r'"ComputerName"="{0}"'.format(repl_host)
regedit4 = r'[HKEY_LOCAL_MACHINESYSTEMCurrentControlSetServicesTcpipParameters]'
regedit5 = r'"NV Hostname"="{0}"'.format(repl_host)
regedit6 = r'"Hostname"="{0}"'.format(repl_host)
f_file = '{0}n{1}n{2}n{3}n{4}n{5}'.format(regedit1,regedit2,regedit3,regedit4,regedit5,regedit6)
f_name = open('C:addComputerName.reg','w')
f_name.write(f_file)
f_name.close()
os.system('regedit /s C:addComputerName.reg')
os.system('shutdown -r -t 30')
else:
print 'OS is not windows system'