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?

2
  • Do you mean to say you need to watch a directory/folder and if there are any new files, transfer and delete them?
    – Sotto Voce
    Commented Oct 17, 2022 at 2:02
  • Yes that is right.
    – user275616
    Commented Oct 17, 2022 at 2:11

3 Answers 3

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
2
  • Thanks a bunch! I guess it is time to go learning bash :D
    – user275616
    Commented Oct 17, 2022 at 3:49
  • There's nothing complicated. If you get stuck, you can come back with a specific question. Commented Oct 17, 2022 at 4:12
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.

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.

1
  • 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
    Commented Oct 17, 2022 at 2:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .