Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 539
  • Last Modified:

Secure FTP connection

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
web121
Asked:
web121
  • 8
  • 6
1 Solution
 
KeithWatsonCommented:
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
 
web121Author Commented:
Thanks for the quick response. I am trying to find a C# library and I don't want to use commercial components.
0
 
web121Author Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
KeithWatsonCommented:
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
 
web121Author Commented:
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
 
web121Author Commented:
I am trying to get it to work with FTP first. Then I am planning on moving it to sftp.
0
 
web121Author Commented:
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
 
KeithWatsonCommented:
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
 
web121Author Commented:
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
 
KeithWatsonCommented:
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
 
web121Author Commented:
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
 
KeithWatsonCommented:
Good stuff.

Glad you got it working.
0
 
web121Author Commented:
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
 
KeithWatsonCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now