Linuxでリモートファイルシステムを監視する方法は?

Linuxでリモートファイルシステムを監視する方法は?

私のビジネス目的は、Linuxでリモートファイルシステムを監視し、新しいファイルがある場合はSFTPを別のシステムに転送して削除することです。

ただし、制限は、リモートコンピュータにライブラリをインストールできないことです。したがって、リモートシステムの間隔SSHコマンドポーリングを実装しようとしています。

私の質問:

  1. 間隔ポーリングを実装できますか?それとももっと良い考えがありますか?

  2. リモートコンピュータを監視するには、どのSSHコマンドを使用する必要がありますか?

ベストアンサー1

簡単で怠惰な方法:

crontabローカルシステムの一般ユーザーの場合(15分ごと):

*/15 * * * * /bin/bash /path/to/script.sh

パスワード:

#!/bin/bash

source ~/.bashrc

ssh-add /home/me/.ssh/id_rsa
ssh user@remote-server printf '%s\n' '/path/to/new_files/*' > ~/.$$_remote-files
if ! cmp ~/.$$_remote-files ~/.remote-files &>/dev/null; then
    echo 'new file(s) or dir(s) detected !'
    # ssh user@remote-server rm -rf '/path/to/new_files/*'
    mv ~/.$$_remote-files ~/.remote-files
fi

自動SSHログインを実装するには、パスワードなしでSSHパスワードフレーズを生成する必要があります。

$ ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/home/me/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/me/.ssh/id_rsa
Your public key has been saved in /home/me/.ssh/id_rsa.pub
The key fingerprint is:
123456789ABCDEF

おすすめ記事