当我 ssh 到特定服务器时,如何让苹果终端窗口自动更改配色方案 [英] How do I make the apple terminal window auto change colour scheme when I ssh to a specific server
问题描述
当我通过 ssh 进入远程生产服务器时,我希望我的终端窗口的配色方案更改为明亮而可怕的颜色,最好是红色,以警告我我正在触摸一个实时的可怕服务器.
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.
如何让它自动检测到我在某个地方 ssh 连接过,如果某个地方在特定列表中,请更改配色方案?
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?
我想更新Terminal.app的Scheme,不知道在纯linux/unix环境下怎么做
I want to update the Scheme of Terminal.app, not know how I would do this in a pure linux/unix env
推荐答案
将以下脚本放在 ~/bin/ssh
中(确保 ~/bin/
在/usr/bin/
在你的路径中):
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 "$@"
记得通过运行 chmod +x ~/bin/ssh
上面的脚本从username@host"行中提取主机名(假设您使用ssh user@host"登录到远程主机).
The script above extracts host name from line "username@host" (it assumes you login to remote hosts with "ssh user@host").
然后根据主机名设置红色背景(用于生产服务器)或绿色背景(用于所有其他服务器).因此,您所有的 ssh 窗口都将带有彩色背景.
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.
我在这里假设您的默认背景是黑色,因此当您从远程服务器注销时,脚本会将背景颜色恢复为黑色(请参阅trap on_exit").
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").
请注意,但是此脚本不会跟踪从一台主机到另一台主机的 ssh 登录链.因此,如果您先登录测试服务器,然后从它登录生产,背景将是绿色的.
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.
这篇关于当我 ssh 到特定服务器时,如何让苹果终端窗口自动更改配色方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!