0% found this document useful (0 votes)
128 views

Secure FTP or SFTP Using Putty-PSFTP With C# (Open Source) and Task Scheduler Configuration

Uploaded by

Leonardo Forero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
128 views

Secure FTP or SFTP Using Putty-PSFTP With C# (Open Source) and Task Scheduler Configuration

Uploaded by

Leonardo Forero
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
ee)b) = PROJECT Secure FTP or SFTP using Putty/PSFTP with C# (Open Source) and Task Scheduler Configuration i Manas Kumar Mohanty % 29 May 2023 CPOL Simple open source code and technique for SFTP connectivity Download demo - 38.2 KB Introduction If you want to use SFTP connection for file transfer, then there are several paidlicense tools available in the market and some of them are very tough to work also. So, much complicated coding is required for simple file transfer using those tools. Recently, one of my clients changed their file transfer server FTP to SFTP and then | did research for a while to find the best way to manage without any paid tool with simple/less C# code. | have used PSFTP with this example. Now What is PSFTP? Most of us know about PuTTY. PuTTY is a free SSH, Telnet and Rlogin client for 32-bit Windows systems, PSFTP, the PuTTY SFTP client, is a tool for transferring files securely between computers using an SSH connection. Download PuTTY What You Need for SFTP? 1, Host Name or FTP Server 2. User Name 3. Password Example: Host Name or FTP Server: ftpdev.mycompany.com User Name: ftptestuser Password: ftptestpassword will use this information in my coding, however you need to replace it with a valid one, Let's Start Now Please follow the steps given below: Step 1 (Installing PuTTY) Download PuTTY and install in your computer (it's free). It will install at default path C:\Program Files (x86)\PUTTY. Step 2 (Authorizing PSFTP) This is a mandatory step, you need to do since it will accept SSH certificate first time from SFTP server. So, this is required before C# coding is done Follow either option 2 or b: * Option a: Click on, Windows start button -> All Programs -> PUTTY -> PSFTP ‘* Option b: Go to C:\Program Files (x86)\PuTTY, then double click on psftp.exe You will see a window like: Bose Then try to connect manually. Command 1: Open ftpdevmycompany.com Command 2: y 2 C:\Program Files (X86)\PUuTTY\psftp.exe a1 sftp. ors Sere ares eee ee otis Stars arenas ras eater cesar Stree OSU cer Teas APC ere im istel en ase ee eee f'you trust this host. enter y" to add the key to ieee eee Sema sty tf you want to carry on connecting just once, without eC Levees a ae eer eS CC ee abe UL cern See CeO cr Command 3: Enter user name C:\Program Files (x86)\PuTTY\psftp.exe Nopen host .nane” to connect et cached in the registry. You that the corver io the computor you PRs uriee rege bwenelc} Ree eee es cee ErCmare me Erarimancat Cre are eae eer crest eg occas ding the key to the cache, enter (RU Oates eae ee Coe UCC pirat Ktore key in cache? v Terres eect Command 4: Enter password C:\Program Files (x86)\PuTTV\psftp.exe pee re eee eae) Pet resre tery Cra ostrn neers retiree eer sti eae Ceemtees ted CUES Cet Rg ae eee TaN (GRU Ct seas Cores cr Cue eet fers CeCe te ee eet cicero rei cizes ia tae ate Cee nent Command 5: You can try some test commands to verify the SFTP files and folder list. Like Help; quit; 1s; get; put, etc, [more can be found at this link] Background We need to install PuTTY (free tool) for developing this program, however you do not need any prior experience of PUTTY. You need to just follow the provided instructions. Using the Code Create a simple console app in Microsoft Visual Studio. App.config: XML Program.cs cH using System; using System.Collections.Generic; using System. Ling; using System.Text; using System. Threading. Tasks; using System.Configuration; using System.105 using System. Diagnostics; namespace PSFTP_Deno © uns Program © seatic void Main(string{] args) ‘ /*€xanple of SFTP using PSFTP */ /* In this example, the Local files are placed in bin/debug folder since I am using AppDomain.CurrentDomain.BaseDirectory path, you can change it but make sure your run time code is accessible to that path. v /*Send file from Local folder to FTP server, send.sftp is a batch file, Look the command in the file from the solution ” PSFTP_TransferFile("send.sftp"); /*Receive file from FTP server to Local folder recieve. sftp is a batch file, Look the command in the file from the solution ” PSFTP_TransferFile("recieve.sftp"); /*delete file from FTP server delete. sftp is a batch file, Look the command in the file from the solution ” PSFIP_IransferFile("delete. sftp"); } private static void PSFTP_TransferFile(string batchFile) t try t Process myProcess = new Process()} myProcess.StartInfo.UseShel Execute myProcess.StartInfo.FileName = Configurationtanager .AppSettings[ "psftp"] .ToString(); var args = ConfigurationManager .AppSettings["user"].ToString() + "@" + ConfigurationManager .AppSettings[ "ftpserver"].ToString() + * pw ConfigurationManager .AppSettings[ "pass"].ToString() + " -batch -be -b " + AppDonain.CurrentDomain.BaseDirectory + "PSFTP-BatchFile\\" + batchFile; myProcess.StartInfo.Arguments = argsi; myProcess.StartInfo.CreateNoWindow = trues var abc = myProcess.Start(); myProcess.WaitForExit(); true; ? catch (Exception ex) t > Console.WriteLine("Error occurred : " + ex.Message); You can also download the complete solution from the link at the top of this article. How to Configure from Task Scheduler? Often, we use the SFTP in an automated environment for file transfer. You might find it difficult to run the above application from windows Task Scheduler if you will not configure it properly. \ will provide the steps for configuring in windows task scheduler. Please follow my instructions: In Windows, go to Start -> Control panel -> Administrative Tools -> Task Scheduler -> then right click on “Task Scheduler Library” -> Create Task. Step 1: General D Create Task x General Triggers Actions Conditions Settings wine [ese Location: V Author __MShDeveloper_ Desciiptioh: [SFTP testing Security options When running the task use the following user accaunt-—— Chengs User which MS\Developer — |——— haa flght soos =e ae oea © Run only when useris logged on @ Runwhether user islogged on ornct T De not store password The tak will only have accers to local computer resources. un with highest privileges Tl Hidden Configurer: | Windows Vista™, Windows Server™ 2008 y ok Cancel Step 2: Triggers Genesal Thg9= Actions Conditions Settings When you cete a task, you can spec the conditions that wil trigger the tesk ‘Tigges Details aw tigge Begin thetask: Ona schedule Settings @ oaty ees Recurevery: [I] aaye O Montaly Advanced stings News] | 1 Delay task fer uote (random del 1 hour Repesttzk every: [Thou Vv [1] Stop all unning tasks ot end of repetition duation LD) Septeskititrunstengerthaw Sys Cera 3) 02 18 23300M A Enabled Step 3: Actions onetime | see [a7 waar Ge [5am] Status synchronize cross time zane foradurstion of [I ay Cancel @ Greate Task Geer | Tigges | Actions [Conditions | Settings ‘When you create a task, you must specify the action that will occur when your task stats, ‘Aetion Detile New Action x You must specify what action this task will perform. Action: [Starta program = Settings Program/scipt c:\consoleapp\sftp-deme.exe Browse. ‘Add acguments (optional Star in (optional: e\conzoleappll [News Do not forget this step otherwise it will not work LI OK Points of Interest This article will help you in buil ling SFTP connectivity using PSFTP. You can add more features depending on your requirements. Your feedback is always welcome. History © 29" May, 2023: Initial version License This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL) Written By Manas Kumar Mohanty Technical Lead B United States This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming. Comments and Discussions 7 messages have been posted for this article Visit https://www.codeproject.com/Articles/1175314/Secure-FTP-or-SFTP-using-Putty-PSFTP-with- Csharp-O to post and view comments on this article, or click here to get a print view with messages. Permalink Article Copyright 2023 by Manas Kumar Advertise Mohanty Privacy Everything else Copyright © CodeProject, Cookies 1999-2023 Terms of Use Web01 2.8:2023-05-13:1

You might also like