Solved

Secure FTP connection

Posted on 2004-09-27
17
536 Views
Last Modified: 2008-01-09
I am trying to build a component to get files from a secure Ftp server (also push it on the server) . The one and only port open onthe server is 22. I tried using FTP libraries that are recommended here, but none worked correctly. The last FTP lib. I tried is FTPFactory.cs. Please advice...
0
Comment
Question by:web121
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
17 Comments
 
LVL 3

Expert Comment

by:KeithWatson
ID: 12164516
SFTP requires an SFTP client; the class you used looks like a standard FTP client. Unfortunately it doesn't look as though there are any free component libraries to talk to an SFTP server; however, there are a couple of commerical components available:

http://www.weonlydo.com/index.asp?showform=SFTP
http://www.programmersheaven.com/zone28/cat1167/27417.htm

There are also a number of free C libraries available, which you could use. These are documented in this thread:

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20938665.html

If it's OK to shell out to an external program to do the work, then SecureFx is very good and cheap:

http://www.vandyke.com/products/securefx/
0
 

Author Comment

by:web121
ID: 12164636
Thanks for the quick response. I am trying to find a C# library and I don't want to use commercial components.
0
 

Author Comment

by:web121
ID: 12228180
After searching for it, I couldn't find a C# library that talks to SFTP server. Now I am planning to execute a SSH client from the command line using C#.  I am using System.Diagnostics.Process to attach to a cmd.exe, any ideas how can I interact with cmd.exe using Process class to run SSH client and run ftp commands on it. SSH client I am planning to use is puTTy. How about creating batch files and executing them accordly sounds? Any alternatives??

Thanks.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 3

Expert Comment

by:KeithWatson
ID: 12229791
A batch file sounds fine; you can also just fire commands into cmd.exe on the command line; so:

cmd.exe /c notepad.exe

will start a command shell, execute notepad and then once notepad is complete (you'd have to kill it in this instance, close the command shell).

You can chain commands together like this as well:

cmd.exe /c dir & pause & dir & pause

So this could save you writing a batch file, but this would also work.

0
 

Author Comment

by:web121
ID: 12231705
Code is as follows, it executes in a webserver as a Webservice under ASPNet account. Cmd.exe starts and exits under ASPNet account with no problem. I need to tell cmd.exe to run the batchfile.bat (that's my file name) somehow. There is no error on the batch file, if I run it manually  it copies the file and closes the cmd.exe. FTP folder has full controll for ASPNet machine account.

//objProcess.StartInfo.Arguments = "/c batchfile.bat";  ??? I tried this but did not work.!! also tried
objProcess.StartInfo.FileName = @"C:\WINDOWS\system32\cmd.exe /c batchfile.bat";


Thanks.


CS code
=====================================================
bool bIsFinished = false;
Process objProcess = new Process();

objProcess.StartInfo.FileName = @"C:\WINDOWS\system32\cmd.exe";
objProcess.StartInfo.UseShellExecute = false;
objProcess.StartInfo.RedirectStandardInput = true;
objProcess.StartInfo.RedirectStandardOutput = true;
objProcess.StartInfo.RedirectStandardError = true;

objProcess.Start();
//objProcess.WaitForExit();
bIsFinished = objProcess.WaitForExit(10000);//10 seconds
=======================================================


batchfile.bat
==========================================================
c:
ftp -s:batchfile.scr
exit
==========================================================

batchfile.scr
===========================================================
open ftpservername
username
password
put Errorlog.log
quit
===========================================================
0
 

Author Comment

by:web121
ID: 12232587
I am trying to get it to work with FTP first. Then I am planning on moving it to sftp.
0
 

Author Comment

by:web121
ID: 12240971
solved the problem for ftp

                                                System.Diagnostics.ProcessStartInfo psi =   new System.Diagnostics.ProcessStartInfo(@"C:\PSFTPbatchfile.bat");
                  psi.RedirectStandardOutput = true;
                  psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
                  psi.UseShellExecute = false;
                  System.Diagnostics.Process objProcess;
                  objProcess = System.Diagnostics.Process.Start(psi);
                  System.IO.StreamReader myOutput = objProcess.StandardOutput;
                  objProcess.WaitForExit(5000);
                  if (objProcess.HasExited)  
                  {  
                        string output = myOutput.ReadToEnd();
                  }

but same code does not work for sftp. I am using psftp (which is putty ssh comandline)
again batch file work fine when I am running it manually from cmd.exe but when it is run like above it does not copy the file, and cmd.exe does not exit if I look at the task manager. Any ideas ?? Everything is in the same directory c:\

.bat
=========================
c:
chdir c:\
psftp ftp01 -l admin -pw adminpwd -b PSFTPbatchfile.scr
exit

.scr
=========================
 cd home
put errorlog.log
quit
0
 
LVL 3

Expert Comment

by:KeithWatson
ID: 12243834
I've never run putty via the command line; however, one thing you might try is sending the batch file is as a parameter to cmd.exe and see if that makes a difference, so instead of executing C:\PSFTPbatchfile.bat, try executing:

cmd /c call C:\PSFTPbatchfile.bat

You would need to set these in the ProcessStartInfo object separately, i.e.

...ProcessStartInfo("cmd.exe", "/c C:\\PSFTPbatchfile.bat");

Incidentally, instead of waiting 5 seconds and then checking to see if the process has exited you can call:

process.WaitForExit();

and it will block until completion.
0
 

Author Comment

by:web121
ID: 12248798
Here is the actual problem when doing sftp with ASPNET user. First time connecting to the sftp server with putty commandline it asks you to response to the message below. You can not automate the response because it is a security concern. If you say yes to the message below it cashes it to the registry and never ask you this again. On the web app that I am doing process is started from a ASPNET user, Since I can not login to the machine as an ASPNET user I can not say "yes" to the message below. Any ideas on this issue??

I tried ProcessStartInfo("cmd.exe", "/c C:\\PSFTPbatchfile.bat"); there is no difference

The server's host key is not cached in the registry. You
have no guarantee that the server is the computer you
think it is.
The server's dss key fingerprint is:
----------------------------------------------
If you trust this host, enter "y" to add the key to
PuTTY's cache and carry on connecting.
If you want to carry on connecting just once, without
adding the key to the cache, enter "n".
If you do not trust this host, press Return to abandon the
connection.
0
 
LVL 3

Expert Comment

by:KeithWatson
ID: 12251865
Could it be the permissions on the website itself? The default setting for ASP.NET websites you can see by right-clicking the web application in ASP.NET, selecting properties and then the "Home Directory" tab. Towards the bottom there is a combo box with "Execute Permissions" next to it; this defaults to "Scripts Only". You could try changing this to "Scripts and Executables".

I'm afraid I know very little about the Putty SFTP client although I do remember the message you point out. I think it's just saying that you've supplied the public key but that it could have come from anywhere, so make sure you know where the public key came from, i.e. make sure you're connecting to the computer you think you are.

Hope that helps,

Keith.
0
 

Author Comment

by:web121
ID: 12251957
I solved the problem.. :-) by using impersonation...


Change the webconfig of the application (most likely u are going to add this line) in <system.web>

<identity impersonate="true" userName="domain\username" password="pwd" />
0
 
LVL 3

Expert Comment

by:KeithWatson
ID: 12251978
Good stuff.

Glad you got it working.
0
 

Author Comment

by:web121
ID: 12252015
Keith I would like to give you some points because u were really trying to help. How can I do this? (  Not 300 but 100 ??)
0
 
LVL 3

Accepted Solution

by:
KeithWatson earned 300 total points
ID: 12252031
Oh, I don't know, not been around here long... Don't worry about it though. Glad you got the problem sorted.
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question