繁体   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