Archive for the ‘Shell’ Category

Enable Screen Sharing from the Terminal in Leopard

TerminalAfter graduation and my last day at work, I’ve taken a road trip to visit the Bennett’s in D.C. and was promptly chagrined while trying to show off Leopard’s screen sharing over OpenVPN.

Fortunately, it’s pretty easy to turn on Screen Sharing from an SSH session.

echo -n enabled > /Library/Preferences/com.apple.ScreenSharing.launchd

Launchd should automatically start the Screen Sharing service when this file is modified.

More information is available at Apple Remote Desktop: Configuring remotely via command line (kickstart)

 

Fast Screen Sharing with Quicksilver

After upgrading all of my personal machines to Leopard, I’ve found myself using the Screen Sharing feature quite often. Many people have two Mac’s these days, particularly owners of the MacBook Air, and screen sharing makes it incredibly convenient to access a machine in another room.

As with most things I do frequently, Quicksilver has utterly spoiled me. The process of making the Finder active, pressing Command+K, and selecting or typing vnc://champ.local is just way too long.

Fortunately, it’s really easy to integrate Screen Sharing into our Quicksilver workflow.

Here’s how.

You’ll need to edit unix plaintext files, rather than rich text which TextEdit.app seems to insist on producing. TextWrangler is a great, free, text editor for editing Unix plain text files, although I’m partial to TextMate.

First, make sure Screen Sharing is turned on in the Sharing Preference Pane in Leopard.

Sharing Pref Pane

Suppose you want to connect to a machine named “champ” in the Sharing Preference Pane.

The script will have the contents:

#!/bin/sh
# Nice and short
open vnc://champ.local &

Save the script into ~/Library/Application Support/Quicksilver/Scripts/champ.sh and make sure that folder is scanned by Quicksilver.

You’ll also need to make sure the script is executable, so open up Terminal.app and change the permissions:

chmod a+x ~/"Library/Application Support/Quicksilver/Scripts/"*.sh

That’s it. Now you should just be able to invoke Quicksilver, start typing the name of the machine you want to share the screen with, and presto! Nice and fast.

Quicksilver VNC

 

Excluding Directories with find

TerminalI’ve been using the find command for over a decade now, and I’m ashamed to say I never really learned how to properly exclude directories. Dealing with with subversion working copies that litter “.svn” folders everywhere, I finally sorted it all out this afternoon.

To exclude “.svn” folders and all contents:

$ find . '!' '(' -name '.svn' -prune ')'

This, combined with find -print0 and xargs -0 to execute arbitrary commands on every filesystem object found is a wonderful tool to keep handy.

 

Sleep Display Script

Here’s a quick script I cooked up to turn off the blinding light that is my iMac display when I go to bed at night.

QuickSilver Display Sleep 1 Minute

Features:

  • Runs great from Quicksilver.
  • Talks to you. (May not be a feature.)
  • Doesn’t require administrative rights.
  #!/bin/bash
  # Jeff McCune 
  export CURRENT_DELAY=$(pmset -g | grep displaysleep | awk '{print $2}')
  # Charger or Battery Flag.
  pmset -g | grep -q '^AC.*\*' && export MODE="-c" || export MODE="-b"
  say "The display will shut off in about 90 seconds" &
  # force allows this to work for non-admin users.
  pmset force $MODE displaysleep 1
  # Quicksilver blocks until script completion.  Fork off the reset command.
  bash -c 'sleep 120; pmset force $MODE displaysleep $CURRENT_DELAY' &
  exit 0
 

svnmerge.py – Managing Subversion Branches

I’ve been doing a lot of subversion branch, test, merge cycles against our main Puppet configuration repository. I’ve run into issues when both the trunk and my testing branches are modified after I’ve forked off my branch.

This creates merge conflicts when I’m done testing, and need to merge my changes back into the production branch.

In an effort to reduce the overhead associated with manually resolving each conflict that arises from the divergence, I’ve started employing the use of svnmerge.py.

It’s great.

  svn copy cluster-orange-server cluster-orange-server-test01
  svn checkin cluster-orange-server-test01 -m 'Branched testing off.'
  cd cluster-orange-server-test01

  svnmerge.py init
  svn ci -F svnmerge-commit-message.txt

Now that my pristine branch of the production code has been initialized with svnmerge, I’m free to make changes to my testing copy. Once I need to merge back into production, I just need:

  svnmerge.py merge
 

User Level VPN with Leopard

CaminoOne of the small, but incredibly useful features for me in Leopard is that ssh-agent is automatically running for each user account. This relatively small change allows me to log into remote machines without entering my password each time.

Using the SOCKS proxy built into ssh, we’re also able to setup a quick and easy secure tunnel. I wanted to check some sensitive information this morning, but I’m at a coffee shop that doesn’t pass VPN traffic, so I quickly hacked together the following:

Setup a new Location in the Network System Preference Pane to configure the SOCKS proxy at 127.0.0.1, port 4088. This connects most Apple applications to the secure and encrypted tunnel.

Network Preferences Socks ssh Proxy

Next, I configured ssh to automatically setup the SOCKS proxy whenever I type “ssh ford”, which is an alias for my workstation back at the office.

# ~/.ssh/config
host ford
  User mccune
  HostName ford.math.ohio-state.edu
  # Handle sleep/wake robustly with TCPKeepAlive
  TCPKeepAlive no
  Port 22
  # DynamicForward is a SOCKS proxy server.
  DynamicForward 4088
  ForwardX11 no

With this configuration, I’m able to load my SSH public key into the ssh-agent running by default on Leopard, type “ssh ford” to setup the encrypted SOCKS proxy, then change location to “SSH Socks Proxy” to automatically have Mail.app, iChat, Safari and Camino use the secure proxy.

An easy way to verify the proxy is working is to add an IP Address gadget to your personal google home page:

Google ip Address

Finally, with the Network Location module for Quicksilver, you can easily switch back and forth between the encrypted proxy.

Quicksilver SSH Network Location

 

TextMate on Leopard Command Line Tool Fix

Text MateI quickly noticed that the “mate” command line tool does not work as expected under GNU Screen on Leopard. The error I get is:

mate: failed to establish connection with TextMate.

I suspect this is a side-effect of the Leopard sandbox feature. I’ve found a quick shell alias to be a nice work-around:

alias mate='open -a TextMate.app'

This works inside of GNU Screen, where the mate command line utility does not.

 

Find a machine with ping and say

VMWare FusionI haven’t posted in awhile, but I was pleased with the results of this small hack. I booted VMWare Fusion on my Leopard workstation, and was disappointed that the keyboard doesn’t work on the console, so I couldn’t run ifconfig to look at it’s dhcp assigned IP address. I run vmware machines in NAT mode, so I couldn’t look at my dhcp server logs either.

The following shell script did the trick nicely. I went about my business, and eventually my computer spoke; “128 is up.” which allowed me to ssh in.

X=2
while [ $X -lt 255 ]
do
  ping -c 1 -t 1 172.16.90.$X 2>/dev/null >/dev/null && (echo $X: Up; say "$X is up") || echo $X: DOWN
  ((X++))
done

You may find this useful for locating machines if you don’t have nmap installed.

 

Quick Synergy KVM Scripts

SynergyI constantly use keyboard sharing software like synergy2, teleport, x2vnc, x2x, etc… I’ve settled on synergy since it’s relatively platform independent. I use the command line program rather than synergyKM, just because I find it far more reliable. I also tunnel through ssh and found the certificate stuff in synergyKM to be less than ideal.

In any case, I wrote a small shell script which fires up the synergy server on the machine with the physical keyboard attached, then reaches out via ssh to the client machine, copying the synergyc client binary, establishes a reverse tunnel back to the synergy server on my laptop, launches the client in the background on the remote machine and connecting to the tunnel, then finally detaches the ssh session. When I close my lid and walk away at the end of the day, the tunnel and synergy processes gracefully clean up after themselves.

I find this setup ideal, because whenever I bring my portable machine into the office in the morning, I just run

kvmc ford

and the script takes care of everything for me.

The scripts are located in the northstarlabs repository. As usual, most of their usefulness is derived from password-less ssh authentication using public keys and ssh-agent.

 

Remembering comm when partitioning text files

TerminalFrom time to time I’ll use the unix find -newer command to snag a list of file and folders modified after some other reference file. I’ll redirect the output of find to a text file, which now contains all folders and files I’m interested in.

I then would like to work with these files and folders at the shell. Yesterday I needed to split the master listing into two disjoint lists; one of all folders, one of all files. It’s fairly easy to create a list of the folders if you don’t care about empty folders. Just chop off each trailing element of each line, then sort the text file and pipe it into uniq.

Files are a bit trickier though. Here’s the command I always forget about:

comm -23 all.txt folders.txt

From the man page: The comm utility reads file1 and file2, which should be sorted lexically, and produces three text columns as output: lines only in file1; lines only in file2; and lines in both files.

So, comm neatly partitions text files organized by lines by finding and printing the intersection of the files.