Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance prompt function to show username/hostname in SSH connection #591

Closed
rkeithhill opened this issue Jun 27, 2018 · 4 comments
Closed

Comments

@rkeithhill
Copy link
Collaborator

rkeithhill commented Jun 27, 2018

As I've been using Windows 10 OpenSSH's sshd service and connecting to my Windows 10 system using ssh, I've been missing seeing the [username@hostname] info the PowerShell prompt on the remote system that I'm used to seeing when I ssh into a Linux box.

We could rectify that by adding the following to our prompt function (somewhere before the prompt suffix):

"$(if (Test-Path Env:\SSH_CONNECTION) {" [$([System.Environment]::UserName)@$([System.Environment]::MachineName)]"})"

A few notes, you get the remote system's PowerShell prompt when you A) ssh into the remote system with OpenSSH sshd running and configured with this subsystem:

Subsystem	powershell	C:/Program Files/PowerShell/6-preview/pwsh.exe -sshs -NoLogo -NoProfile

And no, I'm not quite sure why we get the posh-git prompt when the above specifies -NoProfile. Anyway, I've modified my posh-git prompt to this:

$global:GitPromptSettings.DefaultPromptBeforeSuffix.Text = '`n$([DateTime]::Now.ToString("MM-dd HH:mm:ss"))$(if (Test-Path Env:\SSH_CONNECTION) {" [$([System.Environment]::UserName)@$([System.Environment]::MachineName)]"})'

So that when I ssh into this system, I see this prompt:

C:\Users\hillr
06-27 09:51:19 [hillr@HILLR2] 1>

Note that when you use PS Remoting over SSH, PowerShell controls the prompt. In that case, you currently get just the hostname in the prompt:

06-27 09:51:57 8> Enter-PSSession -HostName hillr2
hillr@KEYSIGHT@hillr2's password:
[hillr2]: PS C:\Users\hillr\Documents>

But that is changing - see PowerShell/PowerShell#7156 & PowerShell/PowerShell#7191

So, should we consider covering this scenario with our prompt function or just add an example of how to customize the posh-git prompt if you want this feature?

@dahlbyk
Copy link
Owner

dahlbyk commented Jun 27, 2018

How is prompt handled for other PowerShell remoting situations?

@rkeithhill
Copy link
Collaborator Author

If I use WinRM as a transport instead of SSH, I get:

06-27 11:39:07 9> Enter-PSSession -ComputerName hillr2
[hillr2]: PS C:\Users\hillr\Documents>

That's pretty much the only other remoting scenario I can think off. Note: it is subtle but the parameter -ComputerName on Enter-PSSession causes the command to use WinRM while the -HostName parameter causes the command to use SSH.

@dahlbyk
Copy link
Owner

dahlbyk commented Jul 6, 2018

If the default behavior is to put the remote host before PS $pwd>, I'm inclined to follow suit with remote host information in DefaultPromptPrefix value. Is there a downside to using environment variables, compared with the implementation over in PowerShell/PowerShell#7191?

@rkeithhill
Copy link
Collaborator Author

OK, back from vacation … that's a great thought but when you are ssh'd into a remote system where PowerShell has been configured as the default shell, the Runspace.ConnectionInfo property is $null. That's because as far as PowerShell is concerned, it isn't involved in remoting. This is still useful info though in the (non-remoting) ssh scenario.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants