Python Remote statistics file code sharing

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.