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()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章