[Shell] 纯文本查看 复制代码
#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
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()
if os.popen('hostname').read().replace('\n','') == repl_host:
#windows加域
os.system('netdom join {0} /domain:render.com /userd:render\administrator /passwordd:123456 >C:\\add\\ad.txt'.format(repl_host))
os.system('shutdown -r -t 100')
else:
#生成更改主机名的注册表,在系统注册,然后重启系统
os.system('echo Windows Registry Editor Version 5.00 >C:\\add\\ComputerName.reg')
os.system('echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName] >>C:\\add\\ComputerName.reg')
os.system('echo "ComputerName={0}" >>C:\\add\\ComputerName.reg'.format(repl_host) )
os.system('echo [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters] >>C:\\add\\ComputerName.reg')
os.system('echo "NV Hostname"="{0}" >>C:\\add\\ComputerName.reg'.format(repl_host))
os.system('echo "Hostname"="{0}" >> C:\\add\\ComputerName.reg'.format(repl_host))
os.system('regedit /s C:\\add\\ComputerName.reg')
os.system('shutdown -r -t 100')
else:
print 'OS is not windows system' config.cfg格式如下:
刚学python,如果哪有写的不足的地方提出来,或者你有更好的方法提出来我,咱们一起学习。
|