SCP文件传输
前言:Unity3D笔记是我平时做一些好玩的测试和研究,记录的笔记。会比较详细也可能随口一提就过了。 所以大家见谅了,内容一般都会是原创的(非原创我会注明转载)。由于很多内容其他的朋友也肯定研究发表过,大家请指出错误。
公司需要我的软件运行的上位机和机器人的下位机,文件传输。由于下位机是Ubuntu的 直接自带SCP服务。于是打算用scp的传输方式。找了很多资料,发现还是很方便的。写一下笔记吧。
1.下位机的文件夹权限
在传输前,linux系统的机器 需要把目录的文件夹读写权限都赋予。具体就是: sudo chmod 777 操作。
window就不需要了,但是需要window需要装scp服务(这个自行百度);
2.C#动态库
需要的动态库:SharpSSH 。这是个ssh协议的动态库,功能很全。
链接:https://pan.baidu.com/s/1QPa3EL2cQy2S_uqzimpBvQ
提取码:6dtg

unity的环境的话,直接把图中的三个动态库 放进Pulgins文件夹里 就可以了。
3.SCP类
public class Scp : SshTransferProtocolBase
{
public Scp(string host, string user);
public Scp(string host, string user, string password);
public bool Recursive { get; set; }
public bool Verbos { get; set; }
protected override string ChannelType { get; }
public override void Cancel();
public void From(string remoteFile, string localPath, bool _recursive);
public void From(string remoteFile, string localPath);
public override void Get(string fromFilePath, string toFilePath);
public override void Mkdir(string dir);
public override void Put(string fromFilePath, string toFilePath);
public void To(string localPath, string remotePath);
public void To(string localPath, string remotePath, bool _recursive);
protected override void ConnectChannel();
protected void SCP_CheckConnectivity();
protected void SCP_ConnectFrom(out Channel channel, out Stream server, string rfile, bool recursive);
protected void SCP_ConnectTo(out Channel channel, out Stream server, string rfile, bool recursive);
protected void SCP_EnterIntoDir(Stream server, string dir);
protected void SCP_EnterIntoParent(Stream server);
protected void SCP_ReceiveFile(Stream server, string rfile, string lfile, int size);
protected void SCP_SendFile(Stream server, string src, string dst);
}
主要用到这几个:
- 下载文件 void Get(string fromFilePath, string toFilePath);
- 上传文件 To(string localPath, string remotePath);
- 创建文件夹和文件 void Mkdir(string dir);
4.下载文件
/// <summary>
/// SCP登录远程,并下载文件
/// </summary>
/// <param name="host">远程主机地址</param>
/// <param name="user">用户名</param>
/// <param name="password">密码</param>
/// <param name="fromFilePath">文件的绝对路径</param>
/// <param name="toFilePath">存放文件的绝对路径</param>
public static void ScpGetFile(string host, string user, string password, string fromFilePath, string toFilePath)
{
try
{
Scp myScp = new Scp(host, user, password);
//一般都是这个默认端口,可以设置
myScp.Connect(22);
myScp.Get(fromFilePath, toFilePath);
//操作结束 就关闭
myScp.Close();
}
catch (Exception e)
{
Debug.Log(e.ToString());
throw;
}
}
5.上传文件
/// <summary>
/// SCP登录远程,并上传文件
/// </summary>
/// <param name="host">远程主机地址</param>
/// <param name="user">用户名</param>
/// <param name="password">密码</param>
/// <param name="fromFilePath">文件的绝对路径</param>
/// <param name="toFilePath">存放文件的绝对路径</param>
public static void ScpToFile(string host, string user, string password, string localPath, string remotePath)
{
try
{
Scp myScp = new Scp(host, user, password);
//一般都是这个默认端口,可以设置
myScp.Connect(22);
myScp.To(localPath, remotePath);
//操作结束 就关闭
myScp.Close();
}
catch (Exception e)
{
Debug.Log(e.ToString())
throw;
}
}
这边需要注意的是:如果目录不对,容易卡死报错。
小结:本身这个库已经封装的很好了,直接拿来用就可以了。