分析家 发表于 2018-6-17 07:42:17

python 给windows机器改名加域

#^_^ coding=gbk ^_^  
import os,sys
  
import linecache
  
import uuid
  
def config():
  '获取配置文件里面的mac和主机名'
  fn = ('c:\\add\\config.cfg')
  lines = [ x.split('\n') for x in linecache.getlines(fn) if x.startswith('R')]
  ip_mac_lines = [ x 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')
  mac_adr1 = mac_adr.split(' ').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(' ')
  
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:render\\administrator /passwordd:密码>C:\\add\\ad.txt'.format(repl_host))
  os.system('regedit /s C:\\add\\denglu.reg')
  os.system('shutdown -r -t 20')
  os.system('del "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\rh.lnk"')
  else:
  #生成更改主机名的注册表,在系统注册,然后重启系统
  regedit1 = r'Windows Registry Editor Version 5.00'
  regedit2 = r''
  regedit3 = r'"ComputerName"="{0}"'.format(repl_host)
  regedit4 = r''
  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:\\add\\ComputerName.reg','w')
  f_name.write(f_file)
  f_name.close()
  os.system('regedit /s C:\\add\\ComputerName.reg')
  os.system('shutdown -r -t 30')
  
else:
  print 'OS is not windows system'
页: [1]
查看完整版本: python 给windows机器改名加域