[英]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 -l
或ls -ld
具体取决于它是文件还是目录。
我需要的信息是:
然而, ls
的问题是:
我正在寻找一个python库或简单的可移植可执行文件(如果有的话)。
我考虑过的解决方案(但似乎不可行)
有什么建议么?
stat
做的伎俩吗? 它似乎具有您正在寻找的所有功能,应该已经安装好了。
http://ss64.com/bash/stat.html
如果你想在python中工作,python有一个名为stat
的内置库,它提供了类似的功能:
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.