在Android 中实现scp操作

本文简单介绍用SSHganymed-ssh2Android中实现scp操作。

SSH

SSH是专为远程登录会话和其他网络服务提供安全性的协议,简单的说就是一种网络协议。是linux的标准配置。用于linux设备之间的通讯。

SCP

SCP是一种基于SSH完成加密拷贝文件的协议。使用SSH进行身份认证确保数据传输的真实性和可靠性。

SCP默认通过TCP端口22运行

SCP程序常用语法:


// 复制文件到主机
scp SourceFile user@host:directory/TargetFile

// 从主机复制文件
scp user@host:directory/SourceFile TargetFile
scp -r user@host:directory/SourceFolder TargetFolder

SFTP

SFTP也是基于SSH安全文件传输协议。不同于基于FTP,FTP基于Tcp使用明文传输用户信息。安全性较差。

Android中使用SCP

  • 下载ganymed-ssh2 jar包

<!-- https://mvnrepository.com/artifact/ch.ethz.ganymed/ganymed-ssh2 -->
<dependency>
    <groupId>ch.ethz.ganymed</groupId>
    <artifactId>ganymed-ssh2</artifactId>
    <version>build210</version>
</dependency>

官方下载地址 http://www.ganymed.ethz.ch/ssh2/


public class Scp {

    private volatile static Scp scpInstance;

    private String user;
    private String pass;
    private String host;
    private Connection connection;
    private SCPClient scpClient;
    private Boolean isAuthed;

    private Scp(String user, String pass, String host){
        this.user = user;
        this.pass = pass;
        this.host = host;
    }

    public static Scp getScpUtilsInstance(String user, String pass, String host){

        if(scpInstance == null) {
            synchronized(Scp.class) {
                if(scpInstance == null) {
                    scpInstance = new Scp(user,pass,host);
                }
            }
        }
        return scpInstance;
    }


    public void connect(){
        connection = new Connection(host);
        try {
            connection.connect();
            isAuthed = connection.authenticateWithPassword(user,pass);
            // scp 连接
            scpClient = connection.createSCPClient();
        } catch (IOException e) {
            e.printStackTrace();
            close();
        }
    }

    public void close(){
        connection.close();
        sftPv3Client.close();
    }

    public boolean getIsAuthed(){
        return isAuthed;
    }

    // 拷贝文件到服务器
    public void putFile(String filePath,String aimPath){
        try {
            if(scpClient != null){
                scpClient.put(filePath,aimPath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

  

}

Scp scp = Scp.getScpUtilsInstance("root","psd","192.168.199.3");
                            scp.connect();
                            if(scp.getIsAuthed()){
                                for(int i = 0;i<data.getLayers();i++){
                                    scp.putFile(SlcParser.pngDirectory+"/"+i+".png","/home");
                                }
                            }

scp

SFTP 删除文件



    private SFTPv3Client sftPv3Client;
    
   sftPv3Client = new SFTPv3Client(connection);

    public void rmFile(String filePath){
            try {
                if(sftPv3Client != null){
                    sftPv3Client.rm(filePath);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

Scp scp = Scp.getScpUtilsInstance("root","psd","192.168.199.3");
                scp.connect();
                if(scp.getIsAuthed()){
                    for(int i = 0;i<10;i++){
                        scp.rmFile("/home/"+i+".png");
                    }
                }

scp

转载于:https://www.cnblogs.com/chenjy1225/p/9662546.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值