Connect and run commands over SSH using script
You can use ‘expect’ to enter the SSH password when prompted. Changes to your script are bolded.


#!/usr/bin/expect -f
spawn ssh root@server
expect "*: "
send "xxxxxxxxxxxxxx
expect "$ "
send "… commands


spawn starts the command as normal, which in this case is SSH
expect waits until the matching text is shown on screen
send enters the text given, which can be a password or command to be run
expect waits for the shell to be shown (this should be changed to match against your shell $PS1)

Save this file, add the executable bit (chmod +x /path/to/file) and open to run the commands.



However, instead of this, you should be using SSH keys to connect to the server without entering a password, and the commands you wish to execute over SSH should be in a script on the server which can be run. Set up SSH keys, then you can run a remote script with ssh -t root@server "/path/to/script".