Is there a way to copy, ssh, and run a command on a remote machine with one password?
There are two ways, varying in their convenience and trade-offs.
One is to transfer the file inline. Do you need input other than the file content? If not, ssh example.com 'cat > the_file ; process the_file' < the_file
will do what you want.
The other is to do something that reuses the SSH connection. ControlMaster
and friends for ssh might help - see ssh_config(1)
for details, or there are various wrappers, or implementations of SSH for various programming languages that help.
Also how to write scripts which need to interact with user to enter passwords?
Read it from the TTY. Generally, the best strategy is to allow ssh to interact directly with the user and not get in the way, because it has a much better tested and validated implementation of interacting in this way.
I do not want to use read as it's not safe.
For interacting with the user, I presume. It isn't substantially different in safety to just letting SSH ask, actually, but it is ideal to just stay out of the way.
I do not want to use passwordless ssh, since my private key file may be compromised, which is even worse.
...and I presume that the SSH agent isn't acceptable either for about the same reason.
except
also stores your password in plain in memory, so it's as bad asread
. Oh, not that bad. Sinceread
is a shell builtin, the variable may be kept in the memory for a longer time if you don't unset it.