发布时间:2019-09-17 07:44:29编辑:auto阅读(1606)
#^_^ 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 格式如下:
o(︶︿︶)o 唉。。这个代码写出来就用了1个小时,调试用了一天。。真纠结,不过收获了好多经验。
上一篇: Python操作MySQL(二) ORM
下一篇: python之程序打包
47840
46390
37280
34733
29312
25971
24911
19950
19544
18030
5791°
6411°
5927°
5961°
7063°
5911°
5942°
6437°
6404°
7778°