使用Putty PSFTP和C#(开源)和任务计划程序配置来保护FTP或SFTP

目录

介绍

现在PSFTP是什么?

SFTP需要什么?

让我们现在就开始吧

背景

使用代码

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

第1步:常规

第2步:触发器

​编辑 第3步:操作

兴趣点


介绍

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

最近,我的一个客户将他们的文件传输服务器FTP更改为SFTP,然后我做了一段时间的研究,以找到使用简单/更少的C#代码进行管理的最佳方法,而无需任何付费工具。我在这个例子中使用了PSFTP

现在PSFTP是什么?

我们大多数人都知道PuTTYPuTTY是适用于32Windows系统的免费SSHTelnetRlogin客户端。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

命令 2y

命令 3:输入用户名

命令 4:输入密码

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

喜欢Help;quit;ls;get;put[更多信息可在此链接找到]

背景

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

使用代码

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连接。您可以根据需要添加更多功能。

https://www.codeproject.com/Articles/1175314/Secure-FTP-or-SFTP-using-Putty-PSFTP-with-Csharp-O

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值