Python remote statistics file code sharing

Source: Internet
Author: User

Python remote statistics file code sharing

It is a simple and practical function code for Python to obtain the remote file size. It is a basic instance for learning Python programming.

Python remote statistics File

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

#! /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 ()

The above is all the content of this article. I hope you will like it.

Note <>: For more exciting tutorials, please pay attention to the help house programming

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.