SCP文件传输

该博客围绕SCP文件传输展开,因公司上位机与Ubuntu下位机文件传输需求,采用SCP方式。介绍了传输前下位机文件夹权限设置,需给Linux目录赋予读写权限;还提及所需C#动态库SharpSSH,以及SCP类的主要方法,如文件下载、上传等,同时提醒注意目录问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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 
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190530144022789.png)
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);
    }

主要用到这几个:

  1. 下载文件 void Get(string fromFilePath, string toFilePath);
  2. 上传文件 To(string localPath, string remotePath);
  3. 创建文件夹和文件 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;
        }
     
    }

这边需要注意的是:如果目录不对,容易卡死报错。

小结:本身这个库已经封装的很好了,直接拿来用就可以了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值