简体   繁体   中英

paramiko Python module to read result of ncdu command in linux

I was trying to write a simple code in Python using paramiko to retrieve the disk space usage of a remote directory using the ncdu command. But ncdu does not seems to be working with paramiko . ncdu uses ncurses . Can anyone help me provide a work-around for it?

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('xxxx', username='root', password='xxxx')
dom="xxxx"
stdin, stdout, stderr = ssh.exec_command('/scripts/whoowns %s' %dom)
test=(stdout.readlines())
new=test[0].strip()
stdin, stdout, stderr = ssh.exec_command('ncdu -q  /home/%s/public_html/' %new)
print (stdout);
ssh.close()

Use the "more primitive" command:

du -hs /directory/ 

To retrieve the dimension of the directory.

-s Instead of the default output, report only the total sum for each of the specified files.
-h print sizes in human readable format (eg, 1K 234M 2G)

Link to man page here .

According to the manual NCDU has an -o flag allowing you to output to a file or STDOUT:

-o FILE
           Export all necessary information to FILE instead of opening the browser interface. If FILE is "-", the
           data is written to standard output.  See the examples section below for some handy use cases.

           Be warned that the exported data may grow quite large when exporting a directory with many files. 10.000
           files will get you an export in the order of 600 to 700 KiB uncompressed, or a little over 100 KiB when
           compressed with gzip. This scales linearly, so be prepared to handle a few tens of megabytes when
           dealing with millions of files.

Its output will look like this:

[1,0,{"progname":"ncdu","progver":"1.10","timestamp":1425971095},
[{"name":"/var /lib/colord","asize":4096,"dsize":4096,"dev":2049,"ino":524803},
{"name":"storage.db","asize":7168,"dsize":8192,"ino":532806},
[{"name":"icc","asize":4096,"dsize":4096,"ino":524911}],
 {"name":"mapping.db","asize":4096,"dsize":4096,"ino":532801}]]%

Unlike the ncurses output of ncdu this does not group directories together unfortunately, this has to be done by your script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM