65.9K
CodeProject 正在变化。 阅读更多。
Home

使用C# (开源) 和任务计划程序配置通过Putty/PSFTP实现安全FTP或SFTP

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.43/5 (7投票s)

2023年5月29日

CPOL

3分钟阅读

viewsIcon

31751

downloadIcon

717

用于SFTP连接的简单开源代码和技术

引言

如果您想使用SFTP连接进行文件传输,那么市场上就有一些付费/许可工具,而且其中一些工具也难以使用。因此,使用这些工具进行简单的文件传输需要非常复杂的编码。

最近,我的一位客户将其文件传输服务器从FTP更改为SFTP,然后我研究了一段时间,以找到在没有付费工具的情况下,用简单/较少的C#代码进行管理的最佳方法。 我在此示例中使用了PSFTP。

现在什么是PSFTP?

我们大多数人都知道PuTTY。PuTTY是适用于32位Windows系统的免费SSH、Telnet和Rlogin客户端。PSFTP,PuTTY SFTP客户端,是一种使用SSH连接在计算机之间安全传输文件的工具。

您需要什么来使用SFTP?

  1. 主机名或FTP服务器
  2. 用户名
  3. 密码

示例:主机名或FTP服务器:ftpdev.mycompany.com
用户名:ftptestuser
密码:ftptestpassword

我将在我的代码中使用此信息,但是您需要将其替换为有效的密码。

现在开始吧

请按照以下步骤操作

步骤 1 (安装PuTTY)

下载PuTTY 并在您的计算机上安装(它是免费的)。它将安装在默认路径C:\Program Files (x86)\PuTTY

步骤 2 (授权 PSFTP)

这是一个强制性步骤,您需要执行此操作,因为它将首次从SFTP服务器接受SSH证书。因此,在完成C#编码之前,这是必需的。

请按照选项ab

  • 选项 a:单击“Windows 开始”按钮 -> “所有程序” -> “PuTTY” -> “PSFTP”
  • 选项 b:转到C:\Program Files (x86)\PuTTY,然后双击psftp.exe

您将看到一个类似

然后尝试手动连接。

命令 1:打开ftpdev.mycompany.com

命令 2:y

命令 3:输入用户名

命令 4:输入密码

命令 5:您可以尝试一些测试命令来验证 SFTP 文件和文件夹列表。

例如 Help; quit; ls; get; put 等 [更多信息可以在 此链接中找到]。

背景

我们需要安装PuTTY(免费工具)来开发此程序,但是您不需要任何PuTTY的先前经验。 您只需要按照提供的说明操作即可。

Using the Code

在Microsoft Visual Studio中创建一个简单的控制台应用程序。

App.config:

  <appSettings>
    <!--psftp path-->
    <add key ="psftp"  value="C:\\Program Files (x86)\\PuTTY\\psftp.exe"/>

    <!--ftp server and credentials-->
    <add key ="user"  value="ftptestuser"/>
    <add key ="pass"  value="ftptestpassword"/>
    <add key ="ftpserver"  value="ftpdev.mycompany.com"/>
  </appSettings>

Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.IO;
using System.Diagnostics;

namespace PSFTP_Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            /*Example 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.
            */

            /*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            
             */
            PSFTP_TransferFile("delete.sftp");
        }
        private static void PSFTP_TransferFile(string batchFile)
        {
            try
            {
                Process myProcess = new Process();
                myProcess.StartInfo.UseShellExecute = true;
                myProcess.StartInfo.FileName = 
                          ConfigurationManager.AppSettings["psftp"].ToString();
                var args1 = ConfigurationManager.AppSettings["user"].ToString() + "@" 
                            + ConfigurationManager.AppSettings["ftpserver"].ToString() + 
                            " -pw " + 
                              ConfigurationManager.AppSettings["pass"].ToString() + 
                            " -batch -be -b " + AppDomain.CurrentDomain.BaseDirectory + 
                            "PSFTP-BatchFile\\" + batchFile;
                myProcess.StartInfo.Arguments = args1;
                myProcess.StartInfo.CreateNoWindow = true;
                var abc = myProcess.Start();
                myProcess.WaitForExit();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error occurred : " + ex.Message);
            }
        }
    }
}

您还可以从本文顶部的链接下载完整的解决方案。

如何从任务计划程序配置?

通常,我们在自动化环境中使用SFTP进行文件传输。如果您没有正确配置,您可能会发现从Windows 任务计划程序运行上述应用程序很困难。

我将提供在Windows任务计划程序中配置的步骤。 请按照我的说明操作

在 Windows 中,转到 开始 -> 控制面板 -> 管理工具 -> 任务计划程序 -> 然后右键单击“任务计划程序库” -> 创建 任务

步骤 1:常规

步骤 2:触发器

步骤 3:操作

关注点

本文将帮助您使用PSFTP构建SFTP连接。您可以根据您的需求添加更多功能。

欢迎您的反馈。

历史

  • 2023年5月29日:初始版本
© . All rights reserved.