-1

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?

Michal J Figurski
  • 1,262
  • 1
  • 11
  • 18
  • The question is how to copy files or how to automate that copy? – Poshi Apr 25 '19 at 14:20
  • @Poshi - as in the title: how to _automate_ copy – Michal J Figurski Apr 25 '19 at 15:32
  • Can you set up cron jobs? I think they are your best bet. Just launch a copy command (or a script that contains your copy command of choice) from your cron with the frequency you need. `cron` will take care of starting it automatically, it is its job :-) – Poshi Apr 25 '19 at 17:46
  • This is XY question. The real question is how to automatically login to ssh without using expect, spawn interact or sshpass. Duplicate of https://stackoverflow.com/questions/1340366/how-to-make-ssh-receive-the-password-from-stdin ? – KamilCuk Apr 26 '19 at 05:38
  • Or then the real question is "how can I install stuff on a Synology" which isn't a programming question at all, and easily googled. – tripleee Apr 26 '19 at 06:55
  • @tripleee - believe me - I've used google. No direct answers on how to install such stuff on Synology. I'm not a linux guru, and this just not doable for me. Another option that I see recommended in some places is to use python. I have to look into that... – Michal J Figurski Apr 26 '19 at 18:24
  • @KamilCuk - unless I am missing something, this is not really duplicate, because the issue is not to ssh to the router, but copy a file. – Michal J Figurski Apr 26 '19 at 18:27
  • You can copy a file by doing `cat file | ssh user@server sh -c 'cat > outfile'`. Yes, this is about ssh to the router, as you are using `scp`, which is a protocol simply build upon `ssh`. If you solve the problem of automation connecting using ssh, you can copy your files, or even (looking at `sshfs`) use rsync for it. – KamilCuk Apr 26 '19 at 19:24
  • @KamilCuk Unfortunately this is no better than using plain `scp`, because it asks me for password... – Michal J Figurski Apr 26 '19 at 19:54
  • So the question I linked reads the passwords from stdin... you can even write a simple script that does `echo password` and pass this script to `SSH_ASKPASS=this_script`..... – KamilCuk Apr 26 '19 at 20:16

2 Answers2

0

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.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Michal J Figurski
  • 1,262
  • 1
  • 11
  • 18
0

The code below works with python 3 :

import os
def run():
    print('Start script')
    pid, fd = os.forkpty()
    if pid==0:
        os.system('scp ...')
    while True:
        data = os.read(fd,1024)
        print (data)
        if "password:".encode() in data:
            os.write(fd,"YOUR-PASSWORD\n".encode())
run()
Dady
  • 71
  • 8