Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
739 views
in Technique[技术] by (71.8m points)

macos - How do I make the apple terminal window auto change colour scheme when I ssh to a specific server

When I ssh into a remote production server I would like the colour scheme of my terminal window to change to something brigh and scary, preferably red, to warn me that I am touching a live scary server.

How can I make it automatically detect that I have ssh'ed somewhere, and if that somewhere is on a specific list, change the colour scheme?

I want to update the Scheme of Terminal.app, not know how I would do this in a pure linux/unix env

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Put following script in ~/bin/ssh (ensure ~/bin/ is checked before /usr/bin/ in your PATH):

#!/bin/sh

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

set_bg () {
  osascript -e "tell application "Terminal" to set background color of window 1 to $1"
}

on_exit () {
  set_bg "{0, 0, 0, 50000}"
}
trap on_exit EXIT

case $HOSTNAME in
  production1|production2|production3) set_bg "{45000, 0, 0, 50000}" ;;
  *) set_bg "{0, 45000, 0, 50000}" ;;
esac

/usr/bin/ssh "$@"

Remember to make the script executable by running chmod +x ~/bin/ssh

The script above extracts host name from line "username@host" (it assumes you login to remote hosts with "ssh user@host").

Then depending on host name it either sets red background (for production servers) or green background (for all other). As a result all your ssh windows will be with colored background.

I assume here your default background is black, so script reverts the background color back to black when you logout from remote server (see "trap on_exit").

Please, note however this script does not track chain of ssh logins from one host to another. As a result the background will be green in case you login to testing server first, then login to production from it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...