如何获取有关通过 sshfs 挂载的远程目录的信息?

ter*_*don 5 mount sshfs

如果我使用 将远程服务器上的目录挂载到本地计算机上,我sshfs如何找到以下详细信息:

  • 当前是否安装了任何此类安装;
  • 安装它的用户;
  • 远程和本地目录;
  • 它被安装的时间。

ter*_*don 7

如果远程目录已挂载,它将列在mount. 其中包含您需要的大部分信息:

$ mount -t fuse.sshfs 
terdon@123.456.7.8:/remote/path/dir/ on /home/terdon/foo type fuse.sshfs (rw,nosuid,nodev,relatime,user_id=1001,group_id=1001)
Run Code Online (Sandbox Code Playgroud)

考虑到这一点,您可以编写一个小脚本来解析输出并为您提供大部分细节:

$ mount -t fuse.sshfs | 
    perl -ne '/.+?@(\S+?):(.+?)\s+on\s+(.+)\s+type.*user_id=(\d+)/; 
    print "Remote host: $1\nRemote dir: $2\nLocal dir: $3\nLocal user: $4\n"'
Remote host: 139.124.66.43
Remote dir: /cobelix/terdon/research/
Local dir: /home/terdon/haha
Local user: 1001
Run Code Online (Sandbox Code Playgroud)

这可以制作成一个 shell 函数或脚本,扩展为显示用户名而不是 UID 并从ps. 这假设您不需要毫秒精度,因为输出是ps指命令启动的时间,而不一定是挂载操作结束的时间。

$ mount -t fuse.sshfs 
terdon@123.456.7.8:/remote/path/dir/ on /home/terdon/foo type fuse.sshfs (rw,nosuid,nodev,relatime,user_id=1001,group_id=1001)
Run Code Online (Sandbox Code Playgroud)

如果将上述函数添加到 shell 的初始化文件(例如~/.bashrc用于 bash),则可以运行:

$ sshfs_info
Remote host: 123.456.7.8
Remote dir: /remote/path/dir
Local dir: /home/terdon/foo
Local user: terdon
Elapsed time: 44:16
Run Code Online (Sandbox Code Playgroud)

请注意,这假设只有一个 sftp 实例正在运行。如果您需要处理多个实例,请改用此实例:

$ mount -t fuse.sshfs | 
    perl -ne '/.+?@(\S+?):(.+?)\s+on\s+(.+)\s+type.*user_id=(\d+)/; 
    print "Remote host: $1\nRemote dir: $2\nLocal dir: $3\nLocal user: $4\n"'
Remote host: 139.124.66.43
Remote dir: /cobelix/terdon/research/
Local dir: /home/terdon/haha
Local user: 1001
Run Code Online (Sandbox Code Playgroud)

输出看起来像:

$ sshfs_info 
Remote host: 123.456.7.8
Remote dir: /remote/path/foobar/
Local dir: /home/terdon/baz
Local user: terdon
Elapsed time:    01:53:26
---
Remote host: 123.456.7.8
Remote dir: /remote/path/foo/
Local dir: /home/terdon/bar
Local user: terdon
Elapsed time:    01:00:39
---
Remote host: 123.456.7.8
Remote dir: /remote/path/bar/
Local dir: /home/terdon/baz
Local user: terdon
Elapsed time:       53:57
---
Remote host: 123.456.7.8
Remote dir: /remote/path/ho on ho
Local dir: /home/terdon/a type of dir
Local user: terdon
Elapsed time:       44:24
---
Run Code Online (Sandbox Code Playgroud)

正如您在上面的示例中看到的,它也可以处理包含空格的目录名称。

最后,请注意,这不是 100% 可移植的。它应该适用于任何具有 GNU 工具集的系统(例如任何 Linux 发行版),但它不适用于非 GNU 系统,因为它使用特定于 GNU grep 的功能。