If you have ever used the whois command from Terminal, you know how many lines of legal junk comes along with the few lines of useful information you wanted. It almost always requires me to grab the mouse and scroll up a page or two to reach the vital information, and that is annoyingly wasteful. So in my quest to reduce the clutter, I wrote this ruby script to deal with the output from .com, .net, .org, and unspecified domain extensions.
I apologize in advance to any real ruby programmers out there, as this is the first script I have ever written. As far as I can tell, it works, but I imagine it is pretty inelegant, and I'm sure someone could clean it up and improve upon it. For one thing, I am not sure it deals well with all the options that you can pass to whois, but for just looking up basic details, you don't need any options.
I periodically receive files that are somewhat cryptically named, and I like to rename them to something more understandable. I found this older hint, but it didn't work for my needs -- my renamed files contain spaces. For example, I might receive several files whose names are something like this:
TS_412_96k.fil
TS_413_96k.fil
TS_414_32k.fil
And I want to rename them something like:
Total Entry #412.txt
Total Entry #413.txt
Total Entry #414.txt
After nearly tearing my hair out trying various combinations of awk and sed in my scripts, I discovered that the bash shell has its own string handling functions! This quickly allowed me create the following simple script:
for prog in *.fil; do
progno=${prog:3:3}
mv $prog 'Total Entry #'$progno'.txt'
done
Voila! Note that bash starts character numbering at zero rather than one, as do awk and sed. Thus, in the example above, the substring returned by {prog:3:3} would be the numeric values in the original filenames.
Open Terminal, and at the command prompt type bash --version. You're likely to see something like GNU bash, version 2.05b.0(1)-release (powerpc-apple-darwin8.0) -- at least if you're on Tiger, that is. Note that you'll have a powerpc version of bash, even if you're on an Intel powered Mac! Here's how I converted to a newer, Intel compiled version.
I installed a newer bash through Fink; the latest version is in the unstable tree, so you may have to modify your Fink config file. Once that's done, just type fink install bash to install. Since I wanted to be able to switch to the old version of bash, if need be, I created a symlink here:
ln -s /sw/bin/bash /usr/bin/bash
Then I updated my NetInfo profile to use /usr/bin/bash as the default shell (which in turn specifies the default shell to /usr/bin/login). I used the GUI NetInfo Manager, but you could use nicl and niutil commands if you're brave. So if something should go wonky with the 'unstable' version of bash, I can login remotely, change the symlink /usr/bin/bash from /sw/bin/bash back to the default of /bin/bash.
One could also change the Terminal preference to call the symlink, instead of using NetInfo to modify /usr/bin/login -- but the NetInfo way feels more canonical. Now type bash --version, and you should have (assuming an Intel-powered Mac) GNU bash, version 3.00.0(1)-release (i386-apple-darwin8.7.1), a whole point update, and Intel native!
By accident I have several times moved some important files from the Desktop to the Trash Can, and then emptied the Trash Can before discovering that the files were missing ... sigh! I decided that I needed some sort of automatic backup mechanism for the Trash Can -- in that way I would have some time to react to such mistakes.
I wrote a very simple bash script to copy files in the trash can to a backup directory:
#!/bin/sh
# trashbackup.sh ; version 1.0
ditto ~/.Trash/ ~/.Trash_backup/
I invoke the script every 10 minutes via /etc/crontab, using this crontab entry: (I am the user bt):
# The periodic and atrun jobs have moved to launchd jobs
# See /System/Library/LaunchDaemons
#
# minute hour mday month wday who command
*/10 * * * * bt /Users/bt/app-unix-scripts/trashbackup.sh
I also invoke another small script that empties the .Trash_backup directory once a week. This could be done more often, or executed manually, if you trash a large amount of data. Nothing fancy, but I think it will same me some frustration in the future. Hopefully some of you might find it useful as well.
[robg adds: Note that this script will only handle items on your boot drive. If you have multiple partitions or drives, you'll have to get fancier with the code to backup the contents of the .Trashes folder on each volume.]
I created the following scripts to mimic the ipconfig /all functionality found in the Windows NT operating system. In the unix world, all of the info is available, but not in one place which is where this script comes in handy. It concatenates all of the various pieces of IP configuration into one easy-to-read display. The bonus is all of the functions used are contained in a separate file, which can be sourced into your shell environment and used individually.
Download the scripts (4KB download), and install them wherever you like. The first script, functions.sh, contains the functions that I use in the second file, ipinfo.sh. Be sure to alter the second line of the ipinfo.sh file to point to the correct location of functions.sh.
When you run ipinfo.sh from the command line, you will receive output similar to the following:
I split the functions out of the main script to I could use them in my bashrc file individually. The functions are as follows:
get_public, get_ip, get_mask, get_gateway, get_dns, get_domain
The get_ip and get_mask functions take a single argument of an interface name (lo0, en0, en1, fw0, etc.). After these are added to my shell environment, I can do things like ping $(get_gateway) or get_ip en1.
[robg adds: These scripts worked as described in my testing. Remember to make ipinfo.sh executable via chmod 755 ipinfo.sh.]
I wrote this short script when I needed to clean up my Downloads folder (but it can be used on any folder). It looks at when you last accessed each file in the folder, and if it is more than a set number of days old (14 by default), it either prints out the name of the file, moves it to the trash, or deletes it.
Save the file somewhere as clean.rb, and in the Terminal (Applications > Utilities), cd to the directory (e.g., cd Desktop if it's on your Desktop), and type chmod +x clean.rb. If you want, you can edit the top of the file. There are two settings: what you want to do with the file (list, move to trash, or delete), and how long it must be since you accessed it. You can then run the file by typing ./clean.rb Downloads, for example. It will clean out or list the files according to your preference. It will only remove whole directories. Hope this is useful to everyone here!
Sometimes you may need to get a sorted list of all files in a direcoty transformed in a database format, so, after getting the directory list you may end cutting and pasting the data to your database. On the other hand, there's an easy way to do that, if you just follow this tip. Open Terminal and type:
If you want to generate a file instead of sending the result to the clipboard, just replace the | pbcopy part with > xxx, where xxx is the file name you want to create.
[robg adds: When this hint was submitted, Geeklog killed the backslashes in the above command. After much trial and error, I believe I have them back where they need to be to generate the proper output. If there are errors, however, they are mine, not the original poster's. I also split the command onto three lines for a narrower display, but it should copy and paste.]
As an owner of two iBooks and a Mac mini, it has struck me that these are all likely targets for a thief. A while ago I recalled this story about how someone used Timbuktu Pro to recover a stolen iMac. I then realised that because we use Apple's Remote Desktop software at work, then all I need is the current IP address of a computer to be able to observe/control it.
So I wrote a perl script to lookup my computer's IP address and email it to me at regular intervals. I then had the idea to make the script determine the location of the computer based on the IP address. This is not always an exact science, but it can often pinpoint the town where the computer is. Recently I also realised that I could use the UNIX screencapture command to also email myself a screen capture (to potentially see what any thief is viewing/typing). This might be of more use to those who don't own Remote Desktop. Finally, my latest source of inspiration occurred after buying an iSight camera for my Mac mini. I discovered the great (and free) isightcapture utility, which now allows me to email pictures taken by the iSight camera.
So I now have my computers send me regular emails throughout the day which get automatically filtered by a rule in my Mail application. Because each of these three different functions (IP address, screen capture, and isight capture) require different tweaks to the system, I wrote three different perl scripts so people can pick and choose what functionality they want. These three scripts can be downloaded as a tarball, or downloaded separately from my blog, which has full and detailed instructions on how to install each script (some require some tweaks to the system).
The main limitations of these scripts are that a) the stolen computers need to be online long enough for the scripts to run, and b) the thief doesn't just wipe your hard drive. I try to counter the second option by not using a password on the screensaver, and setting the computers to automatically log into a somewhat-restricted guest account following any reboot. Hopefully this would encourage someone to use the computer just long enough for you to get some helpful information emailed to you.
The Remote Desktop Client app is a pretty handy way to remotely manage/control a Microsoft Windows workstation. However, the solution Microsoft provided is a little bit outdated and runs on Rosetta, which hogs CPU and memory. An alternative way of doing this is by using the rdesktop program, which is open source for Linux/Unix.
First, go to their download page and download the latest source code. Extract the source to say ~/rdesktop. Then, we need to compile the program on Mac OS X. Open Terminal and cd ~/rdesktop, run the following commands:
$ ./configure
$ make
$ sudo make install
Finally, you can use X11 to run the rdesktop program.
[robg adds: You'll need to have the X11 application installed in order to compile and use rdesktop. I had some issues initially, due to an X11 install that was somehow messed up (no X11R6 directory in /usr). After some retries with the X11 installer, compiling worked as described above.]