macos 当我 ssh 到特定服务器时,如何让苹果终端窗口自动更改配色方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/157959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I make the apple terminal window auto change colour scheme when I ssh to a specific server
提问by Laurie Young
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?
我怎样才能让它自动检测我在某个地方 ssh 过,如果某个地方在特定列表中,请更改配色方案?
I want to update the Scheme of Terminal.app, not know how I would do this in a pure linux/unix env
我想更新 Terminal.app 的 Scheme,不知道我会如何在纯 linux/unix 环境中做到这一点
回答by Yurii Soldak
Put following script in ~/bin/ssh
(ensure ~/bin/
looked before /usr/bin/
in your PATH):
将以下脚本放入~/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 "
}
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 "$@"
The script above extracts host name from line "username@host" (it assumes you login to remote hosts with "ssh user@host").
上面的脚本从“username@host”行中提取主机名(假设您使用“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.
然后根据主机名设置红色背景(用于生产服务器)或绿色背景(用于所有其他服务器)。因此,您所有的 ssh 窗口都将带有彩色背景。
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").
我在这里假设您的默认背景是黑色,因此当您从远程服务器注销时,脚本会将背景颜色恢复为黑色(请参阅“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.
但请注意,此脚本不会跟踪从一台主机到另一台主机的 ssh 登录链。因此,如果您先登录到测试服务器,然后从它登录到生产,背景将是绿色的。
回答by Chris Page
A lesser-known feature of Terminal is that you can set the name of a settings profile to a command name and it will select that profile when you create a new terminal via either Shell > New Command…or Shell > New Remote Connection….
终端的一个鲜为人知的功能是,您可以将设置配置文件的名称设置为命令名称,当您通过Shell > New Command...或Shell > New Remote Connection...创建新终端时,它将选择该配置文件。
For example, duplicate your default profile, name it “ssh” and set its background color to red. Then use New Command…to run ssh host.example.com
.
例如,复制您的默认配置文件,将其命名为“ssh”并将其背景颜色设置为红色。然后使用New Command...运行ssh host.example.com
.
It also matches on arguments, so you can have it choose different settings for different remote hosts, for example.
例如,它还匹配参数,因此您可以让它为不同的远程主机选择不同的设置。
回答by bingles
Here's a combined solution based on a couple of existing answers that handles the exit. Also includes a little extra if you don't want to deal with 16 bit color values.
这是基于处理退出的几个现有答案的组合解决方案。如果您不想处理 16 位颜色值,还包括一些额外的内容。
This should be put in your ~/.bash_profile
这应该放在你的~/.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 - takes 4 (8 bit) color values
- set_bg_from_hex - takes 4 hex values. most of my color references I use are in hex, so this just makes it easier for me. It could be taken a step further to actually parse #RRGGBB instead of RR GG BB, but it works well for me.
- ssh - wrapping the default ssh command with whatever custom logic you want. The if statement is used to handle the exit to reset the background color.
- set_bg - 采用 4(8 位)颜色值
- set_bg_from_hex - 采用 4 个十六进制值。我使用的大部分颜色参考都是十六进制的,所以这对我来说更容易。实际解析 #RRGGBB 而不是 RR GG BB 可以更进一步,但它对我来说效果很好。
- ssh - 用你想要的任何自定义逻辑包装默认的 ssh 命令。if 语句用于处理退出以重置背景颜色。
回答by Maxim Yefremov
Combining answers 1and 2have the following:
Create ~/bin/ssh
file as described in 1with the following content:
使用以下内容创建~/bin/ssh
如1中所述的文件:
#!/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 [ "" != "Basic" ]; then
trap on_exit EXIT;
fi
osascript ~/Dropbox/macCommands/StyleTerm.scpt ""
}
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 "$@"
Make it executable: chmod +x ~/bin/ssh
.
使其可执行:chmod +x ~/bin/ssh
.
File ~/Dropbox/macCommands/StyleTerm.scpt
has the following content:
文件~/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
Words Basic, Homebrew, Ocean, Novel, Grass
are from mac os terminal settings cmd,:
回答by arnon cohen
Another solution is to set the colors strait in the ssh config file:
另一种解决方案是在 ssh 配置文件中设置颜色海峡:
inside ~/.ssh/config
在 ~/.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}"
回答by Milhous
You can set the $PS1 variable in your .bashrc.
您可以在 .bashrc 中设置 $PS1 变量。
red='\e[0;31m'
PS1="$\[${red}\]"
EDIT: To do this open the Terminal. Then say
编辑:要做到这一点,请打开终端。然后说
#touch .bashrc
You can then open .bashrc in textEdit or in TextWranglerand add the previous commands.
然后您可以在 textEdit 或TextWrangler 中打开 .bashrc并添加之前的命令。
回答by skymt
Xterm-compatible Unix terminals have standard escape sequences for setting the background and foreground colors. I'm not sure if Terminal.app shares them; it should.
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
The second number specifies the desired color. 0=default, 1=red, 2=green, etc. So this snippet, when put in a shared .bashrc, will give you a red background on live servers and a green background on testing ones. You should also add something like this to reset the background when you log out.
第二个数字指定所需的颜色。0=default, 1=red, 2=green, etc. 所以这个片段,当放在共享的 .bashrc 中时,会在实时服务器上给你一个红色背景,在测试服务器上给你一个绿色背景。您还应该添加类似的内容以在您注销时重置背景。
on_exit () {
echo -e '\e]11;0\a'
}
trap on_exit EXIT
EDIT: Google turned up a way to set the background color using AppleScript. Obviously, this only works when run on the same machine as Terminal.app. You can work around that with a couple wrapper functions:
编辑:谷歌提出了一种使用 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 "
}
sshl () {
set_bg_color "{45000, 0, 0, 50000}"
ssh "$@"
set_bg_color "{0, 0, 0, 50000}"
}
You'd need to remember to run sshl instead of ssh when connecting to a live server. Another option is to write a wrapper function for ssh that scans its arguments for known live hostnames and sets the background accordingly.
您需要记住在连接到实时服务器时运行 sshl 而不是 ssh。另一种选择是为 ssh 编写一个包装函数,该函数扫描已知实时主机名的参数并相应地设置背景。
回答by unexist
Why not just changing the shell prompt whenever you are logged in via SSH? There are usually specific shell variables: SSH_CLIENT, SSH_CONNECTION, SSH_TTY
为什么不在通过 SSH 登录时更改 shell 提示?通常有特定的 shell 变量:SSH_CLIENT、SSH_CONNECTION、SSH_TTY
回答by Joshua Pinter
Set the terminal colours in the server's /.bashrc
在服务器端设置终端颜色 /.bashrc
I needed the same thing, something to make me aware that I was on a Staging or Production server and not in my Development environment, which can be very hard to tell, especially when in a Ruby console or something.
我需要同样的东西,让我意识到我是在临时或生产服务器上而不是在我的开发环境中,这很难说,尤其是在 Ruby 控制台或其他东西中时。
To accomplish this, I used the setterm
command in my server's ~./bashrc
file to inverse the colours of the terminal when connecting and restore the colours when exiting.
为此,我使用setterm
服务器~./bashrc
文件中的命令在连接时反转终端的颜色并在退出时恢复颜色。
~/.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
I then put this in all the servers' ~/.bashrc
files so that I know when my terminal is on a remote server or not.
然后我把它放在所有服务器的~/.bashrc
文件中,这样我就知道我的终端何时在远程服务器上。
Another bonus is that any of your development or devops team get the benefit of this without making it part of the onboarding process.
另一个好处是,您的任何开发或 DevOps 团队都可以从中受益,而无需将其纳入入职流程。
Works great.
效果很好。
回答by Shayan Amani
You should change the color of username and host machine name.
您应该更改用户名和主机名称的颜色。
add the following line to your ~/.bash_profile
file:
将以下行添加到您的~/.bash_profile
文件中:
export PS1=" \[3[34m\]\u@\h \[3[33m\]\w\[3[31m\]\[3[00m\] $ "
The first part(purple colored) is what you're looking for.
在第一部分(紫色)是你在找什么。
This is my preferred colors. You can customize each part of prompt's color by changing m
codes (e.g. 34m
) which are ANSI color codes.
这是我喜欢的颜色。您可以通过更改ANSI颜色代码的m
代码(例如34m
)来自定义提示颜色的每个部分。
List of ANSI Color codes:
ANSI 颜色代码列表:
- Black: 30m
- Red: 31m
- Green: 32m
- Yellow: 33m
- Blue: 34m
- Purple: 35m
- Cyan: 36m
- White: 37m
- 黑色:30m
- 红色:31m
- 绿色:32m
- 黄色:33m
- 蓝色:34m
- 紫色:35m
- 青色:36m
- 白色:37m