My solution is to use the Unix "expect" domain-specific language to trick SSH into communicating with a virtual terminal, and then programming the input that it receives until the connection is established. The script below runs on OS X and Linux and allows you to supply a password on the command line. I implemented this script to terminate with an interactive prompt, but you could instead close the connection after the remote command executes.
#!/usr/bin/expect -f # # syntax: sshnopassword password host command set password [lindex $argv 0] set host [lindex $argv 1] set command [lindex $argv 2] spawn ssh $host $command expect { # Agree to modify known_hosts if prompted "(yes/no)?" { send -- "yes\r" exp_continue } # Enter the password "*?assword:*" { send -- "$password\r" } } interact
Morgan McGuire is a professor of Computer Science at Williams College and a professional game developer. He is the author of The Graphics Codex, an essential reference for computer graphics that runs on iPhone, iPad, and iPod Touch.