sshfsを介してマウントされたリモートディレクトリに関する情報を取得するには?

sshfsを介してマウントされたリモートディレクトリに関する情報を取得するには?

自分のローカルコンピュータでリモートサーバーを使用してディレクトリをマウントする場合は、次のsshfs詳細をどのように見つけることができますか?

  • そのようなマウントが現在インストールされているかどうか
  • それをインストールしたユーザー。
  • リモートおよびローカルディレクトリ。
  • インストール時間。

ベストアンサー1

リモートディレクトリがマウントされると、出力に一覧表示されますmount。これには必要なほとんどの情報が含まれています。

$ mount -t fuse.sshfs 
[email protected]:/remote/path/dir/ on /home/terdon/foo type fuse.sshfs (rw,nosuid,nodev,relatime,user_id=1001,group_id=1001)

これを念頭に置いて、出力を解析し、ほとんどの詳細を提供する小さなスクリプトを書くことができます。

$ 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

これはシェル関数またはスクリプトで作成し、UIDの代わりにユーザー名を表示し、時間を抽出するように拡張できますps。これはミリ秒精度が不要であると仮定します。なぜなら、その出力はpsコマンドが開始された時間を参照するからであり、必ずしもインストール操作が終了した時間を意味するわけではありません。

sshfs_info(){
    mount -t fuse.sshfs | head -n1 |
    perl -ne '/.+?@(\S+?):(.+)(?= on\s+\/)(.+)\s+type.*user_id=(\d+)/; 
     print "Remote host: $1\nRemote dir: $2\nLocal dir: $3\nLocal user: " . 
     `grep  :1001: /etc/passwd | cut -d: -f1` '
    printf "Elapsed time: %s\n" $(ps -p $(pgrep -f sftp | head -n1) o etime=)
    }

上記の関数をシェルの初期化ファイル(~/.bashrcbashなど)に追加すると、次のようになります。

$ 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

これは、1つのsftpインスタンスのみが実行されていると仮定します。複数のインスタンスを処理する必要がある場合は、次を使用してください。

sshfs_info(){
## A counter, just to know whether a separator should be printed
c=0
## Get the mounts
mount -t fuse.sshfs | grep -oP '^.+?@\S+?:\K.+(?= on /)' |
# Iterate over them
    while read mount
    do
    ## Get the details of this mount. 
    mount | grep -w "$mount" |
        perl -ne '/.+?@(\S+?):(.+)\s+on\s+(.+)\s+type.*user_id=(\d+)/; 
              print "Remote host: $1\nRemote dir: $2\nLocal dir: $3\nLocal user: " . 
              `grep  :1001: /etc/passwd | cut -d: -f1` '
    printf "Elapsed time: %s\n" "$(ps -p $(pgrep -f "$mount") o etime=)"
    ## Increment the counter
    let c++;
    ## Separate the entries if more than one mount was found
    [[ $c > 0 ]] && echo "---"

    done
}

出力は次のとおりです。

$ 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
---

上記の例に示すように、スペースを含むディレクトリ名も処理できます。

最後に、100%移植性がないことに注意してください。 GNUツールセット(Linuxディストリビューションなど)を持つすべてのシステムで動作する必要がありますが、GNU grep関連の機能を使用するため、GNU以外のシステムでは機能しません。

おすすめ記事