Solved

Secure FTP connection

Posted on 2004-09-27
17
533 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
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

803 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