首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 给windows机器改名加

python 给windows机器改名加

作者头像
py3study
发布2020-01-07 10:13:00
1.3K0
发布2020-01-07 10:13:00
举报
文章被收录于专栏:python3python3
#^_^ 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[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: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'[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName]'
      regedit3 = r'"ComputerName"="{0}"'.format(repl_host)
      regedit4 = r'[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters]'
      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'

config.cfg 格式如下:

162733190.png
162733190.png

o(︶︿︶)o 唉。。这个代码写出来就用了1个小时,调试用了一天。。真纠结,不过收获了好多经验。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档