Solved

Secure FTP connection

Posted on 2004-09-27
17
527 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
  • 8
  • 6
17 Comments
 
LVL 3

Expert Comment

by:KeithWatson
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 3

Expert Comment

by:KeithWatson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 3

Expert Comment

by:KeithWatson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Good stuff.

Glad you got it working.
0
 

Author Comment

by:web121
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

728 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now