SFTP file download works for a while and then shows "permission denied, password" error

I’m writing a short C# (.NET) script that can download (and later upload) all files in my shared web hosting account to a Windows folder with SFTP protocol using SSH.NET library. It works for awhile and then out of the blue it starts giving me the following errors:

First “the client is already connected”, and then if I run the script again it gives me “permission denied (password)” error.

Any idea why is this happening?

Nevermind, guys. I figured it out. It was an issue in my .NET code.

PS. Moderator – I don’t know how to delete this question.

I can delete your question, but to be honest I’d rather have you share your code both the ‘wrong’ and the fixed one :slight_smile: I don’t know much of .NET and I’m really curious about it. I’m sure other people would appreciate it too.

PS No problem if you prefer not to share your code. Send me a message and I’ll remove this question.

Oh, sorry, I didn’t see your reply…

So sure, I can elaborate more on what I was doing.

Well, the code itself may be beyond the scope of this forum. (I don’t want to bore anyone.) If you’re interested, SSH.NET is an open source C# library. You can get its source code. See link in my original post. And as for how this can be used for an SFTP connection/download script, then here’s a good couple examples on Stackoverflow. I used something very similar to that.

As for my own script that I wrote, it turns out that adding a plaintext password as a Windows command line parameter is not a good idea. Windows treats some characters as escape sequence, or something of that nature. (This goes back to DOS time, I believe) that can screw things up. In my case my script was accepting an SFTP connection password as a parameter as such:

pwd “h8dO73h4Rh^6SGH3eDf”

but that caret symbol, or ^, was a special one for Windows, so for some reason it (or maybe Visual Studio) was either removing it, or adding something else to it, so the password was distorted. And that was the reason I was getting all those errors.

So I was able to correct it in my script by scrambling it & encoding it with base64, which would not contain any illegal characters for a Windows command line.

Lastly if you’re interested here’s the resulting tool I made. (Idk if I can post links here, if not, then remove it.) I called it FtpsTransfer. The premise is that it can download or upload all your files, securely via SSH or ftps, in one command line call. Which is good for weekly backups, or when you move your files from one webhost to another. It also makes downloaded files compatible with Windows file system. For instance, Windows is not case sensitive, when Linux is. Additionally, if you have two directories, say “Pay” and “pay” in one directory in your Linux file system, Windows won’t be able to handle them, so if you connect via any FTP client and download those into one folder, Windows will either overwrite one on top of another or will somehow else butcher it up. I couldn’t find any software that will handle this situation, thus I had to write my own tool. So if anyone else wants to use it too, you’re more than welcome.

1 Like

Wow, this is great. Thanks for sharing!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.