How to show ssh host name on the iTerm’s background

(Mac OS X) (5,210 views)

iTerm How many ssh session do you open usually? In Scribd we have about 50 machines, and most of the time I have to connect to several of them to do my work. But there is a big problem — it’s hard to distinguish among different tabs in iTerm. Of course, you can see the host where you connected to in the tab title, but the title is really small, low contrast, etc. So I had a dream since I’ve started using Mac — to get current host written with large letters on the background.

iTerm has a great support of AppleScript. Looking through the commands list I’ve found a really useful one: background image path. The last thing to figure out is how to create an image with the current host name, and I decided to use ImageMagick. So, let’s do it!

First, you need to install ImageMagick. You can use binary distribution, but I’d prefer the MacPorts version:

1
port install ImageMagick

Let’s create a test image:

1
2
3
convert -size "570x342" xc:black -gravity "NorthEast" -fill "#662020" \
        -family "Georgia" -style "Normal" -pointsize "60" -antialias \
        -draw "text 20,10 'kpumuk.info'" "/tmp/iTermBG.png"

Here is the result and it looks pretty good!

Image with hostname

Ok, now the most interesting part. I’ve created a bash script in ~/bin/ssh, which sets the background before calling ssh and clears it on exit. I suppose you have this folder specified before /usr/bin in your .bash_profile:

1
export PATH=$HOME/bin:/opt/local/bin:$PATH

Here is the script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/bin/bash

BG_COLOR="#000000"      # Background color
FG_COLOR="#662020"      # Foreground color
DIMENSIONS="570x342"    # Console dimensions
GRAVITY="NorthEast"     # Text gravity (NorthWest, North, NorthEast,
                        # West, Center, East, SouthWest, South, SouthEast)
OFFSET="20,10"          # Text offset
FONT_SIZE="60"          # Font size in points
FONT_FAMILY="Georgia"   # Font family
FONT_STYLE="Normal"     # Font style (Any, Italic, Normal, Oblique)

HOSTNAME=`echo $@ | sed "s/.*@//" | sed "s/ .*//"`

set_bg () {
  osascript -e "tell application \"iTerm\"
    set current_terminal to (current terminal)
    tell current_terminal
      set current_session to (current session)
      tell current_session
        set background image path to \"$1\"
      end tell
    end tell
  end tell"

}

on_exit () {
  if [ ! -f /tmp/iTermBG.empty.png ]; then
    convert -size "$DIMENSIONS" xc:"$BG_COLOR" "/tmp/iTermBG.empty.png"
  fi
  set_bg "/tmp/iTermBG.empty.png"
  rm "/tmp/iTermBG.$$.png"
}
trap on_exit EXIT

convert -size "$DIMENSIONS" xc:"$BG_COLOR" -gravity "$GRAVITY" -fill "$FG_COLOR" -family "$FONT_FAMILY" -style "$FONT_STYLE" -pointsize "$FONT_SIZE" -antialias -draw "text $OFFSET '$HOSTNAME'" "/tmp/iTermBG.$$.png"
set_bg "/tmp/iTermBG.$$.png"

/usr/bin/ssh "$@"

Let’s try it:

iTerm with hostname in the background

So now I can stop shaking with fear when doing some destructive things in console — I know which host exactly I’m trying to kill. I hope you will find this post useful.

Updated 05/01/2009: Updated on_exit function to fix bug with scrolling after ssh session being closed.

Updated 05/03/2009: Added some variables to customize how host name will look.

9 Responses to this entry

Subscribe to comments with RSS

e0ne @
said on 2009-05-01 at 11.42 am · Permalink

It’s interesting feature. But I think it’s not very useful.

dgs
said on 2009-05-02 at 5.05 am · Permalink

Very cool, that would be so useful in large data centers!!!

j2
said on 2009-05-06 at 9.48 pm · Permalink

This will work better with arguments passed to ssh:

1
HOSTNAME=`echo $@ | sed 's/.* \(.*\)$/\1/' | sed "s/.*@//"`

e.g. on OSX -Y forwards X11 connections easily, but using your sed expressions the command ssh -Y myhost will result in -Y being the image shown in the background.

Also, you still need to be careful that you are on the host that you logged onto originally from OSX. if you chain your ssh’s your background image will be incorrect.

said on 2009-07-02 at 3.34 pm · Permalink

Hi,

Thanks a lot for this, that solved problems I had for ages connecting to too many servers….

just a little problem, the background image disapear when I enter full screen (CMD-Enter)

cheers

julien

Doug Vasquez @
said on 2009-09-17 at 6.11 pm · Permalink

Great tip, thanks for sharing!

I did discover an error when attempting to run in snow leopard (v10.6.1):

1
2
3
Error loading /Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types:  dlopen(/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types, 262): no suitable image found.  Did find:
/Library/ScriptingAdditions/Adobe Unit Types.osax/Contents/MacOS/Adobe Unit Types: no matching architecture in universal wrapper
osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/Adobe Unit Types.osax" declares no loadable handlers.

After a quick google search, I ran into a post (http://forums.adobe.com/thread/486208) that suggested prefixing the osascript command with the appropriate supported architecture:

1
arch -i386 osascript ...

So if anyone runs into this issue, just change the following line:

1
osascript -e "tell application \"iTerm\"

with this:

1
arch -i386 osascript -e "tell application \"iTerm\"
Andrew McFague @
said on 2009-10-01 at 3.23 pm · Permalink

I loved this, but the problem is, I use a fullscreen monitor specifically for SSH at work, but I only have my regular laptop screen at home. So, of course, the background would be extremely distorted.

So I came up with the following; this should net you the approximate resolution by converting the rows/columns to a resolution. I simply used the ratio of the columns/rows to the resolution on my laptop:

1
2
3
4
# Generate dimension based on screensize
H=$((1440/178*`stty size | cut -d ' ' -f2`))
V=$((900/47*`stty size | cut -d ' ' -f1`))
DIMENSIONS=$H"x"$V

Its not foolproof, but its very dynamic and works great for me! :)

Andrew

Andrew McFague @
said on 2009-10-06 at 3.45 pm · Permalink

A quick improvement I had to implement when dealing with non-terminal windows (i.e., bash scripts):

1
2
3
4
5
6
7
# First, check to see if we have the correct terminal!
[ "$( tty )" == 'not a tty' ] && /usr/bin/ssh "$@" && exit

# Generate dimension based on screensize
H=$((1440 / 178 * `stty size | cut -d ' ' -f2` ))
V=$((900 / 47 * `stty size | cut -d ' ' -f2` ))
DIMENSIONS=$H"x"$V    # Console dimensions

This uses the the tty variable to detect whether or not its a terminal session, so it won’t try and grab the screen size from stdin-less sessions. Note, this depends on the tty variable being set which, from my basic testing, works flawlessly on bash-3.2.

Andrew

said on 2009-10-06 at 3.48 pm · Permalink

That’s sweet, thank you! Will test on my machine and update the post. Good work!

Andrew McFague @
said on 2009-10-06 at 7.24 pm · Permalink

Wait! I realized a mistake; ssh doesn’t always exit with a 0 status, even though it may be a normal exit. To make it a little clearer, the following worked:

1
2
3
4
5
# First, check to see if we have the correct terminal!
if [ "$( tty )" == 'not a tty' ] ; then
    /usr/bin/ssh "$@"
    exit
fi

Comments are closed

Comments for this entry are closed for a while. If you have anything to say – use a contact form. Thank you for your patience.