Hi,
Yes, what you want to do is definitely possible, but the “best” solution depends a bit on how the 2FA is implemented on the server.
In general, constantly polling the server every 5 minutes with a fresh login is usually not ideal, especially with OTP-based authentication.
A few thoughts:
You could also look into:
If your 2FA solution expires immediately after login and requires a new OTP for every new SSH connection, then fully unattended automation may intentionally be blocked by the security policy.
In that case, one workaround is:
Another option is to ask the server admin whether a service account/API token/key-based exception exists specifically for automated transfers.
Also, instead of “copy then delete,” be careful with data integrity. Usually it’s safer to:
Otherwise partial transfers can become painful.
Overall, your idea is reasonable — I’d just recommend moving away from the infinite loop approach and toward scheduled sync tools or persistent SSH sessions.
Yes, what you want to do is definitely possible, but the “best” solution depends a bit on how the 2FA is implemented on the server.
In general, constantly polling the server every 5 minutes with a fresh login is usually not ideal, especially with OTP-based authentication.
A few thoughts:
- If the server allows SSH key authentication together with 2FA, you may be able to establish a long-lived authenticated session and reuse it.
- Another common approach is using
rsyncorscpwith SSH keys and a scheduled task/cron job instead of an infinite loop. - Some systems also support SFTP automation tools that can maintain sessions or cache authentication tokens temporarily.
You could also look into:
-
ssh-agentfor keeping keys loaded - SSH multiplexing (
ControlMaster) to reuse a single authenticated connection - file synchronization tools like
lsyncd,syncthing, orrclone
If your 2FA solution expires immediately after login and requires a new OTP for every new SSH connection, then fully unattended automation may intentionally be blocked by the security policy.
In that case, one workaround is:
- Manually authenticate once
- Keep the SSH session alive
- Reuse that existing tunnel/session for transfers during the day
Another option is to ask the server admin whether a service account/API token/key-based exception exists specifically for automated transfers.
Also, instead of “copy then delete,” be careful with data integrity. Usually it’s safer to:
- copy
- verify transfer success
- then remove the original file
Otherwise partial transfers can become painful.
Overall, your idea is reasonable — I’d just recommend moving away from the infinite loop approach and toward scheduled sync tools or persistent SSH sessions.