paramiko Python模块在linux中读取ncdu命令的结果

时间:2015-02-10 07:31:20

标签: python linux paramiko

我试图使用paramiko在Python中编写一个简单的代码,以使用ncdu命令检索远程目录的磁盘空间使用情况。但ncdu似乎与paramiko无关。 ncdu使用ncurses。任何人都可以帮我提供解决方案吗?

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()

2 个答案:

答案 0 :(得分:1)

使用"更原始的"命令:

du -hs /directory/ 

检索目录的维度。

-s而不是默认输出,仅报告每个指定文件的总和 -h以人类可读格式打印尺寸(例如,1K 234M 2G)

链接到手册页here

答案 1 :(得分:0)

根据手册,NCDU有一个-o标志,允许您输出到文件或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.

它的输出如下:

[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}]]%

与ncdu的ncurses输出不同,遗憾的是,这不会将目录组合在一起,这必须由您的脚本完成。