Python Remote Statistics file
#!/usr/bin/python#encoding=utf-8import timeimport osimport paramikoimport multiprocessing# Statistics file number def get_total (IP, Password,filepath): Paramiko.util.log_to_file (' Paramiko.log ') Ssh=paramiko. Sshclient () Ssh.set_missing_host_key_policy (Paramiko. Autoaddpolicy ()) Try:starttime=time.time () Ssh.connect (hostname=ip,port=22,username= ' root ', Password=password) # Stdin,stdout,stderr = Ssh.exec_command (str (LEN (Os.listdir (filepath))) Stdin,stdout,stderr = Ssh.exec_command (' CD filepath, ls |wc-l ') #print ip,filepath,stdout.read (). strip (' \ n ') Count=int (Stdout.read (). strip (' \ n ')) endtime=t Ime.time () caltime=endtime-starttime result=ip+ ', ' +filepath.strip (' \ n ') + ', ' +str (count) + ', ' +str (caltime) + ' \ n ' retu RN result except:result=ip+ ', ' +filepath.strip (' \ n ') + ', ' + ' failed ' + ' \ n ' return result# read IP, password, ip.csv each behavior 192.168.1.1, 111111,/var The first column is the IP address, the second is the password, and the third column is the path Iplist=open (' Ip.csv '). ReadLines () #存入统计结果ipresultlist =[' Ip,filepath,count, Timecost\n '] #多进程统计pool =multiprocessing. Pool (processes=6) #循环每一行进行统计for IP in Iplist:ipin=ip.split (', ') Pool.apply_async (Ipresultlist.append (Get_total (ipin[0), IPIN[1],IPIN[2])) Pool.close () pool.join () #写入文件fp =open (' tongji_log ' + ' _ ' +time.strftime ('%y%m%d%h%m%s '), Time.localtime ()) + '. csv ', ' A + ') fp.writelines (ipresultlist) fp.close ()
The above mentioned is the whole content of this article, I hope you can like.