簡體   English   中英

什么是獲取Unix / Linux遠程主機支持的文件/目錄信息的最佳方法

[英]what's the best way to get file/directory information that's supported on Unix/Linux remote host

我有一個python代碼,需要在Unix / Linux(操作系統名稱:HP-UX / RedHat / SunOS / AIX / Linux /等)的遠程主機上獲取文件或目錄信息。

程序SSH進入遠程主機(使用paramiko庫)並執行ls -lls -ld具體取決於它是文件還是目錄。

我需要的信息是:

  1. 許可(用戶/組/其他)
  2. 用戶所有者
  3. 集團所有者
  4. 上一次更改
  5. 文檔名稱

然而, ls的問題是:

  1. 輸出與平台不同,因此需要特殊處理,這使得代碼使用檢查變得冗長。
  2. 文件大小單位在輸出中可能不同,取決於環境變量(對於GNU coreutils,BLOCK_SIZE),或者某些甚至可能不支持此。 這還需要特定於平台的檢查。

我正在尋找一個python庫或簡單的可移植可執行文件(如果有的話)。

我考慮過的解決方案(但似乎不可行)

  1. 如果匹配konwn格式,請使用正則表達式檢查輸出和處理的格式。 但是,由於嘗試檢查,這似乎很容易出錯。
  2. 還要嘗試檢查文件大小的環境變量,並在輸出中找出文件大小單位。 (例如,將幾個字符回顯到文件並檢查單位。如果寫入4個字符並且大小為1,那么我確定單位大於1個字節。重復這些步驟)。 這似乎也容易出錯
  3. 在每個主機上安裝跨平台編譯器,編譯然后執行。 無法執行此操作,因為如果重新安裝主機操作系統或將主機操作系統還原到沒有編譯器的位置,則需要重復此安裝過程。

有什么建議么?

stat做的伎倆嗎? 它似乎具有您正在尋找的所有功能,應該已經安裝好了。

http://ss64.com/bash/stat.html

如果你想在python中工作,python有一個名為stat的內置庫,它提供了類似的功能:

https://docs.python.org/2/library/stat.html

SFTP是SSH-2中內置的標准文件傳輸協議。 因此,如果您可以通過SSH連接到遠程主機,那么很可能您可以以標准方式使用SFTP來列出和統計文件。 SFTP 得到了包括OpenSSH在內的SSH服務器的廣泛支持

paramiko可能是Python最流行的SSH / SFTP包裝器。 以下是使用paramiko執行SFTP統計的示例腳本:

import paramiko

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.WarningPolicy()) # Only warn on no known_hosts
ssh.connect("localhost", username="sam")
sftp = ssh.open_sftp()
try:
  listing = sftp.listdir_attr(".")
  print(listing[0:10])
  my_stat = sftp.stat(".")
  print(my_stat)
  print(my_stat.st_size, my_stat.st_mtime, my_stat.st_atime, my_stat.st_uid, my_stat.st_gid)
except IOError:
  pass
ssh.close()

當然這只是一個簡單的例子,你可以用paramiko SFTP API做更多的事情 - 文檔


更新:相關文章: Python中的SFTP? (獨立於平台)

你可能也沒有在所有這些機器上安裝python,但我敢打賭你甚至在HPUX盒子上都有一些古老的硬殼版perl。

~/tmp/t3 $perl -e 'print (join (",", (stat("t.awk"))), "\n");'
655368,160089,33204,1,16257,200,0,178,1480727842,1480710575,1480710575,4096,8

~/tmp/t3 $stat t.awk
  File: `t.awk'
  Size: 178             Blocks: 8          IO Block: 4096   regular file
Device: a0008h/655368d  Inode: 160089      Links: 1
Access: (0664/-rw-rw-r--)  Uid: (16257/mcgowan)   Gid: (  200/    users)
Access: 2016-12-02 17:17:22.000000000 -0800
Modify: 2016-12-02 12:29:35.000000000 -0800
Change: 2016-12-02 12:29:35.000000000 -0800

這是perl stat字段的交叉引用:

  0 dev      device number of filesystem
  1 ino      inode number
  2 mode     file mode  (type and permissions)
  3 nlink    number of (hard) links to the file
  4 uid      numeric user ID of file's owner
  5 gid      numeric group ID of file's owner
  6 rdev     the device identifier (special files only)
  7 size     total size of file, in bytes
  8 atime    last access time in seconds since the epoch
  9 mtime    last modify time in seconds since the epoch
 10 ctime    inode change time in seconds since the epoch (*)
 11 blksize  preferred I/O size in bytes for interacting with the
             file (may vary from file to file)
 12 blocks   actual number of system-specific blocks allocated
             on disk (often, but not always, 512 bytes each)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM