Posts RSS Comments RSS 48 Posts and 155 Comments till now

Archive for the 'System' Category

Using Terminal to control Spotlight

Spotlight is the Macintosh technology that lets you search for things on your Mac. When you search for something like “dog” it not only finds that letter combination in the names of files but searchs the contents of the files for that term as well.

Occasionally Spotlight goes a little nuts and does things like continuously indexing your entire machine, which can slow things down. A good fix for this problem is to simply delete the Spotlight index and let it recreate itself. This can also fix problems where things aren’t showing up in a search that you know are there. The tool we use to do this is a command line tool called mdutil

To delete a Spotlight index enter the following in Terminal. Spotlight will automatically begin to rebuild the index.

sudo mdutil -E volume_name

If you only have one hard drive or partition on your machine or you want to rebuild the index on the drive you’re booted from use this syntax:

sudo mdutil -E /

If you want to rebuild the index on a different drive then your boot drive use this syntax. We’ll assume the drive is name “Extra Stuff”:

sudo mdutil-E "/Volumes/Extra Stuff"

If, for some reason, you find you need to completely turn off Spotlight use this command.

sudo mdutil -i on/off volume_name

To turn off Spotlight indexing on your boot volume use this syntax:

sudo mdutil -i off /

Pinging a server using AppleScript

I’ve recently had a machine that suddenly seems to lose it’s network connection. I can’t figure out why but it always comes back after a reboot. The big problem with this is it’s my backup server and having that off the network is not good. So, I came up with an AppleScript that runs each hour to see if we’re still talking to the world. If not then I reboot the machine.

Here’s the basic script:


try
set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"
on error
delay 60
try
set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"
on error
do shell script "/sbin/shutdown -r now"
end try
end try

Breaking down the script

set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"

This executes the command line version of ping to see if we’re on the network. The “-o” means quit pinging as soon as we get a reply. Don’t want to flood Google with pings! The “-c 5” says only send 5 pings at the most.

If there is no response for what ever reason it throws an error. So the “on error” part of the script catches that and tells the script to delay itself for 60 seconds and then continue.

After 60 seconds we try again. If we get an error again the we tell the machine to restart itself using “shutdown -r”.

I run this script as a System cron job, running once an hour. By running as system I don’t have to include any passwords and if it needs to shutdown most likely no active processes can get in the way. I use Cronnix to set up my cron jobs. Saving the job as “ping.scpt” the cron job would look like this:

/usr/bin/osascript /path/to/ping.scpt

Setting the time server for multiple machines

On a single Mac it’s a simple thing to set which time server your machine gets its time from. Simple go to System Preferences, click on “Date & Time” and enter the address for your preferred time server in the “set date & time automatically” box.

However, if you want to blow that setting out to multiple machines what do you do? The file that holds this setting is located in the /etc directory and is named ntp.conf.

Copy that file to the /etc directory on all your machines using something like Apple Remote Desktop and you’re done!

Just make sure the permissions match so the machine can read it correctly. The permissions should look like this:

-rw-r--r-- 1 root wheel 47 Apr 25 09:35 ntp.conf

Making Symantec (Norton) AntiVirus update when logged out

Norton AntiVirus (now Symantec AntiVirus) is not, out of the box, able to automatically update it’s virus definitions or it’s application when running on a Macintosh. Symantec has posted two articles that, when put together, let you have all of your Macs update in the dead of night while they are logged out. I’ve been using this technique on all of the Macs in my computer labs for over a year with great success.

The two articles are Running LiveUpdate using UNIX commands and Scheduling LiveUpdate for all users using UNIX commands.

Putting the information from these two articles together you can set up your Macs to auto-update using this procedure.

We’re going to set up a root-level cron job to run a command to update the things we need. I like to have it run around 3 AM. If you don’t know how to do it at the command line use Cronnix and edit the System cron job.

First we’ll cover the basic flag options you have.

-liveupdatequiet

If you don’t want that annoying LiveUpdate window to pop up (especially since you’re logged out) you need the -liveupdatequiet YES flag. If you do want it to show then leave this flag out.

The default action is to show it. However, if you are running this when the machine is logged out you could come in the next morning to a lot of machines with the LiveUpdate window showing behind the login window.

-liveupdateautoquit

If you want LiveUpdate to automatically quit when it’s done use the -liveupdateautoquit YES flag.

I’m not sure why you WOULDN’T want it to quit when it’s done.

Update options

To tell LiveUpdate what to update you have to choose from these options with the -update flag.

LUal = All Symantec products
LUdf = Norton AntiVirus virus definition updates
LUlu = LiveUpdate
LUna = Norton AntiVirus

Putting it all together

So, putting this all together, an example command to silently update the virus definitions would look like this. Watch out for line wraps. This should all be on one line:

/Applications/Symantec\ Solutions/LiveUpdate.app/Contents/MacOS/LiveUpdate -update LUdf
-liveupdatequiet YES -liveupdateautoquit YES

Things to watch out for

One “gotcha” to look out for. Symantec changed the name of the folder that holds LiveUpdate between versions 9 and 10. So, the above example will work in version 10. To have it work in version 9 use this path:

/Applications/Norton\ Solutions/LiveUpdate.app/Contents/MacOS/LiveUpdate

A second thing to watch out for is when you use the LUal option. If the actual AntiVirus or LiveUpdate applications get updated you’ll need to restart the machine. Norton will give your users an error message when they log in if the machine hasn’t been rebooted since the update.

For that reason I only update the virus definitions this way. When the actual applications need updating I run the command using the LUal flag using Apple Remote Desktop’s “Run UNIX command” function on all the machines at once and then restart them.

Using System Profiler in Terminal

There are times when you may need to get information about your system via the command line. Perhaps for a shell script or you’re already working in Terminal and it’s just quicker then opening the GUI version of System Profiler.

The command line version of System Profiler is, appropriately enough, “system_profiler”.

You can type man system_profiler to get a list of all the many things it can do. Here are a few examples.

You can get three different levels of reports.

  • system_profiler -detailLevel mini gives you a report without personal information
  • system_profiler -detailLevel basic gives you hardware and network information only
  • system_profiler -detailLevel full gives you everything

system_profiler -xml will export everything to an XML file for use in a web page or database. You can combine this command with others to get only certain information

For example, to get only the hardware and network information on a certain machine and write it to an XML file you can use this:

system_profiler -xml -detaiLevel basic > /Users/myaccount/Desktop/report.xml

Enter system_profiler -listDataTypes to get a list of the different areas that system_profiler gathers data on.

Those types are:

SPHardwareDataType
SPNetworkDataType
SPSoftwareDataType
SPParallelATADataType
SPAudioDataType
SPBluetoothDataType
SPDiagnosticsDataType
SPDiscBurningDataType
SPFibreChannelDataType
SPFireWireDataType
SPDisplaysDataType
SPMemoryDataType
SPPCCardDataType
SPPCIDataType
SPParallelSCSIDataType
SPPowerDataType
SPPrintersDataType
SPSerialATADataType
SPUSBDataType
SPAirPortDataType
SPFirewallDataType
SPNetworkLocationDataType
SPModemDataType
SPNetworkVolumeDataType
SPApplicationsDataType
SPExtensionsDataType
SPFontsDataType
SPFrameworksDataType
SPLogsDataType
SPPrefPaneDataType
SPStartupItemDataType

So, to get information on the type of hardware a machine has enter system_profiler SPHardwareDataType into Terminal and you’ll get something like this:

Hardware Overview:

Machine Name: Power Mac G5
Machine Model: PowerMac7,3
CPU Type: PowerPC G5 (3.0)
Number Of CPUs: 2
CPU Speed: 2.5 GHz
L2 Cache (per CPU): 512 KB
Memory: 2 GB
Bus Speed: 1.25 GHz
Boot ROM Version: 5.1.8f7
Serial Number: XXXXXXXXXXX

system_profiler SPPrintersDataType will give you all the information on the currently installed printers.

system_profiler SPFirewallDataType will tell you if the Firewall is on or off and if it’s on what rules are in effect. If the firewall is off you’ll get no return on the command.

Read the Login accounting file

This hint is for people who don’t have Apple Remote Desktop 3.0 and may need to know whose been logging into a machine. This can come in particular use if you run a public computing lab and need to know whose been logging into your computers at 2 AM or need to know if a certain student was using a computer at a certain time.

The login stats are kept in /var/log/ in a file named wtmp

Go to Terminal to run the commands below. You either have to be logged in as root or run them as sudo.

To find out how many times people have logged in during the current month:

ac -p

To find out when people have logged in during the current month:

last

To find out this information for previous months you must unzip the log files in /var/log

Example:

gunzip /var/log/wtmp.0.gz will unzip the previous months log files

After you have unzipped you can run the following:

To find out when people logged in during that month:

last -f "path to unzipped file"

Example:

last -f /var/log/wtmp.0

To find out how many times people logged in that month:

ac -p -w "path to file name"

Example:

ac -p -w /var/log/wtmp.0

Setting the startup disk using Terminal

While it’s quite easy to change which disk your machine starts up from using System Preferences there may be times when you need/want to do it either at the command line or within a script.

The command for setting the startup disk using Terminal is “bless“. To get the full story on “bless” open up Terminal and type “man bless” (no quotes).

To change the startup disk type the following in Terminal:

sudo bless -mount /Volumes/"name of your startup disk" -setBoot

So, if the desired disk was named “TestDisk” you would type this:

sudo bless -mount /Volumes/TestDisk -setBoot

If your disk name has spaces in it you’ll need to put quotes around the path to the disk, like this:

sudo bless -mount "/Volumes/My Disk" -setBoot

You can incorporate this into a UNIX shell script to reboot your machine to another disk at a certain time. Perhaps you want to reboot to another disk every Friday to run a disk utility on it, or to image it.


#!/bin/bash
bless -mount /Volumes/TestDisk -setBoot
shutdown -r now

Breaking down this script the first line sets the disk your Mac will boot from. The second line tells it to shutdown and restart immediately. If you have an Intel Mac you can add “–nextonly” at the end of the “bless” line. That will boot the machine to that volume first and then boot back to the original volume on subsequent reboots without having to reset the startup disk.

This command also comes in handy if you’re booting back and forth between volumes to test things. For example, you have a partition with 10.3 and another with 10.4 on it and you want to test some software in 10.3. You can wrap all of this up in an AppleScript and either save it on your desktop as an application or save it as a script and put it in your Script menu.

Paste this code in Script Editor and run it. Make sure you change the disk name to your disk. What for the AppleScript line breaks in the code. Any line that ends in “¬” means the line below is part of the same line. Pasting it in as it is on the web page will still work however.

[codesyntax lang=”applescript” lines=”no”]
do shell script “bless -mount \”/Volumes/Drive Name\” ¬
-setBoot with administrator privileges
do shell script “shutdown -r now” with administrator privileges
[/codesyntax]

Then simply click on the application or select if from the Script menu, enter your admin name and password and it will select the disk and reboot for you.

If you want to choose between several disks you can add in a dialog box to let you choose the correct disk.

[codesyntax lang=”applescript” lines=”no”]
display dialog “Select a startup disk” buttons ¬
{“name of disc 1”, “name of disc 2”]
set bootVol to the button returned of the result as text
do shell script “bless -mount \”/Volumes/” ¬
& bootVol & “\” -setBoot” with administrator privileges
do shell script “shutdown -r now” with administrator privileges
[/codesyntax]

Identifying a DHCP server

Here are three quick ways to identify which DHCP server your machine is getting it’s IP address from. This can come in handy when trying to track down rogue DHCP servers that pop up on a network.

In Tiger:

  • Open System Profiler
  • Click on “Network”
  • Select which service you want to see
  • It is listed under “DHCP Server Responses:” next to “Server Identifier”

In Panther and below:

  • In Terminal type “ipconfig getoption en0 server_identifier”
  • Change “en0” to “en1″ if using wireless

You can also wrap it in an AppleScript. This should work on most version of OS X.

[codesyntax lang=”applescript” lines=”no”]
do shell script “ipconfig getoption en0 server_identifier”
set theIP to the result
display dialog “Your DHCP server IP is: ” & theIP
[/codesyntax]

Save it as an application. Great for getting this information from the Terminal-challenged user.

Clearing the MCX cache

When you manage the preferences on your workstations via Workgroup Manager the settings are stored on the local clients in the MCX cache. This can be manually cleaned out in NetInfo but there is a much easier way.

  • Open Terminal
  • Run the following command. Use “sudo” if you’re not logged in as root:
    /System/Library/CoreServices/mcxd.app/Contents/Resources/MCXCacher
  • Enter your admin password

The command needs to be run on each machine seperately.

If you are using Apple Remote Desktop 3.x you can enter this withou the “sudo” in the “Run UNIX” box and run it as root on a large number of machines at once..

« Prev