2015-02-10 71 views
0

我試圖用Python編寫一個簡單的代碼,使用paramiko來檢索使用ncdu命令的遠程目錄的磁盤空間使用情況。但ncdu似乎並沒有與paramikoncdu使用ncurses。任何人都可以幫我提供解決方法嗎?paramiko Python模塊讀取Linux中的ncdu命令的結果

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

回答

1

使用 「更原始的」 命令:

du -hs /directory/ 

要檢索的目錄的尺寸。

-s代替默認輸出,只報告每個指定文件的總和。
-h人可讀格式的打印尺寸(例如,1K 234M 2G)

鏈接到手冊頁here

0

根據手冊NCDU有-o標誌讓您輸出到文件或標準輸出:

-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的輸出這不會不幸地將目錄分組在一起,這必須由您的腳本來完成。