I'm working on a script that needs to copy some files from a local machine to a directory on a remote server. The problem I'm running into is that the directory (/etc/init.d) is owned by root so I get permission exceptions if I try to copy files into it. That means I can't use scp without logging in as root.
The closest solution I have found so far is this answer: https://askubuntu.com/a/872537/798391 . Unfortunately, the answer as given doesn't quite work and none of the suggestion given in the comments seem to fix it. If I run
cat myscript.sh | ssh foo@myserver "sudo tee -a /etc/init.d/myscript.sh"
I get the error
sudo: no tty present and no askpass program specified
One of the comments suggested adding -t to the ssh command
cat myscript.sh | ssh -t foo@myserver "sudo tee -a /etc/init.d/myscript.sh"
but that resulted in the error
Pseudo-terminal will not be allocated because stdin is not a terminal.
Another suggested option was to use the -S argument of sudo
cat myscript.sh | ssh foo@myserver "sudo -S tee -a /etc/init.d/myscript.sh"
That at least prompts for the password, but it times out and asks again before the password can be entered completely.
At this point I'm out of ideas. Is there some way to get this command to work? Is there a better alternative solution for copying files to a protected remote location?