我有一个python代码,需要在一个名为Unix/Linux的远程主机上获取文件或目录信息(OS名称:HP-UX/RedHat/SunOS/AIX/Linux/等等)。
程序SSH进入远程主机(使用paramiko库),并根据文件或目录执行ls -l或ls -ld。
我需要的信息是:
然而,ls的问题是:
我正在寻找一个python库或简单的可移植的可执行文件,如果有一个。
我考虑过的解决方案(但似乎不可行)
有什么建议吗?
发布于 2016-12-05 23:49:00
stat会这样做吗?它似乎拥有所有你想要的功能,应该已经安装好了。
http://ss64.com/bash/stat.html
如果您想在python中工作,python有一个名为stat的内置库,它提供了类似的功能:
发布于 2016-12-06 00:51:56
SFTP是内置于SSH-2中的标准文件传输协议.因此,如果您可以在远程主机中使用SSH,那么很可能可以以标准的方式使用SFTP来列出和统计文件。SFTP是包括广泛支持在内的SSH服务器的OpenSSH。
帕拉米科可能是Python最流行的SSH/SFTP包装器。下面是一个使用paramiko进行SFTP stat的示例脚本:
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()当然,这只是一个简单的例子,您可以使用ParamikoSFTPAPI-文档做更多的事情。
更新:相关帖子:Python中的SFTP?(平台独立)
发布于 2016-12-06 01:17:32
您可能也没有在所有这些机器上安装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字段的交叉引用:
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)https://stackoverflow.com/questions/40985436
复制相似问题