当我ssh到特定服务器时,如何使Apple终端窗口自动更改颜色方案

时间:2008-10-01 14:41:38

标签: macos terminal osx-leopard

当我进入一个远程生产服务器时,我希望我的终端窗口的配色方案变成一种明亮可怕的颜色,最好是红色,以警告我,我正在触摸一个可怕的服务器。

如何让它自动检测到我在某处有ssh'ed,如果某个地方位于特定列表中,请更改颜色方案?

我想更新Terminal.app的Scheme,不知道如何在纯linux / unix环境中执行此操作

10 个答案:

答案 0 :(得分:50)

~/bin/ssh中添加以下脚本(确保在您的路径~/bin/之前/usr/bin/查看{}}:

#!/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 "$@"

上面的脚本从“username @ host”行中提取主机名(它假定您使用“ssh user @ host”登录到远程主机)。

然后根据主机名称设置红色背景(用于生产服务器)或绿色背景(用于所有其他)。因此,所有ssh窗口都将采用彩色背景。

我假设您的默认背景为黑色,因此当您从远程服务器注销时,脚本会将背景颜色恢复为黑色(请参阅“trap on_exit”)。

但请注意,此脚本不会跟踪从一个主机到另一个主机的ssh登录链。因此,如果您先登录测试服务器,然后从中登录生产,背景将为绿色。

答案 1 :(得分:29)

终端的一个鲜为人知的功能是您可以将设置配置文件的名称设置为命令名称,并在通过 Shell>创建新终端时选择该配置文件。新命令...... 外壳>新的远程连接......

例如,复制默认配置文件,将其命名为“ssh”并将其背景颜色设置为红色。然后使用新命令... 运行ssh host.example.com

它也匹配参数,因此您可以让它为不同的远程主机选择不同的设置,例如。

答案 2 :(得分:7)

这是一个基于处理退出的几个现有答案的组合解决方案。如果您不想处理16位颜色值,还包括一些额外的内容。

这应该放在你的〜/ .bash_profile

# Convert 8 bit r,g,b,a (0-255) to 16 bit r,g,b,a (0-65535)
# to set terminal background.
# r, g, b, a values default to 255
set_bg () {
    r=${1:-255}
    g=${2:-255}
    b=${3:-255}
    a=${4:-255}

    r=$(($r * 256 + $r))
    g=$(($g * 256 + $g))
    b=$(($b * 256 + $b))
    a=$(($a * 256 + $a))

    osascript -e "tell application \"Terminal\" to set background color of window 1 to {$r, $g, $b, $a}"
}

# Set terminal background based on hex rgba values
# r,g,b,a default to FF
set_bg_from_hex() {
    r=${1:-FF}
    g=${2:-FF}
    b=${3:-FF}
    a=${4:-FF}

    set_bg $((16#$r)) $((16#$g)) $((16#$b)) $((16#$s))
}

# Wrapping ssh command with extra functionality
ssh() {
    # If prod server of interest, change bg color
    if ...some check for server list
    then
        set_bg_from_hex 6A 05 0C
    end

    # Call original ssh command
    if command ssh "$@"
    then
        # on exit change back to your default
        set_bg_from_hex 24 34 52
    fi
}
  • set_bg - 需要4(8位)颜色值
  • set_bg_from_hex - 需要4个十六进制值。我使用的大多数颜色参考都是十六进制的,所以这对我来说更容易。它可以更进一步实际解析#RRGGBB而不是RR GG BB,但它对我来说效果很好。
  • ssh - 使用您想要的任何自定义逻辑包装默认的ssh命令。 if语句用于处理退出以重置背景颜色。

答案 3 :(得分:6)

将答案12结合起来有以下几点:

按照1中的说明创建# BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress # X-XSS-Protection <IfModule mod_headers.c> Header set X-XSS-Protection "1; mode=block" </IfModule> # X-Frame-Options <IfModule mod_headers.c> Header always append X-Frame-Options SAMEORIGIN </IfModule> # Security Headers - X-Content-Type: nosniff <IfModule mod_headers.c> Header set X-Content-Type-Options nosniff </IfModule> # BEGIN 301 Redirects Redirect 301 /old-page-name/ https://www.myurl.com/new-page-name/ Redirect 301 /another-old-page/ https://www.myurl.com/another-new-page/ # END 301 Redirects 文件,其中包含以下内容:

~/bin/ssh

使其可执行:#!/bin/sh # https://stackoverflow.com/a/39489571/1024794 log(){ echo "$*" >> /tmp/ssh.log } HOSTNAME=`echo $@ | sed s/.*@//` log HOSTNAME=$HOSTNAME # to avoid changing color for commands like `ssh user@host "some bash script"` # and to avoid changing color for `git push` command: if [ $# -gt 3 ] || [[ "$HOSTNAME" = *"git-receive-pack"* ]]; then /usr/bin/ssh "$@" exit $? fi set_bg () { if [ "$1" != "Basic" ]; then trap on_exit EXIT; fi osascript ~/Dropbox/macCommands/StyleTerm.scpt "$1" } on_exit () { set_bg Basic } case $HOSTNAME in "178.222.333.44 -p 2222") set_bg "Homebrew" ;; "178.222.333.44 -p 22") set_bg "Ocean" ;; "192.168.214.111") set_bg "Novel" ;; *) set_bg "Grass" ;; esac /usr/bin/ssh "$@"

文件chmod +x ~/bin/ssh包含以下内容:

~/Dropbox/macCommands/StyleTerm.scpt

单词#https://superuser.com/a/209920/195425 on run argv tell application "Terminal" to set current settings of selected tab of front window to first settings set whose name is (item 1 of argv) end run 来自mac os终端设置 cmd terminal profiles

答案 4 :(得分:5)

您可以在.bashrc中设置$ PS1变量。

red='\e[0;31m'
PS1="$\[${red}\]"

编辑:   为此,打开终端。然后说

#touch .bashrc

然后,您可以在textEdit或TextWrangler中打开.bashrc并添加以前的命令。

答案 5 :(得分:5)

另一个解决方案是在ssh配置文件中设置颜色strait:

在〜/ .ssh / config

里面
Host Server1
   HostName x.x.x.x
   User ubuntu
   IdentityFile ~/Desktop/keys/1.pem
   PermitLocalCommand yes
   LocalCommand osascript -e "tell application \"Terminal\" to set background color of window 1 to {27655, 0, 0, -16373}"

Host Server2
   HostName x.x.x.x
   User ubuntu
   IdentityFile ~/Desktop/keys/2.pem
   PermitLocalCommand yes
   LocalCommand osascript -e "tell application \"Terminal\" to set background color of window 1 to {37655, 0, 0, -16373}"

答案 6 :(得分:2)

Xterm兼容的Unix终端具有用于设置背景和前景色的标准转义序列。我不确定Terminal.app是否共享它们;应该。

case $HOSTNAME in
    live1|live2|live3) echo -e '\e]11;1\a' ;;
    testing1|testing2) echo -e '\e]11;2\a' ;;
esac

第二个数字指定所需的颜色。 0 =默认,1 =红色,2 =绿色等。因此,当放入共享.bashrc时,此片段将为您提供实时服务器的红色背景和测试版本的绿色背景。您还应该添加这样的内容以在注销时重置背景。

on_exit () {
    echo -e '\e]11;0\a'
}
trap on_exit EXIT

编辑:谷歌出现了set the background color using AppleScript的方式。显然,这仅在与Terminal.app在同一台机器上运行时才有效。您可以使用几个包装函数来解决这个问题:

set_bg_color () {
    # color values are in '{R, G, B, A}' format, all 16-bit unsigned integers (0-65535)
    osascript -e "tell application \"Terminal\" to set background color of window 1 to $1"
}

sshl () {
    set_bg_color "{45000, 0, 0, 50000}"
    ssh "$@"
    set_bg_color "{0, 0, 0, 50000}"
}

连接到实时服务器时,您需要记住运行sshl而不是ssh。另一个选择是为ssh编写一个包装器函数,该函数扫描其已知实时主机名的参数并相应地设置背景。

答案 7 :(得分:1)

为什么不通过SSH登录时只更改shell提示符?通常有特定的shell变量: SSH_CLIENT SSH_CONNECTION SSH_TTY

答案 8 :(得分:1)

在服务器的/.bashrc

中设置终端颜色

我需要同样的东西,让我意识到我在Staging或Production服务器上,而不是在我的开发环境中,这很难说,特别是在Ruby控制台或其他东西时。

为了做到这一点,我在服务器的setterm文件中使用~./bashrc命令在连接时反转终端的颜色,并在退出时恢复颜色。

〜/ .bashrc中

# Inverts console colours so that we know that we are in a remote server. 
# This is very important to avoid running commands on the server by accident.
setterm --inversescreen on

# This ensures we restore the console colours after exiting.
function restore_screen_colours {
  setterm --inversescreen off
}
trap restore_screen_colours EXIT

然后我把它放在所有服务器的~/.bashrc文件中,以便我知道我的终端何时在远程服务器上。

另一个好处是,您的任何开发团队或devops团队都可以从中获益,而不会将其作为入职流程的一部分。

效果很好。

答案 9 :(得分:0)

您应该更改用户名和主机名的颜色。

将以下行添加到~/.bash_profile文件中:

export PS1=" \[\033[34m\]\u@\h \[\033[33m\]\w\[\033[31m\]\[\033[00m\] $ "

第一部分(紫色)是您正在寻找的。

预览: enter image description here

这是我喜欢的颜色。您可以通过更改ANSI颜色代码的m代码(例如34m)来自定义提示颜色的每个部分。

ANSI颜色代码列表:

  • 黑色:30米
  • 红色:31米
  • 绿色:32米
  • 黄色:33米
  • 蓝色:34米
  • 紫色:35米
  • 青色:36米
  • 白色:37米