Reputation: 6056
I am working to customize my PowerShell prompt, and I want it to show a special icon when the current session is over SSH.
Specifically, I want my prompt indicator to have a little airplane:
c:\foo> # connected to my local machine
c:\foo>ssh remote
#...
c:\bar✈> # connected to remote!
I am using the same prompt between my local and remote machines.
How can I detect that a given session is remote/over SSH?
Disclaimer: I work for Microsoft.
Upvotes: 2
Views: 533
Reputation: 6056
I do not have a general solution to any remote connection, but if you have a remote SSH session, the SSH_CLIENT
and SSH_CONNECTION
environment variables will be set:
>dir env:*ssh*
Name Value
---- -----
SSH_CONNECTION [redacted]
SSH_CLIENT [redacted]
So you can just test for that variable's existence in your prompt!
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"
This is referenced in 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.
Upvotes: 4