Bash: controlling SSH
The proper way to do this without storing passwords in plaintext on your machine is with ssh. First run:
ssh-keygen
This will generate a new SSH key in ~/.ssh/id_rsa.pub
. After that simply run:
ssh-copy-id [email protected]
If you're on OS X or another machine that does not have "ssh-copy-id" there are one-line alternatives such as this one:
cat ~/.ssh/id_rsa.pub | ssh user@machine "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys"
Ultimately you just need to append the contents of ~/.ssh/id_rsa.pub
on your local machine to ~/.ssh/authorized_keys
on the remote server. How you do that is up to you, the above are just quick shortcuts to do that.
Many tools to go great lengths to prevent what you are doing. I recommend using ssh public keys to solve this problem instead of passwords.
The big alternative is to write your own modified ssh client based on the open source so as to take control of the password management.
Oh, well, I forgot. You can probably outsmart this with a pty, since then /dev/tty will be what you control. expect might help you with this.
Expect is the usual tool for automating interactive sessions.