gpt4 book ai didi

powershell - 如何确定当前的PowerShell session 是否通过SSH?

转载 作者:行者123 更新时间:2023-12-02 14:27:42 24 4
gpt4 key购买 nike

我正在自定义PowerShell提示符,并且希望当前 session 通过SSH时它显示一个特殊图标。

具体来说,我希望我的提示指示器有一架飞机:

c:\foo> # connected to my local machine
c:\foo>ssh remote
#...
c:\bar✈> # connected to remote!

我在本地计算机和远程计算机之间使用相同的提示。

如何检测给定的 session 是远程/通过SSH?

最佳答案

对于任何远程连接,我都没有通用的解决方案,但是如果您有远程SSH session ,则会设置SSH_CLIENTSSH_CONNECTION环境变量:

>dir env:*ssh*

Name Value
---- -----
SSH_CONNECTION [redacted]
SSH_CLIENT [redacted]

因此,您可以在提示中测试该变量的存在!

function Test-IsRemote {
[CmdletBinding()]
param()

return Test-Path env:SSH_CLIENT;
}

# ... in your prompt:
if (Test-IsRemote) {
Microsoft.PowerShell.Utility\Write-Host "✈ " -NoNewLine -ForegroundColor "DarkGray"
}

# ...
Microsoft.PowerShell.Utility\Write-Host ">" -NoNewLine -ForegroundColor "DarkGray"

这在 a guide for customizing your ZSH prompt中引用:

Note: you can argue that the hostname in the prompt is useful when you frequently have multiple terminal windows open to different hosts. This is true, but then the prompt is defined by the remote shell and its configuration files on the remote host. In your configuration file, you can test if the SSH_CLIENT variable is set and show a different prompt for remote sessions. There are more ways of showing the host in remote shell sessions, for example in the Terminal window title bar or with different window background colors.

关于powershell - 如何确定当前的PowerShell session 是否通过SSH?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60645123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com