PDA

View Full Version : Ninja script



desmogod
24th April 2007, 01:13 PM
Hi guys.
I need a bit of a hand with scripting.
Now I know it's bad to have users as admins, but that is just the way it is in my organisation.
Now one of the things that these users do is switch off ARD when they realise I can use it to administer their machines.
What I would like to be able to achieve is to have a "ninja" background script or cron job to check every 30 seconds or so if the ARD daemon is running. if not, I want it to fire it up.
I'm pretty new to mac admin, so please go easy on me
Any help would be appreciated.

MacDave
25th April 2007, 01:27 AM
http://docs.info.apple.com/article.html?artnum=108030 <-this is how to fire up ARD in the CLI.

Your basic shell script would be: if ARD's process is running, then do nothing, else run kickstart.

Conversely, if ARD's process is not running, then run kickstart. Either is fine.

In cron, you can run the script by the minute:

*/1 * * * * <user> /path/to/script.sh

If you look at some of my shell scripts I've posted in the Terminal and UNIX forum, you'll see the bash syntax for conditional statements. Keep in mind also that when creating cronjobs in /etc/crontab on Tiger, there is no path set at the top of crontab so in your shell script, all paths to commands (such as "ps") must be complete or alternatively, you can put a path in crontab similar to the way it used to be in Panther.

http://macdave.com/chronicles/index.php <some of my shell scripts are here too, probably with all the syntax you need.

I'm just waking up now and still pretty fog-headed. If you need further assistance, just let me (us) know. Several of us could write the shell script for you, but then that's not as much fun, is it ? :)

Here's a tip:

[Isis:~] root# ps auxc | grep -c ARDAgent
1

That says that there is currently one ARDAgent process running on my Xserve.

Dave

MacDave
26th April 2007, 08:27 AM
The kickstart command itself may need modification depending on one's personal needs:

********************************************

#!/bin/bash

ARD=`ps auxc | grep -c ARDAgent`

if [ $ARD -eq 0 ]; then

cd /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources

./kickstart -activate -configure -access -on -users admin -privs -all -restart -agent -menu

fi

********************************************

BTW, this script needs to be run as root and will work on Tiger -only-! To work on Panther, slight modifications need to be made as the path to Kickstart is different.

Dave

desmogod
26th April 2007, 03:57 PM
so edit it, save it as ninja.sh, chmod +x it, then type ./ ninja.sh to run?

thorevenge
26th April 2007, 04:00 PM
No you would need to run this as a cron job as part of your crontab.

The only way I have played with cron jobs is through Cronnix - Dave should be able to help you more with being able to program the script to run it as a cron job.

MacDave
27th April 2007, 12:55 PM
Please d/l the script from here:

http://macdave.com/mtau/ninja.sh.zip

Expand the zip file and open ninja.sh in TextEdit just to see what's written there. This is just an act of sanity. Though it should be needless to say, NEVER mess with shell scripts unless you understand what they do!

The only differences you'll see are full paths to "ps" and "grep." This will allow the shell script to run in Tiger's cron without a global path (as Tiger's cron is deprecated.) Also, by using a script I''ve already tested and know to work, it eliminates the possibility of there being extra spaces at the end of lines from copy/pasting which can sometimes cause shell scripts to fail.

Typically, shell scripts and third party CLI stuff is put in /usr/local/bin which does not exist on a default install of OS X. Nor is there a path to it (obviously) by default. We're going to cheat here a bit and put ninja.sh in /usr/bin. Once done, and made executable, simply typing "ninja.sh" (without the quotes) will run the script. Remember, this script needs to be run as ROOT.

First, make sure that ninja.sh is on your Desktop.

Type the following in Terminal:

sudo cp ~/Desktop/ninja.sh /usr/bin

Enter your password at the prompt. You won't see the characters as you type them. This is normal. By doing this, you've just copied the ninja.sh script to your /usr/bin.

Now type the following in Terminal:

sudo chmod 755 /usr/bin/ninja.sh

Enter your password again (if necessary.) You've just made the script executable. Restart Terminal. When ARD is NOT running in the sharnig pane, by typing "sudo ninja.sh" (without quotes), you should see the following:

[c-24-7-70-83:/usr/local/bin] root# ninja.sh
Starting...
Created preference to start ARD after reboot.
Started ARD Agent.
Restarted Menu Extra (System UI Server).
Done.

If ARD is already running and configured the same way the ninja script sets it up, nothing should happen if you type "sudo ninja.sh" again like so;

[c-24-7-70-83:/usr/local/bin] root# ninja.sh
[c-24-7-70-83:/usr/local/bin] root#

Now that you've activated ARD in the sharing pane, check its access priveledges. You'll note that I configured the script to enable everything. Look here if you wish ARD configured differently:

http://docs.info.apple.com/article.html?artnum=108030

Right, now for the cronjob. You'll need to edit /etc/crontab as root and paste this ONE line at the bottom:

*/1 * * * * root /usr/bin/ninja.sh &> /dev/null

That's all. It will run the script by the minute. If you don't know how to edit /etc/crontab, please refer here to read about Pico, a simple CLI text editor:

http://forums.mactalk.com.au/showthread.php?t=5715&highlight=pico

Good luck and let us know how it goes. =)

Dave

The Fluffy Duck
27th April 2007, 06:37 PM
Is their a script to turn me into a god? I wish I knew the power of unix and scripting any good tutes?

MacDave
27th April 2007, 07:17 PM
Is their a script to turn me into a god? I wish I knew the power of unix and scripting any good tutes?

Before the scripting, one needs to be familiar with the basic, day to day (if you will) shell commands that are bundled with UNIX and UNIX-Like operating systems. UNIX has been around over 40 years. It's one of the best documented topics on the web!

You could start with "UNIX for Dummies" or a similar book, either online or in print. I have a few UNIX manuals lying about which unfortunately, I can't access readily as my G5 is in the shop atm...

Virtually every shell command has a "man" page. That is , if you want to learn about "ls," just type in Terminal:

man ls

Personally, I picked up UNIX largely by sitting in a few IRC channels and just listening and quietly taking notes. I had the good fortune too of having a few mates who would answer my questions at the time. Macosxhints.com has a UNIX section. if you register there for free, you can configure your account to just show entries marked as "UNIX."

One way or the other, some of the commands you should start with are:

ls, cp, mv, rm, chmod, chown, ps, pwd, cd, grep (a very important one) and learn at least one CLI text editor. Pico/nano is the easiest IMHO.

You'll see soon enough that many commands have names which give their function away: cd = change directory, ls = list, etc. Most of these commands have options (sometimes referred to as "flags.")

Like now, open a Terminal window and just type: ls

Now, type: ls -l

Now type: ls -a

Now type: ls -la

Now tyoe: ls -lah

And so on....

Do be careful with the "rm" command. Think of it as "remove." It has the power to delete your entire home directory with one command executed as user and can delete your entire HD if executed as root!

Oh yeah, there is one other way to start learning UNIX. In the MTAU Terminal, UNIX and Command line forum. =) Purana wrote up a few beginner's tutorials for a few commands a while back. Look for them.

Good luck. =)

Dave

The Fluffy Duck
27th April 2007, 08:25 PM
thx dude :)

Currawong
27th April 2007, 10:25 PM
Unix for Dummies, books 1 and 2 are fantastic for kick-starting terminal use and scripting.

desmogod
1st May 2007, 06:56 PM
Mate, it works flawlessly, thank you :)