Running stuff locally uses a shell script, and you have several good answers toward that end already posted here.
Given you want to run this stuff remotely, also recognize that the ssh command accepts a command (which can be a script) as a parameter and will invoke that (command, script, whatever) on the remote (target) host. You can place the script on the remote target, and can then easily invoke it.
Note: You should not and often cannot embed passwords with ssh within scripts. You will want to create and use a public key for that; to copy a public key over to the target system, and add the reference to the new public key into the ssh authorization file on the target host. This is the no-password login.
The man ssh man page has syntax details for the ssh command syntax, including the available command parameter.
Other options for remote invocations and remote operations and distributed management can include tools such as Puppet (remote commands) and Munki (tool and kit deployments) and Apple Remote Desktop (ARD), and here is a list of some of the other tools available for distributed command submissions, and remote processing requirements. (Some of the GUI terminal tools around can submit one command across multiple terminals, too.)
For managing remote sessions from the command line, tools such as screen (built into Mac OS X) and tmux (add-on) can be useful. If you're doing C development work, tools such as distcc are very useful.
And if you're entirely new to shell scripting, Apple has a very introductory document with their Shell Scripting Primer and there's the Advanced Bash Scripting guide and various bash books and cookbooks are available on the topic. Oh, and the CommandFu web site is well worth a look, just to learn more about commands of interest.