Sharpssh连接linux上传下载文件
来源:互联网 发布:tensorflow theano 编辑:程序博客网 时间:2024/06/11 01:38
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Tamir.SharpSsh;
using Org.Mentalis.Security;
using Tamir.SharpSsh.jsch;
/*
*
* 连接linux,传输数据
*
*/
namespace ReplayTool
{
/// <summary>
/// Sample showing the use of the SSH file trasfer features of
/// SharpSSH such as SFTP and SCP
/// </summary>
public class SshFileTransfer
{
private Replay replay = null;
public SshFileTransfer(Replay replay)
{
this.replay = replay;
}
private void getStruct()
{
}
/*
* 根据结构体中的信息,上传下载文件
* 出现异常则返回null
*/
public string transferFile(ref TransferStructInfo transfer)
{
try
{
// SshConnectionInfo input = Util.GetInput();
TransferStructInfo input = new TransferStructInfo { Host = "192.168.50.39", User = "ei", Pass = "ei", Direction = "to", Protocol = "sftp", FromPath = "D:\\c.txt", ToPath = "/home/ei/" };
input = transfer;
if (input.Protocol == null)
{
input.Protocol = "sftp";
}
string protocol = input.Protocol.ToLower();
SshTransferProtocolBase sshCp;
if (protocol.Equals("scp"))
sshCp = new Scp(input.Host, input.User);
else
sshCp = new Sftp(input.Host, input.User);
if (input.Pass != null) sshCp.Password = input.Pass;
if (input.IdentityFile != null) sshCp.AddIdentityFile(input.IdentityFile);
sshCp.OnTransferStart += new FileTransferEvent(sshCp_OnTransferStart);
sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
sshCp.OnTransferEnd += new FileTransferEvent(sshCp_OnTransferEnd);
Console.Write("Connecting...");
sshCp.Connect();
Console.WriteLine("OK");
while (true)
{
string direction = input.Direction.ToLower();
string fromPath = input.FromPath;
string toPath = input.ToPath;
if (direction == "to")
{
sshCp.Put(fromPath, toPath);
replay.writeLogToRichTb("【执行信息】上传文件" + input.FromPath + "到用户" + input.User + "的路径" + input.ToPath + "下成功");
}
else
{
sshCp.Get(fromPath, toPath);
}
break; //此处为临时加,待改动
}
sshCp.Close();
return "ok";
//replay.writeLogToRichTb("OK");
}
catch (Exception e)
{
replay.writeLogToRichTb(e.Message);
}
return null;
}
static Tamir.SharpSsh.jsch.examples.ConsoleProgressBar progressBar;
private static void sshCp_OnTransferStart(string src, string dst, int transferredBytes, int totalBytes, string message)
{
Console.WriteLine();
progressBar = new Tamir.SharpSsh.jsch.examples.ConsoleProgressBar();
progressBar.Update(transferredBytes, totalBytes, message);
}
private static void sshCp_OnTransferProgress(string src, string dst, int transferredBytes, int totalBytes, string message)
{
if (progressBar != null)
{
progressBar.Update(transferredBytes, totalBytes, message);
}
}
private static void sshCp_OnTransferEnd(string src, string dst, int transferredBytes, int totalBytes, string message)
{
if (progressBar != null)
{
progressBar.Update(transferredBytes, totalBytes, message);
progressBar = null;
}
}
}
//用以存储连接linux所需的信息
public struct TransferStructInfo
{
public string Host; //ip
public string User; //用户名
public string Pass; //密码
public string IdentityFile; //可为空
public string Direction; //传输方向
public string Protocol; //协议,可设置为sftp
public string FromPath; //文件路径
public string ToPath;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Tamir.SharpSsh;
using Org.Mentalis.Security;
using Tamir.SharpSsh.jsch;
/*
*
* 连接linux,传输数据
*
*/
namespace ReplayTool
{
/// <summary>
/// Sample showing the use of the SSH file trasfer features of
/// SharpSSH such as SFTP and SCP
/// </summary>
public class SshFileTransfer
{
private Replay replay = null;
public SshFileTransfer(Replay replay)
{
this.replay = replay;
}
private void getStruct()
{
}
/*
* 根据结构体中的信息,上传下载文件
* 出现异常则返回null
*/
public string transferFile(ref TransferStructInfo transfer)
{
try
{
// SshConnectionInfo input = Util.GetInput();
TransferStructInfo input = new TransferStructInfo { Host = "192.168.50.39", User = "ei", Pass = "ei", Direction = "to", Protocol = "sftp", FromPath = "D:\\c.txt", ToPath = "/home/ei/" };
input = transfer;
if (input.Protocol == null)
{
input.Protocol = "sftp";
}
string protocol = input.Protocol.ToLower();
SshTransferProtocolBase sshCp;
if (protocol.Equals("scp"))
sshCp = new Scp(input.Host, input.User);
else
sshCp = new Sftp(input.Host, input.User);
if (input.Pass != null) sshCp.Password = input.Pass;
if (input.IdentityFile != null) sshCp.AddIdentityFile(input.IdentityFile);
sshCp.OnTransferStart += new FileTransferEvent(sshCp_OnTransferStart);
sshCp.OnTransferProgress += new FileTransferEvent(sshCp_OnTransferProgress);
sshCp.OnTransferEnd += new FileTransferEvent(sshCp_OnTransferEnd);
Console.Write("Connecting...");
sshCp.Connect();
Console.WriteLine("OK");
while (true)
{
string direction = input.Direction.ToLower();
string fromPath = input.FromPath;
string toPath = input.ToPath;
if (direction == "to")
{
sshCp.Put(fromPath, toPath);
replay.writeLogToRichTb("【执行信息】上传文件" + input.FromPath + "到用户" + input.User + "的路径" + input.ToPath + "下成功");
}
else
{
sshCp.Get(fromPath, toPath);
}
break; //此处为临时加,待改动
}
sshCp.Close();
return "ok";
//replay.writeLogToRichTb("OK");
}
catch (Exception e)
{
replay.writeLogToRichTb(e.Message);
}
return null;
}
static Tamir.SharpSsh.jsch.examples.ConsoleProgressBar progressBar;
private static void sshCp_OnTransferStart(string src, string dst, int transferredBytes, int totalBytes, string message)
{
Console.WriteLine();
progressBar = new Tamir.SharpSsh.jsch.examples.ConsoleProgressBar();
progressBar.Update(transferredBytes, totalBytes, message);
}
private static void sshCp_OnTransferProgress(string src, string dst, int transferredBytes, int totalBytes, string message)
{
if (progressBar != null)
{
progressBar.Update(transferredBytes, totalBytes, message);
}
}
private static void sshCp_OnTransferEnd(string src, string dst, int transferredBytes, int totalBytes, string message)
{
if (progressBar != null)
{
progressBar.Update(transferredBytes, totalBytes, message);
progressBar = null;
}
}
}
//用以存储连接linux所需的信息
public struct TransferStructInfo
{
public string Host; //ip
public string User; //用户名
public string Pass; //密码
public string IdentityFile; //可为空
public string Direction; //传输方向
public string Protocol; //协议,可设置为sftp
public string FromPath; //文件路径
public string ToPath;
}
}
0 0
- Sharpssh连接linux上传下载文件
- 用PuTTY连接Linux上传下载文件常用方法
- 用PuTTY连接Linux上传下载文件常用方法
- Linux上传下载文件命令
- linux上传下载文件
- linux上传下载文件
- Linux上传下载文件/文件夹
- linux文件上传下载
- linux服务器上传下载文件
- SSH 连接、远程上传下载文件
- linux文件上传下载命令使用
- linux上传下载文件工具lrzsz
- linux主机之间 上传下载文件
- linux中tftp上传下载文件
- 向linux服务器上传下载文件
- 在Windows上使用终端模拟程序连接操作Linux以及上传下载文件
- ubuntu SSH 连接、远程上传下载文件
- 3. SSH 连接、远程上传下载文件
- 关于Handler和Looper
- json解析,jquery解析遍历json
- hbase编程指南
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (
- OSG OSGearth vs2010编译
- Sharpssh连接linux上传下载文件
- order by 列加索引
- MySQL中文全文检索demoSQL
- Iptables防火墙使用
- Parallel Computing Toolbox(PCT)
- Android知识梳理之BroadcastReceiver整理
- 查看android sqlite数据库常用操作
- Android中通过EventBus传递消息数据
- # Java面试复习准备