Skip to content

Instantly share code, notes, and snippets.

@laanwj
Created April 9, 2011 07:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save laanwj/911215 to your computer and use it in GitHub Desktop.
Save laanwj/911215 to your computer and use it in GitHub Desktop.
ssh.py

Purpose

This scripts helps distinguish between Konsole terminal tabs, so that is is possible to see what host is connected to from the tab icons.

Features

  • Sets Konsole tab icon before connecting, restores it to original after disconnecting
  • Auto-detects whether output connected to a tty, only changes icon if not redirecting to file or stream.
  • Supports icon per host, of even per user@host

Installation

To use this script, copy the host_icons.example to ~/.host_icons and adapt it. The file must be in valid JSON format. An overview of the parameters:

path

Path to the icons.

domain

Default domain. This will be used in case a bare hostname is SSHed to.

icons

List of icons, per host. Icons can be either generic or separate for a user, such as root.

Then, put ssh.py somewhere in your PATH before /usr/bin, and rename it to ssh. For example in /home/user/bin, which is put in front of your path by default in (K)Ubuntu.

{
"path":"/home/foo/icons/",
"domain":"example.com",
"icons":[
{"host":"aaa.example.com", "icon":"red.png"},
{"host":"bbb.example.com", "icon":"green.png"},
{"host":"ccc.example.com", "icon":"yellow.png"},
{"host":"ddd.example.com", "icon":"orange.png"},
{"host":"eee.example.com", "icon":"blue.png"},
{"host":"fff.example.com", "icon":"cyan.png"},
{"host":"ggg.example.com", "icon":"black.png", "icon_root":"black_root.png"}
]
}
#!/usr/bin/python
"""
Script that wraps around ssh and sets the console icon,
based on the host connected to. After the session, the default
konsole icon is restored.
Wladimir van der Laan, 2011. Released into the public domain.
"""
import subprocess
import json
import os
import sys
def wrapper(program):
return subprocess.call([program] + sys.argv[1:])
def set_icon(icon="utilities-terminal"):
if sys.stdout.isatty():
# Only set icon if our output is a tty, not a pipe or file
sys.stdout.write("\033]32;%s\007" % icon)
sys.stdout.flush()
# Read configuration from json file
home = os.getenv("HOME")
cfg_file = os.path.join(home, ".host_icons")
cfg = json.load(open(cfg_file,"r"))
icon_path = cfg["path"]
domain = "."+cfg["domain"]
icons = {}
for icon in cfg["icons"]:
icons[icon["host"]] = icon
if icon["host"].endswith(domain):
icons[icon["host"][0:-len(domain)]] = icon
# Find command line arguments in host->icon mapping
icon = None
for arg in sys.argv[1:]:
# support root@...
(user, _, host) = arg.rpartition("@")
# special icon for user@host?
try:
icon = icons[host]["icon_" + user]
except KeyError:
# otherwise, fall back to normal "icon"
try:
icon = icons[host]["icon"]
except KeyError:
pass
if icon is not None:
set_icon(os.path.join(icon_path,icon))
try:
rv = wrapper('/usr/bin/ssh')
finally:
if icon is not None:
set_icon()
exit(rv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment