Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use a shell script and Growl to report a router's status UNIX
Growl my Router Alive! is a bash shell script that will ping an IP address (ie a router) and will notify you, using Growl, if the ping is successful. This requires some Terminal basics, both Growl and growlnotify being installed, and a document with a notification icon for Growl. Enjoy.

I wrote this because I have a wireless connection with a D-Link router that is quite annoying because it sometimes drops its wireless network or internet connection. This script will keep pinging my router and sends an alert when the wireless connection is dropped, and when it comes online again.

Here's the code:
#!/bin/sh

#Growl my Router alive!
#2010 by zionthelion73 [at] gmail . com
#use it for free
#redistribute or modify but keep these comments
#not for commercial purposes

iconpath="/path/to/router/icon/file/internet.png"
# path must be absolute or in "./path" form but relative to growlnotify position
# document icon is used, not document content

# Put the IP address of your router here
localip=192.168.1.1

clear
echo 'Router avaiability notification with Growl'

#variable
avaiable=false

com="################"
#comment prefix for logging porpouse

while true;
do
if $avaiable
then
  echo "$com 1) $localip avaiable $com"
  echo "1"
  while ping -c 1 -t 2 $localip
    do
      sleep 5
    done
  growlnotify  -s -I $iconpath -m "$localip is offline"
  avaiable=false
else
  echo "$com 2) $localip not avaiable $com"
  #try to ping the router untill it come back and notify it
  while !(ping -c 1 -t 2 $localip)
  do
   echo "$com trying.... $com"
   sleep 5
  done
  
  echo "$com found $localip $com"
  growlnotify -s -I $iconpath -m "$localip is online"
  avaiable=true
fi

sleep 5

done
Copy this script into a directory as a script named pingrouter.sh or whatever; the name of your shell script must end with a .sh. Make the script executable by typing chmod ugo+x pingrouter.sh. Execute the script with sh ping.sh. Comments and suggestions are always welcomed!

[robg adds: I haven't tested this one.]
  Post a comment  •  Comments (6)  
  • Currently 2.45 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (11 votes cast)
 
[8,425 views] Email Article To a Friend View Printable Version
Fix a bug in snmp output to show data for new Mac mini UNIX
I've searched for a command line tool to integrate in my mrtg page. It should fetch the temperature and fans peed from the system on my Mac mini. Apparently snmpd can do this.

After starting snmpd, I stumbled across /usr/share/snmp/SensorDat.xml. There is an entry there for my Mac mini, but it didn't workL:
    <key>MacMini3,1</key>
    <dict>
        <key>fan_data</key>
I downloaded the net-snmp-source from Apple, and found a test program in this directory:
net_snmp/Build/net-snmp/agent/mibgroup/ucd-snmp/data_access/
read more (329 words)   Post a comment  •  Comments (3)  
  • Currently 2.44 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (9 votes cast)
 
[5,915 views] Email Article To a Friend View Printable Version
Execute small bits from the clipboard in Terminal UNIX
Perl and Bash programmers will recognize the backtick operator (`some_command`) as a way to cause the string enclosed in the backticks operator (some_command) to be executed as a command in the shell.

If you put some executable text on the OS X clipboard, this can also be directly executed with the backtick operator. For example, the shell command ls -l -G -a -F $HOME produces a colorized long listing of the user's home directory. Copy that command to the OS X clipboard.

In Terminal, type pbpaste, and you will get the text of the command. If you instead type `pbpaste`, the shell will execute the command and produce the directory listing.

[robg adds: This tidbit is most useful as part of a larger script, as seen in these examples. If you're in Terminal with some executable Unix command on the clipboard, you could run that command directly by pressing Command-V and Return.]
  Post a comment  •  Comments (10)  
  • Currently 2.25 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (16 votes cast)
 
[8,306 views] Email Article To a Friend View Printable Version
Use Spotlight to speed up grep searches UNIX
The grep -ri command (recursively search for matches, ignoring case) in Terminal is incredibly useful, but incredibly slow. In OS X, we can leverage mdfind to quickly find the files containing our search string, then use grep to find the strings within only those files. Here's a script to do just that.
#!/bin/bash
#
# Spotlight metadata find and grep by Daniel.Hoherd at gmail dot com

## Check for at least two arguments, print usage if else
if [ $# -lt 1 ] ; then
  echo "usage: $0 searchstring [dir or file] [dir2 or file2]"
  exit ;
fi

ss=$1;
shift;
files=$@;

until [ -z "$1" ] ; do
  thisitem=$1
  onlyin="-onlyin '$thisitem' $onlyin"
  shift
done;
eval mdfind -0 $onlyin "$ss" | xargs -0 grep -Hi "$ss"
Leveraging mdfind to eliminate files that do not contain the matching string makes this operation an order of magnitude quicker, cutting it to mere seconds instead of minutes on over 100GB of data in my tests. The downside to this is that Spotlight does not index every directory, so you may be missing some files such as system files.

If you don't want to use this script, the same thing can be accomplished by hand by writing mdfind -0 -onlyin searchdir searchstring | xargs -0 grep -Hi searchstring. I've included the -H and -i options to more closely match the output of mdfind, but you can safely remove them.
  Post a comment  •  Comments (7)  
  • Currently 3.42 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (19 votes cast)
 
[9,014 views] Email Article To a Friend View Printable Version
Use a third-party APC UPS management tool for more control UNIX
My APC UPS beeps whenever the power fails. If this happens at night, the beep is loud enough to wake people. The beep can be disabled by software in Windows, but not in the Mac version of PowerChute Personal Edition. The Mac version provides features that Mac OS X already provides: shutdown timers for any UPS connected via USB.

I stumbled across the open source Unix app Apcupsd which gives me all the features I want from the Windows version of PowerChute: beep settings, battery change dates, and so forth. Furthermore, it can send email notifications for significant events. As someone who runs an old G4 as a home server, this is very nice to know.

After downloading and installing Apcupsd is installed, run this command from Terminal: sudo /sbin/apctest. This will display a simple text-based menu, and it's self-explanatory. If you get an error message that reads Cannot find UPS device..., it could be because:
  1. Only one tool can query the UPS, and the background monitoring process is running, so stop it with sudo /Library/StartupItems/apcupsd/apcupsd stop, then try again.
  2. The software might only work with PowerPC Macs at this point. It works perfectly on my G4, but not on my ICBM (Intel Chip Based Mac).
[robg adds: I haven't tested this app.]
  Post a comment  •  Comments (20)  
  • Currently 4.32 / 5
  You rated: 4 / 5 (22 votes cast)
 
[23,672 views] Email Article To a Friend View Printable Version
Check the status of the online Apple Store via shell script UNIX
Sometimes, like today, I'm particularly interested in the Apple Store's status...as in, when is it back up so that I can order whatever it is I'm waiting to order. While sitting around pressing Command-R this morning, I figured there must be a better way. After a few minutes with curl and GeekTool, I had the solution.

Because I didn't have very much time to implement this trick this morning, I just created a new 'Shell' Geeklet in GeekTool, with the refresh set to every five seconds, that showed the output of this command:

curl -s http://store.apple.com/us | grep backsoon | grep australia

Note that the above is specific both to the US Apple Store, and to the current version of that store. You may have to modify it for other geographies, or for future store site changes. Basically, what the command does is grab the Apple Store page via curl, suppresses the normal output (-s), and then searches for backsoon, which is the name of the GIF image Apple uses for the "We'll be back soon!" graphic.

There are, however, lots of matches for backsoon, so I then grep again for australia, which was the first occurrence of backsoon in the output. This way, I get just one line of output when the store is down. When it's up, there shouldn't be any output.

Testing was real world, too, as I waited for the 5:30am scheduled store opening. About a minute before that, the output from my Geeklet suddenly went blank, so I reloaded the web page, and there was the store. Nifty. For longer-term use, though, my solution leaves much to be desired. So after ordering the iPad, I turned my quick solution into a slightly more robust script that uses Growl to put the alert onscreen. Read on for that version.
read more (290 words)   Post a comment  •  Comments (3)  
  • Currently 3.30 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (10 votes cast)
 
[8,157 views] Email Article To a Friend View Printable Version
Experiment with GIMP's new single window mode UNIX
Are you curious, like me, about the new Single Window Mode (most excellent; see this article at Ars for more details) available in the newest unstable 2.7.x GIMP releases? Well, sadly, the final and stable GIMP 2.8 release won't come out before the end of this year, and there are still no experimental 2.7.x binary releases available for Mac OS X (via X11). One could always try to compile everything from source, but that might be quite complicated and time-consuming.

So, let's look at another, definitely easier way of running GIMP 2.7.x on Mac OS X: not (semi) natively through X11, but through virtualization. First of all, we need a virtual machine with the latest Ubuntu 9.10 (Karmic), or even 10.04 (Lucid, in Alpha at this time) installed: you can create a 32- or 64-bit Ubuntu VM in VMware Fusion, Parallels Desktop or Sun VirtualBox (which is free). On the Mac OS X integration front, Fusion currently offers the best tools for Linux VMs (including an integrated Unix/OS X windows mode (Unity) that really works).

After having set up the Ubuntu VM (also with the latest updates, etc.), we can begin with the essence of this hint.
read more (141 words)   Post a comment  •  Comments (15)  
  • Currently 2.06 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (18 votes cast)
 
[12,465 views] Email Article To a Friend View Printable Version
Clean .DS_Store, .Trash, and ._resources files prior to copy UNIX
Frequently we need to clean a directory before zipping it or copying it to an external USB drive to be used by Windows or Linux users.

Apple Finder has the custom of populating directories with those unavoidable .DS_Store files, volumes with .Trashes, and some files (especially pictures) with ._resources. The following interactive script will safely remove these files prior to copying.
#!/bin/sh
# bash script to clean (delete) Finder .DS_Store, .Trashes and ._resources
# Use cleandsstores.sh 
# juanfc 2010-03-06

if [ $# != 1 ]
then
  echo "ERROR:  use\n\t`basename $0` dirtoclean"
  exit 1
fi

res=`find "$@" \( -name ".DS_Store" -or -name ".Trashes" -or -name "._*" \) -print`


if [[ -z $res ]]; then
  echo "nothing to delete"
  exit 0
else
  echo "Going to delete:"
  echo $res
fi
read -p "Ok (yYsS1)?" ok

case $ok in
  [yYsS1] )
    find "$@" \( -name ".DS_Store" -or -name ".Trashes" -or -name "._*" \) -exec rm -rf "{}" \; -prune ;;
  * )
    echo "aborted."
esac

exit 0
[robg adds: I haven't tested this one. This older hint contained a one-liner to clean up just the .DS_Store files, and a commenter there notes the existence (beginning in 10.5) of the dot_clean command to simplify the task.]
  Post a comment  •  Comments (12)  
  • Currently 2.13 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (16 votes cast)
 
[21,008 views] Email Article To a Friend View Printable Version
Copy a file to all subfolders of a folder with one command UNIX
Over the weekend, I was installing a demo of some web-based help desk software, and this particular package used encrypted PHP files. With my hosting company, I have to place a php.ini file in every directory that contains encrypted files, or the site won't work. This particular package had hundreds of directories, buried in folders and subfolders, and I was dreading the task of copying this one file into each of them.

A tip from my friend James pointed me to the solution, in the form of Smiling Dragon's reply in this thread over on the Unix/Linux forums. Assuming the file exists in the parent directory, then this command does the trick:
find . -type d -exec cp php.ini {}/ \;
This worked perfectly, and incredibly quickly -- at first I thought it had failed, but a quick sample of a deeply-buried folder showed that the file was now in each and every sub-directory on the site. Note that if something goes wrong when you try this, you may create quite a mess to clean up; I tried it first on my local machine, just to be sure it worked. Only then did I run the command on my web server.
  Post a comment  •  Comments (8)  
  • Currently 3.44 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (16 votes cast)
 
[11,886 views] Email Article To a Friend View Printable Version
How to use 'cp' as a simple but reliable backup tool UNIX
While looking for the perfect product to keep my photos safe, I discovered that sometimes simple is best. My requirements were simple: ensure that all my digital photos, stored on a locally attached USB drive, were duplicated to another drive attached to my AirPort Extreme. My photos are in RAW format (specifically DNG files) and will never change, so I only need to concern myself with new files.

I checked out numerous commercial and free products for backup, synchronizing and more, and nothing quite fit the bill. Whilst rsync could probably do the job, I couldn't get my head around the terminology to be sure I wasn't risking the original files. Then I discovered the solution. So mind-bogglingly simple, and no third-party software required. In Terminal, I run this command:
cp -npRv "/Volumes/LocalUSB/Photos/" "/Volumes/RemoteUSB/Photos/"
Yes, it is the standard Unix copy (cp) command with a few options:
  • n - Do not overwrite an existing file
  • p - Preserves attributes, including resource forks
  • R - When the source file is a directory and the path ends in with a slash (/) then the entire contents of the directory are copied recursively
  • v - Causes files to be listed when copied
The n and R ensure that all new files are copied from the directory tree. Files already there are not re-copied. In short, a quick and efficient means of getting just the new photos copied over.
  Post a comment  •  Comments (47)  
  • Currently 2.38 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (34 votes cast)
 
[22,764 views] Email Article To a Friend View Printable Version