0

tl;dr in powershell, how can I detect a login is specifically SFTP and not SSH or local?

On Windows 10 I have the built-in OpenSSH service running. For SFTP protocol connections, the server uses the typical sftp-server, i.e. the sshd_config reads

Subsystem    sftp  sftp-server.exe

The default login shell is powershell.exe. That default powershell.exe login sources $PROFILE which I have modified to print some helpful startup information. However, printing that helpful startup information causes SFTP connections to fail. e.g. when I connect WinSCP to the OpenSSH service it fails to connect with popup error that reads

Received too large (1127898197 B) SFTP packet. Max supported packet size is 1024000 B.

Cannot initialize SFTP protocol. Is the host running an SFTP server?

WinSCP SFTP login failure

I found via experimentation that this error is due to messages printed when the Powershell instance executes $PROFILE. Or to say in other words, if I prevent any Write-Host calls in the $PROFILE then WinSCP successfully completes the connection. Also, it does not appear due to printing too much. It appears that printing anything from $PROFILE causes WinSCP connections to fail. Also, similar errors happen when using other SFTP clients like lftp and termscp so it's not an issue with WinSCP.

I'd like to have the powershell script at $PROFILE break early when it detects an SFTP login (and not call Write-Host). I'd like the $PROFILE run to complete for all other logins (SSH, local, etc.).

Is there a way to detect an SFTP login within powershell?


Original post on StackOverflow (closed).

1 Answers1

0

you can add a conditional statement in your $PROFILE script to skip any output commands if the session is detected as SFTP. For example, you can use something like this:

# Check if this is an SFTP session
$IsSFTP = $env:SSH_ORIGINAL_COMMAND -eq "sftp-server.exe"

Execute the rest of the $PROFILE script only if this is not an SFTP session

if (-not $IsSFTP) { # Your original $PROFILE script goes here Write-Host "Welcome to PowerShell" # ... }

Ace
  • 812