Lau*_*ung 44 macos terminal osx-leopard
当我进入一个远程生产服务器时,我希望我的终端窗口的颜色方案变成一些明亮可怕的颜色,最好是红色,以警告我,我正在触摸一个可怕的服务器.
我怎样才能让它自动检测到我在某个地方ssh'ed,如果某个地方在特定的列表上,改变颜色方案?
我想更新Terminal.app的Scheme,不知道如何在纯linux/unix环境中执行此操作
Yur*_*dak 50
将以下脚本放入~/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 "$@"
Run Code Online (Sandbox Code Playgroud)
上面的脚本从"username @ host"行中提取主机名(它假定您使用"ssh user @ host"登录到远程主机).
然后根据主机名称设置红色背景(用于生产服务器)或绿色背景(用于所有其他).因此,所有ssh窗口都将采用彩色背景.
我假设您的默认背景为黑色,因此当您从远程服务器注销时,脚本会将背景颜色恢复为黑色(请参阅"trap on_exit").
请注意,此脚本不会跟踪从一个主机到另一个主机的ssh登录链.因此,如果您首先登录测试服务器,然后从中登录到生产,则后台将为绿色.
Chr*_*age 30
终端的一个鲜为人知的功能是您可以将设置配置文件的名称设置为命令名称,并在通过Shell> New Command ...或Shell> New Remote Connection ...创建新终端时选择该配置文件.
例如,复制默认配置文件,将其命名为"ssh"并将其背景颜色设置为红色.然后使用New Command ...运行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
}
Run Code Online (Sandbox Code Playgroud)
创建~/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 [ "$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 "$@"
Run Code Online (Sandbox Code Playgroud)
让它可执行: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
Run Code Online (Sandbox Code Playgroud)
单词Basic, Homebrew, Ocean, Novel, Grass来自mac os终端设置cmd,:

您可以在.bashrc中设置$ PS1变量.
red='\e[0;31m'
PS1="$\[${red}\]"
Run Code Online (Sandbox Code Playgroud)
编辑:要做到这一点打开终端.然后说
#touch .bashrc
Run Code Online (Sandbox Code Playgroud)
然后,您可以在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}"
Run Code Online (Sandbox Code Playgroud)
/.bashrc我需要同样的东西,让我意识到我是在临时或生产服务器上,而不是在我的开发环境中,这可能很难说,特别是在 Ruby 控制台或其他东西中时。
为了实现这一点,我使用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
Run Code Online (Sandbox Code Playgroud)
然后我将其放入所有服务器的~/.bashrc文件中,以便我知道我的终端何时位于远程服务器上。
另一个好处是,您的任何开发或 DevOps 团队都可以从中受益,而无需将其纳入入职流程。
效果很好。
| 归档时间: |
|
| 查看次数: |
17178 次 |
| 最近记录: |