Summary: I'm looking to set-up a bash script to transfer a single file from a Synology DS to a dd-wrt router (Netgear WNR3500Lv1).
The problem: router only allows me to write in the /tmp
folder, which is erased after reboot. Instead of checking if the router rebooted, I would like to just push the file ("hosts") to it automatically every morning.
The solution that did not work: Similar question was asked before (Automate scp file transfer using a shell script), however none of the answers work for me. The shell in Synology DS does not have required commands: expect
, spawn
, interact
or sshpass
. It does have rsync
, but the router does not have it.
The solution with SSH keys does not work for me either, because I can't write anything permanently to the router -> so after reboot the setup is gone.
Question: is there a way to add the required commands to DS shell? Or perhaps a simpler way to accomplish this - so that it can happen automatically without prompting for password?
It looks like this is not possible with the limited linux bash of Synology DSM. However, it turns out scp
can be automated using Python.
I have combined code from this question: How to scp in python?, with the script from this thread: https://unix.stackexchange.com/questions/276336/automatically-enter-ssh-password-without-using-ssh-key-expect-sshpass-or-p, and then automated it using this guide: https://forum.synology.com/enu/viewtopic.php?t=141745
As a result, I have the following Python script:
import os
def run():
pid, fd = os.forkpty()
if pid==0:
os.system('scp "%s" "%s:%s"' % ('myfile.txt', 'user@domain.com', 'path') )
while True:
data = os.read(fd,1024)
print data
if "password:" in data: # ssh prompt
os.write(fd,"mypassword\n")
elif "100%" in data: # upload done
os.write(fd,"echo hello\n")
os.write(fd,"echo bye\n")
os.write(fd,"exit\n")
os.close(fd)
run()
This does the job, however, it always exits with OSError: [Errno 9] Bad file descriptor
.