51

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

如何让它自动检测到我在某处进行了 ssh,如果某处在特定列表中,请更改配色方案?

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

4

10 回答 10

53

将以下脚本放入~/bin/ssh(确保之前在您的 PATH~/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 "$@"

请记住通过运行使脚本可执行chmod +x ~/bin/ssh

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

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

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

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

于 2008-10-03T10:12:06.057 回答
36
于 2013-09-11T12:47:33.753 回答
9

另一种解决方案是在 ssh 配置文件中设置颜色限制:

在 ~/.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}"
于 2015-12-29T15:09:57.780 回答
8

这是一个基于处理退出的几个现有答案的组合解决方案。如果您不想处理 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 语句用于处理退出以重置背景颜色。
于 2015-09-24T12:05:32.410 回答
8

结合答案12有以下内容:

按照1~/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

文字Basic, Homebrew, Ocean, Novel, Grass来自mac os终端设置cmd,终端型材

于 2016-09-14T11:42:58.827 回答
5

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

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

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

#touch .bashrc

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

于 2008-10-01T14:44:45.147 回答
3

在服务器端设置终端颜色/.bashrc

我需要同样的东西,让我意识到我在一个暂存或生产服务器上,而不是在我的开发环境中,这很难说清楚,尤其是在 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 团队都可以从中受益,而无需将其作为入职流程的一部分。

效果很好。

于 2018-04-28T16:22:38.337 回答
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=default, 1=red, 2=green 等等。所以这个片段,当放在一个共享的 .bashrc 中时,会给你实时服务器的红色背景和测试服务器的绿色背景。您还应该添加类似这样的内容以在您注销时重置背景。

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

编辑:谷歌找到了一种使用 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 编写一个包装函数,该函数扫描其参数以查找已知的实时主机名并相应地设置背景。

于 2008-10-01T15:21:13.407 回答
1

为什么不在通过 SSH 登录时更改 shell 提示?通常有特定的 shell 变量:SSH_CLIENTSSH_CONNECTIONSSH_TTY

于 2008-10-01T14:44:38.533 回答
0

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

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

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

第一部分(紫色)是您要查找的内容。

预览: 在此处输入图像描述

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

ANSI 颜色代码列表:

  • 黑色:30m
  • 红色:31m
  • 绿色:32m
  • 黄色:33m
  • 蓝色:34m
  • 紫色:35m
  • 青色:36m
  • 白色:37m
于 2018-01-13T23:36:52.613 回答