chr*_*ris 22 linux ssh terminal gnome-terminal ubuntu
我想根据主机名更改我的终端背景颜色。我的理由主要是有额外的方式来提醒自己我在哪个服务器上,所以我不会做一些愚蠢的事情。
如果我在本地机器上,有什么办法可以让它使用标准的茄子背景,然后如果我 ssh 进入生产网络服务器或我可以指定的其他主机,则更改为红色?
val*_*dil 10
您可以使用 ssh/config 的 localcommand 选项在使用别名时运行命令。我用
host hostname
user myusername
localcommand xtermcontrol --bg '#abc'
Run Code Online (Sandbox Code Playgroud)
这取决于 xtermcontrol 和您的术语是 xterm。大概还有其他术语的其他应用程序。
这种方法的唯一问题是它在您调用 ssh 时发生。没有什么可以撤消颜色更改。我已经通过在 ssh 周围包装一个函数来完成它,但这也有它的缺点。
function ssh() {
FG=$(xtermcontrol --get-fg)
BG=$(xtermcontrol --get-bg)
$(which ssh) "$@"
xtermcontrol --fg="$FG"
xtermcontrol --bg="$BG"
}
Run Code Online (Sandbox Code Playgroud)
似乎没有任何功能gnome-terminal可以从命令行向现有窗口添加新选项卡。但是有几个选项可以完成您想要的操作。
gnome-terminal为您将通过 SSH 连接的每个主机创建一个新配置文件。如果您只有几个经常连接的主机,这可能是最简单的。每个配置文件都可以定义不同的标题、前景色、背景色、自定义命令和其他设置。然后您可以使用File -> Open Tab所选配置文件打开一个新选项卡。
创建一个新的gnome-terminal,将被用于要连接到不同的SSH主机(在此基础上,每次打开一个新的窗口轮廓AskUbuntu答案是斯蒂法诺指出)。如果您经常连接到许多不同的主机,这会很好用。这将不允许您gnome-terminal仅根据背景/前景色区分连接到不同主机的不同窗口,但每个窗口将具有不同的标题。
gnome-terminal配置文件创建一个新配置文件 ( File -> New Profile)Default并将其命名为“RemoteHost”(注意,“RemoteHost”中没有空格以使命令更容易)。Title and Command选项卡下,更改:
Initial title: 到“远程主机”When terminal commands set their own titles: 到 Replace initial titleColors选项卡下,更改:
Use colors from system themeBuild-in schemes: 到 CustomText color:和Background color:您选择的颜色。请记住,某些命令(如ls)为其输出使用颜色,并且您不想选择会使输出难以阅读的颜色。Close按钮以保存您的新配置文件。gnome-terminal使用命令为每个远程 SSH 主机打开一个新窗口gnome-terminal --window-with-profile=RemoteHost -t "Some Remote SSH Host" -x ssh user@somehost。该-t选项设置gnome-terminal窗口标题,该-x选项在终端中执行命令行的其余部分。您甚至可以alias缩短总击键次数。我发现这个博客条目与使用下面的脚本xdotool和wmctrl命令(他们没有默认安装在Ubuntu上,所以你可能需要先安装它们)使用gnome-terminal Ctrl+ Shift+t键盘快捷键在当前打开一个新标签gnome-terminal窗口. 可以修改它以打开具有特定配置文件的新选项卡并为您执行一些命令。
#!/bin/bash
# Path: /usr/local/bin/gnome-terminal
if [ "x$*" != "x" ]; then
/usr/bin/gnome-terminal "$@"
else
pgrep -u "$USER" gnome-terminal | grep -qv "$$"
if [ "$?" == "0" ]; then
WID=`xdotool search --class "gnome-terminal" | head -1`
xdotool windowfocus $WID
xdotool key ctrl+shift+t
wmctrl -i -a $WID
else
/usr/bin/gnome-terminal
fi
fi
Run Code Online (Sandbox Code Playgroud)
你可以发挥创意并尝试其他一些事情。
这个超级用户答案基本上使用了一些“脚本功能”杂技演员来创建一个gnome-terminal用于打开新窗口的临时配置文件。它可能会被修改以供您使用。
每当您通过 SSH 连接到远程主机时,您都可以使用此StackOverflow Q&A和更多“script-fu”杂技演员来动态更改gnome-terminal标题。它不会像背景/前景色变化那样突出,但它始终比标准Terminal标题更好。
这将适用于 Gnome,如果您愿意为每个 ssh 会话使用一个新的 gnome-terminal 窗口。
创建一个名为“远程”的新配置文件(具有不同的背景颜色)
将以下内容插入.bash_aliases, 或.bashrc
### add to .bash_aliases, for differentiating between local and remote hosts
sshhelper() {
gnome-terminal --window-with-profile=Remote -x bash -c "ssh $1";
}
alias sshc=sshhelper
Run Code Online (Sandbox Code Playgroud)现在使用“远程”配置文件sshc remote-machine打开一个新的gnome-terminal 会话。这将区分本地和远程配置文件。
要容纳多个配置文件/主机,请创建多个配置文件并在其中放置类似的内容.bash_aliases:
### add to .bash_aliases, for differentiating between multiple remote hosts
sshhelper() {
HOST=`echo $1 | cut -d'@' -f2`
case $HOST in
Production ) PROFILE="Red" ;;
Test ) PROFILE="Green" ;;
# ... if you have more cases ...
*) PROFILE="Default" ;;
esac
gnome-terminal --window-with-profile=$PROFILE -x bash -c "ssh $1";
}
# alias ssh=sshhelper # this will "override" the ssh command, but may break other stuff!
alias sshc=sshhelper
Run Code Online (Sandbox Code Playgroud)
现在sshc Production将使用“红色”配置文件sshc Test打开一个新会话窗口,使用“绿色”配置文件打开一个新会话窗口,其他主机将使用“默认”配置文件。