public
Last active

ssh.py

  • Download Gist
host_icons.example
1 2 3 4 5 6 7 8 9 10 11 12 13
{
"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"}
]
}
readme.rst
reStructuredText

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.

ssh.py
Python
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#!/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)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.