python实现多线程软件分发(局域网)
程序说明:
1、适用于局域网C类地址,如要适用CIDR,则需要改造程序;
2、多并发下发软件,但没有进行安装;
3、需要有远程PC的管理员帐号密码,比较适合域控环境;
4、比较适合企业IT管理员使用。
#-*- coding:cp936 -*-
#import sys
#reload(sys)
#sys.setdefaultencoding('utf8')
import subprocess
import threading
import os
print '开启软件分发程序'
print '-----------------------------------'
ipstart=raw_input('请键入你的起始IP地址 :')
ipend=raw_input('请键入你的结束IP地址 :')
user=raw_input('请输入客户端的管理员用户 :')
password=raw_input('请输入客户端管理员密码 :')
print '-----------------------------------'
print '\n'
ipstart='x.x.x.x'
ipend='x.x.xx'
user='xxx'
password='xxx'
ipstartlist=ipstart.split('.')
ipendlist=ipend.split('.')
a=ipstartlist[0]
b=ipstartlist[1]
c=ipstartlist[2]
start=int(ipstartlist[3])
end=int(ipendlist[3])
cmd="cmd.exe"
f=open(r'c:\cmd.txt','a')
h=1
n=1
#线程数
ep=(end-start+1)/n
remain=(end-start+1)%n
def deploy(start1,end1):
global h
while start1 <= end1:
p=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
#ping 进程
pp=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
#net use进程
ppp=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,
stdin=subprocess.PIPE,
stderr=subprocess.PIPE)
#copy进程
ipk=a,b,c,str(start1)
ip='.'.join(ipk)
cmd1=r'net use \\%s\ipc$ "%s" /user:"%s"'%(ip,password,user)+"\n"
cmd2=r'cmd /k xcopy /i /s /e /y D:\winxp\*.* \\"%s"\c$\winxp'%ip+"\n"
p.stdin.write("ping %s"%ip+"\n")
p.stdin.close()
#pp.wait()
print p.stdout.read()
pout=p.communicate()[0]
if pout.find('Reply from')>=0:
print '(%s)&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&'%h
print '* %s 客户端在线!!!'%ip
pp.stdin.write(cmd1)
pp.stdin.close()
#p.wait()
ppout=pp.stdout.read()
if ppout[143:145]=='命':
print "* 成功连接客户端: %s"%ip
print '\n'
f.write('%s %s 成功连接客户端'%(h,ip)+'\n')
f.write('\n')
ppp.stdin.write(cmd2)
ppp.stdin.close()
#ppp.wait()
pppout=ppp.stdout.read()
if pppout.find('11')==468:
f.write('%s %s 成功复制所有文件'%(h,ip)+'\n')
print "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
print '\n'
else:
print "文件复制失败"
f.write('%s 文件复制失败'%ip)
print "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
print '\n'
else:
print "* 客户端:%s连接失败"%ip
print '\n'
f.write('%s %s 客户端连接失败'%(h,ip)+'\n')
f.write('\n')
else:
print '\n\n'
print '(%s)##################################################'%h
print '* %s 客户端不在线'%ip
print '####################################################'
print '\n'
f.write('%s %s 客户端不在线'%(h,ip)+'\n')
start1=start1+1
h=h+1
if __name__=='__main__':
threads=[]
if ep >=1:
if remain == 0:
for i in xrange(0,n):
d=start+i*ep
endd=start+ep*(i+1)-1
threads.append(threading.Thread(None,target=deploy,args=(d,endd)))
else:
for i in xrange(0,n):
d=start+i*ep
endd=start+ep*(i+1)-1
threads.append(threading.Thread(None,target=deploy,args=(d,endd)))
threads.append(threading.Thread(None,target=deploy,args=(start+n*ep,end)))
else:
threading.Thread(None,target=deploy,args=(start,end))
for t in threads:
t.start()
for t in threads:
t.join()