2

My business purpose is to monitor the remote file system on Linux, and if there are any new files, SFTP them to another machine and delete them.

However, the limitation is that I cannot install any libraries on the remote machine. So, I am considering implementing interval SSH command polling to the remote machine.

My questions:

  1. Is interval polling implementable? Or do you have any better ideas?

  2. What kind of SSH command should I use to monitor the remote machine?

Z0OM
  • 3,149

3 Answers3

1

The simple and lazy way:

crontab with your regular user on local system (every 15 min):

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

The code:

#!/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

To implement auto ssh login, you have to generate a ssh-passphrase without password:

$ 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
Pablo A
  • 2,712
1

entr

You can also use entr (man, homepage): run arbitrary commands when files change.

ls pathToRead | entr -r scp pathToRead remoteDestination

Check also other ways to copy.

Pablo A
  • 2,712
0
  1. The better way would be to install a scanner (based on inotify) on the file server. Once the new file appears - the scanner will send some kind of message to anyone who is interested (be it a UDP broadcast, email, or something else). This would require some programming and such application must be installed and running on the file server.
    If you are unable to do it - the polling is the only choice.

  2. Not ssh command, but sftp's command. And that command is ls. It would also require some programming, but could be done completely in shell.

White Owl
  • 5,129
  • 1
    Thanks for the answer. I think polling is the sole choice for me, could you specify how to implement interval monitoring polling? I have googled it but I have got no clue to do it. – user275616 Oct 17 '22 at 02:14