Automate a list of Terminal commands?

I have a set of commands that I execute very frequently involving scp'ing and ssh'ing to and from a linux sever from my iMac that I would really like to automate.


I am wondering if there is a way I could just save the list of commands I'd like into a file or something, and then execute that file (instead of each command step-by-step like I do currently).


Any help is appreciated!


Thanks!

Posted on Apr 17, 2011 6:27 PM

Reply
4 replies

Apr 17, 2011 7:54 PM in response to Qcom

You can use #!/bin/sh


The trick is to setup public keys so that you can ssh and scp without a password. Make sure to give your keys a passphrase (most online instructions say to omit this). The first time you need to enter your passphrase, you will be able to store it in your keychain. Now you've got the best of both worlds - ssh without a password but your password is secure and encrypted in case you ever lose control of your private key.

Apr 18, 2011 9:33 AM in response to Qcom

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.

Apr 19, 2011 3:23 PM in response to Qcom

FYI: You can embed short scripts in an AppleScript wrapper (or invoke Bash scripts) using AppleScript's Do Shell Script command, then conveniently call them using Script Menu (/System/Library/CoreServices/Menu\ Extras/Script\ Menu.menu).


I have AppleScript wrappers around often invoked scripts which create SSH tunnels to remote boxes for Screen Sharing, Remote Admin, etc. Can also be used for non-GUI applications, like simply running scripts on the remote box. All this is conveniently available from a menulet.


I use public key authorization because it's far more secure than password challenge authentication systems, not to mention much more convenient. I disable password challenge authentication on all my remote boxes for security reasons.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automate a list of Terminal commands?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.