我怎么做苹果终端窗口自动改变颜色方案时,我ssh到一个特定的服务器

StackOverflow https://stackoverflow.com/questions/157959

  •  03-07-2019
  •  | 
  •  

当我ssh到一个远程生产服务器我会喜欢的颜色方式的我的终端窗口改变的东西brigh和可怕的,最好是红色警告我,我摸一个活的可怕的服务器。

我怎么能让它自动检测,我ssh'ed地方,如果这地方是在一个特定的清单,改变颜色方案?

我想要更新的方案的终端。应用程序,不知道我怎么会这样做在一个纯粹的linux/unix env

有帮助吗?

解决方案

将以下脚本放在〜/ bin / ssh 中(确保〜/ bin / 在PATH中的 / 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登录链。因此,如果您先登录测试服务器,然后从中登录生产,背景将为绿色。

其他提示

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

例如,复制您的默认个人资料,将其命名为“ ssh”并将其背景颜色设置为红色。然后使用新命令… 运行 ssh host.example.com

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

这里有一个合并的解决方案基于几个现有的答案,处理退出。还包括一些额外的如果你不想要处理的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(8bit)的颜色值
  • set_bg_from_hex-4六角的价值观。我的大多数色引用我使用的是六角的,因此这只是使得它对我来说更容易。它可以采取的一个步骤进一步实际分析#RRGGBB而不是RR GG BB,但它的工作。
  • ssh包默认ssh命令的任何定义的逻辑,你想要的。如果声明是用于处理出口到重置的背景的颜色。

结合答案 1 2 具有以下内容:

按照 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

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

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

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

#touch .bashrc

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

另一个解决方案是在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}"

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

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

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

在服务器的 / .bashrc

中设置终端颜色

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

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

〜/ .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团队都可以从中获益,而不会将其作为入职流程的一部分。

效果很好。

你应该改变颜色的用户名和主机名称。

添加下面的线给你 ~/.bash_profile 文件:

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

第一部分 (紫色的)是你在找什么。

预览: enter image description here

这是我的首选的颜色。你可以定制的每个部分的提示的颜色变化 m 代码(例如 34m)是ANSI颜色的代码。

列表ANSI颜色代码:

  • 黑色:30分钟
  • 红色:31m
  • 绿色:32米
  • 黄:33米
  • 蓝色:34m
  • 紫色:小时35分钟
  • 青:36米
  • 白:37米
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top