让终端根据 ssh 主机名使用不同颜色的简单方法是什么?

发布于 2024-08-14 12:34:08 字数 583 浏览 8 评论 0原文

在 Windows 中使用 putty,您可以保存连接到特定主机的会话并使用特定的文本颜色...这对我来说非常有用,因为我与一堆远程主机一起工作,我想知道是否有(必须是)一种让终端(在 Snow Leopard 中)模拟这种行为的方法。

我想知道我会怎样 1. 保存连接(例如[电子邮件受保护])并建立该连接始终以某种文本颜色打开(例如#00ff00) 2. 理想情况下,让任何终端窗口检测它所在的主机并相应地更改其颜色。因此,如果我在常规终端环境中并成功发出 ssh [email protected] ],它会自动将该终端窗口(或选项卡)的文本颜色更改为#00ff00

请告诉我,谢谢!

Using putty in windows, you can save sessions that connect to a certain host and use a certain text color...this was super useful for me since I work with a bunch of remote hosts and I'm wondering if there is (there must be) a way to get Terminal (in Snow Leopard) to emulate this behavior.

I'm wondering how I would
1. Save a connection (e.g. [email protected]) and have that connection always open with a certain text color (e.g. #00ff00)
2. Ideally, have any terminal window detect what host it was in and change its color accordingly. So if I was in my regular Terminal environment and issued a successful ssh [email protected], it would automatically change the text color of that terminal window (or tab) to #00ff00

Let me know, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘香 2024-08-21 12:34:08

好的,如果您坚持从命令行调用 ssh,这里有一个方法可以解决这个问题:编写一个 shell 脚本并将其保存为 colorssh.sh。当它运行时,它会查看匹配主机的参数并适当地设置活动终端窗口的颜色。然后它调用真正的 ssh,传递这些参数。当 ssh 返回执行脚本时,它将颜色设置回正常。

由于您可能希望继续输入 ssh 而不是 colorssh.sh,因此您可以在 .profile 中设置别名。

至于剧本本身?这是代码:

#!/bin/bash

function setTerminalColors {
    osascript \
        -e "tell application \"Terminal\"" \
        -e "tell selected tab of front window" \
        -e "set normal text color to $1" \
        -e "set background color to $2" \
        -e "end tell" \
        -e "end tell"
}

for ARG in $*
do
    case "$ARG" in
        host.example.com)
        [email protected])
            setTerminalColors "{0,65535,65535}" "{65535,0,0}"
            ;;
        [email protected])
            setTerminalColors "{65535,65535,0}" "{0,65535,0}"
            ;;
    esac
done

ssh $*

# back to normal
setTerminalColors "{0,0,0}" "{65535,65535,65535}" 

您必须编辑脚本才能添加新的主机/颜色组合。

请注意,颜色必须指定为 0-65535 范围内整数的 RGB 三元组。我知道,很奇怪,对吧?

从技术上讲,AppleScript 部分更改了已弃用的属性。您应该通过其“设置集”属性来更改窗口的颜色,但我怀疑这会更改使用该设置集的所有窗口,而不仅仅是当前的窗口。

另外,此脚本假定白底黑字是您的“正常”设置。如果不是这种情况,您可以更改脚本以在运行之前保存当前值或使用默认设置集中的颜色。

OK, if you insist on invoking ssh from the command line, here's something that should do the trick: write a shell script and save it somewhere as colorssh.sh. When it runs, it looks at its arguments for a matching host and sets the active terminal window's colors appropriately. Then it invokes the real ssh, passing along those arguments. When ssh returns execution to the script, it sets the colors back to normal.

Since you probably want to keep typing ssh instead of colorssh.sh, you can set an alias in your .profile.

As for the script itself? Here is teh codez:

#!/bin/bash

function setTerminalColors {
    osascript \
        -e "tell application \"Terminal\"" \
        -e "tell selected tab of front window" \
        -e "set normal text color to $1" \
        -e "set background color to $2" \
        -e "end tell" \
        -e "end tell"
}

for ARG in $*
do
    case "$ARG" in
        host.example.com)
        [email protected])
            setTerminalColors "{0,65535,65535}" "{65535,0,0}"
            ;;
        [email protected])
            setTerminalColors "{65535,65535,0}" "{0,65535,0}"
            ;;
    esac
done

ssh $*

# back to normal
setTerminalColors "{0,0,0}" "{65535,65535,65535}" 

You'll have to edit the script to add new host/color combinations.

Note that colors must be specified as an RGB triplet of integers in the range 0-65535. I know, weird, right?

Technically, the AppleScript portion changing deprecated properties. You're supposed to change a window's colors via its "settings set" property, but I suspect that would change all windows using that settings set, not just the current one.

Also, this script assumes that black on white is your "normal" setting. If that's not the case you could change the script to save the current values before running or use the colors from the default settings set.

迷途知返 2024-08-21 12:34:08

在终端中,您可以定义具有不同窗口背景颜色、不透明度等的配置文件。此外,在配置文件中您可以指定启动命令。您可以使用“ssh me@thathost”启动命令为使用的每个主机设置不同的配置文件,但这仅适用于新窗口。通过 Shell 可以轻松获取配置文件 ->新窗口。

In terminal you can define profiles with different window background color, opacity, etc. Also in profiles you can specify a startup command. You could set up a different profile for each host you use with a startup command of "ssh me@thathost", but this would only work for new windows. Profiles are easy to get to via Shell -> New Window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文