You can follow this approach :
- Connect to remote machine using Expect Script. If your machine doesn't support expect you can download the same. Writing Expect script is very easy (google to get help on this)
- Put all the action which needs to be performed on remote server in a shell script.
- Invoke remote shell script from expect script once login is successful.