转: window中使用PSFTP/WinSCP实现SFTP上传下载

摘自百度百科

sftp是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的加密方法。

sftp 与 ftp 有着几乎一样的语法和功能。

SFTP 为 SSH的一部份,是一种传输档案至 Blogger 伺服器的安全方式。

其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol的安全文件传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。

但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。

1.WinSCP部分

1.1 cmd命令行实例

Microsoft Windows XP [Version 5.1.2600]

(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\sobne>d:

D:\>cd winscp437

D:\winscp437>WinSCP

winscp> open ftp://username:[email protected] or hostname

Connecting to 172.0.0.1 ...

Connected with 172.0.0.1. Waiting for welcome message...

Connected

Starting the session...

Reading remote directory...

Session started.

Active session: [1] [email protected]

winscp> put d:\psftp.exe WinSCP/psftp.exe

d:\psftp.exe              |        320 KiB |  608.0 KiB/s | binary | 100%

winscp> get 20120420020049.txt

20120420020049.tx |          0 KiB |    0.0 KiB/s | ascii  | 100%

winscp> get 20120420020049.txt c:\t2.txt

20120420020049.tx |          0 KiB |    0.0 KiB/s | ascii  | 100%

winscp>

1.2 Batch批处理实例

将下面语句存入1.txt:

option batch on option confirm off # Connect using a password # open 用户名:密码@主机 # Connect open 用户名:密码@主机 cd /home/user option transfer binary get /root/test.c d:/ put d:/test.txt close exit

执行脚本

winscp.exe /console /script=1.txt

1.3 C#程序实现

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics;
namespace SFTP {     public class WinSCPtest     {         public string shellName { get { return "D:\\winscp437\\WinSCP.com"; } }         public string userName { get { return "username"; } }         public string userPassWord { get { return "userpassword"; } }         public string serverAddress { get { return "172.0.0.1"; } }         public string portNumber { get { return "21"; } }         public string fromFile { get { return "D:\\psftp.txt"; } }         public string toFile { get { return "WinSCP/psftp.txt"; } }         public void upload()         {             Process CommandLine = new Process();             CommandLine.StartInfo.FileName = shellName;             // CommandLine.StartInfo.Arguments = "/log=" + this._logPath;             CommandLine.StartInfo.UseShellExecute = false;             CommandLine.StartInfo.RedirectStandardInput = true;             CommandLine.StartInfo.RedirectStandardOutput = true;             CommandLine.StartInfo.CreateNoWindow = true;             CommandLine.Start();             //username用户名  targetAddress IP地址  portNumber 端口号             CommandLine.StandardInput.WriteLine("open ftp://{0}:{1}@{2}:{3}",             this.userName,this.userPassWord, this.serverAddress, this.portNumber);                          //上传文件到sftp服务器             string command = "put " + fromFile + " " + toFile ;             //fromFile要传送的文件路径本地的绝对路径    toFile服务器上保存文件的路径相对路径             CommandLine.StandardOutput.DiscardBufferedData();             CommandLine.StandardInput.WriteLine(command);             string result = CommandLine.StandardOutput.ReadLine();         }     } }

1.4 在线资源

http://winscp.net/eng/docs/commandline

http://winscp.net/eng/docs/guide_dotnet#input

2.SFTP部分

2.1 cmd命令行实例

C:\Documents and Settings\sobne>e:

E:\>cd SFTP

E:\SFTP>psftp.exe [email protected] -i privatekey.ppk

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 rsa2 key fingerprint is:

ssh-rsa 2048 0a:**:**:**:54:**:82:**:**:1f:**:**:**:87:30:**

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.

Store key in cache? (y/n) n

Using username "sobne".

Remote working directory is /

psftp>

2.2 Batch批处理实例

将下面语句存入myscript.scr:

cd / put File1.txt put File2.txt put File3.txt close

执行脚本

c:\putty\psftp.exe [email protected] -i putty_id_rsa_1024.ppk -b c:\putty\myscript.scr -bc -be -v

2.3 C#程序实现

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.Threading;
namespace Lib4Net {
    public class PSFTPunity     {         public string shellCommand { get; set; }
        public string serverName { get; set; }         public string userName { get; set; }         public string privateKey { get; set; }
        public PSFTPunity()          {                       }         public PSFTPunity(string shell, string server, string user, string ppk)         {             shellCommand = shell;             serverName = server;             userName = user;             privateKey = ppk;         }         /// <summary>         /// Upload the files         /// </summary>         /// <param name="localFile">localfile fullname</param>         /// <param name="remotePath">remotefile fullname</param>         /// <returns>output of the plugin during its running in console</returns>         public string Upload(string localFile, string remotePath)         {             string outPutMessage = "";
            ProcessStartInfo processInfo = new ProcessStartInfo();             processInfo.FileName = this.shellCommand;             processInfo.CreateNoWindow = true;             processInfo.UseShellExecute = false;             processInfo.RedirectStandardError = true;             processInfo.RedirectStandardInput = true;             processInfo.RedirectStandardOutput = true;             string arguments = "";             arguments += userName + "@" + serverName + " ";             arguments += "-i " + privateKey;             processInfo.Arguments = arguments;
            Process process = new Process();             try             {                 process.StartInfo = processInfo;                 process.Start();                 Thread.Sleep(5000);                 process.StandardInput.WriteLine("n");                 Thread.Sleep(2000);                 //process.StandardInput.WriteLine("cd " + remotePath);                 process.StandardInput.WriteLine("put " + localFile + " " + remotePath);                 //process.StandardInput.Close();                 Thread.Sleep(10000);                 //outPutMessage += process.StandardOutput.ReadToEnd();                 //outPutMessage += process.StandardError.ReadToEnd();                 process.WaitForExit(3000);                 process.Close();                 return outPutMessage;             }             catch (Exception ex)             {                 throw new Exception("Error occured during upload file to remote server!", ex);             }             finally             {                 process.Dispose();             }         }      } }

2.4 在线资源

http://dafis.ucdavis.edu/install/sftp/PuttyPSFTPKey.cfm

http://the.earth.li/~sgtatham/putty/0.52/htmldoc/Chapter8.html#8.3

文中使用的软件下载地址:http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html

时间: 2024-08-14 03:58:19

转: window中使用PSFTP/WinSCP实现SFTP上传下载的相关文章

SFTP上传下载文件

secureCRT SFTP上传/下载文件 远程登陆IP secureCRT会话中点击SFTP 3.cd  /home/dowload       linux平台切换到/home/dowload目录 4.cd d:\   windows平台切换到d盘 5.put 文件名           上传 /home/dowload目录下 6.get 文件名   下载文件到windows平台 d盘

Java Sftp上传下载文件

需要使用jar包  jsch-0.1.50.jar sftp上传下载实现类 package com.bstek.transit.sftp; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import co

SFTP上传下载文件、文件夹常用操作

SFTP上传下载文件.文件夹常用操作 1.查看上传下载目录lpwd 2.改变上传和下载的目录(例如D盘):lcd  d:/ 3.查看当前路径pwd 4.下载文件(例如我要将服务器上tomcat的日志文件取出来)进入你要下的文件所在的文件夹:cd /usr/apache-tomcat-6.0.39/logs/下载:get catalina.out 5.上传文件(例如我要上传一个文件到usr目录下)进入你想要上传文件的目录cd /usr上传文件put do.sh 6.上传下载文件夹格式:下载文件夹g

Xshell5下利用sftp上传下载传输文件

sftp是Secure File TransferProtocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp与 ftp有着几乎一样的语法和功能.SFTP为 SSH的一部分,是一种传输档案至Blogger伺服器的安全方式.其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol)的安全文件传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接操作,所以从某种意义上来

JSP+Servlet中使用jspsmartupload.jar进行图片上传下载

JSP+Servlet中使用cos.jar进行图片上传 upload.jsp <form action="FileServlet" method="post" enctype="multipart/form-data"> <input type="file" name="myfile"> <input type="text" name="cmt&q

Linux系统中使用lftp命令实现FTP上传下载

Linux 下常用的操作命令有 ftp. lftp 和 sftp,图形化界面非常好用的有 FileZilla.不过在服务器命令界面中,lftp使用比较方便,功能也比ftp更加强大.lftp的界面很像Linux的shell,有命令补全.历史记录.允许多个后台任务执行.书签.排队.镜像.断点续传.多进程下载等功能. 登录ftp命令 代码: lftp 用户名:密码@ftp地址:传送端口(默认21) 用法 (1)lftp username:[email protected]:21 回车 (2)lftp

用jsch.jar实现SFTP上传下载删除

java类: 需要引用的jar: jsch-0.1.53.jar package com.isoftstone.www.ftp; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; import java.util.Vector; import com.jcraft.jsch.Channel; import com.jc

JAVA Sftp 上传下载

SftpUtils package xxx;import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.ChannelSftp.LsEntry; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpATTRS; import com.jcraft.jsc

Sftp上传下载文件,部分代码取自网络,需要添加DiffieHellman.dll、Org.Mentalis.Security.dll、Tamir.SharpSSH.dll三个dll引用

using System; using System.Collections; using System.Collections.Generic; using System.IO; using Tamir.SharpSsh.java.io; using Tamir.SharpSsh.jsch; namespace EB.Mall.Core.Utils { public class SFTPHelper { private Session m_session; private Channel m_