|
#! /Usr/bin/python
# Encoding = UTF-8
Import time
Import OS
Import paramiko
Import multiprocessing
# Count the number of files
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.exe c_command (str (len (OS. listdir (filepath ))))
Stdin, stdout, stderr = ssh.exe c_command ('CD filepath; ls | wc-l ')
# Print ip, filepath, stdout. read (). strip ('\ n ')
Count = int (stdout. read (). strip ('\ n '))
Endtime = time. time ()
Caltime = endtime-starttime
Result = ip + ',' + filepath. strip ('\ n') +', '+ str (count) +', '+ str (caltime) +' \ N'
Return result
Except t:
Result = ip + ',' + filepath. strip ('\ n') +', '+ 'failed' +' \ N'
Return result
When reading the ipsag password, each line of ip.csv is 192.168.1.1, 111111, the first column of/var is the IP address, the second column is the password, and the third column is the path
Iplistmediaopen('ip.csv '). readlines ()
# Storing statistical results
Ipresultlist = ['IP, FILEPATH, COUNT, TIMECOST \ n']
# Multi-process Statistics
Pool = multiprocessing. Pool (processes = 6)
# Collect statistics for each row in a loop
For ip in iplist:
Ipin = ip. split (',')
Pool. apply_async (ipresultlist. append (get_total (ipin [0], ipin [1], ipin [2])
Pool. close ()
Pool. join ()
# Writing files
Comment ', 'a + ')
Fp. writelines (ipresultlist)
Fp. close ()
|