How to implement SCP operation in Android

Time:2022-5-6
catalogue
  • SSH
    • SCP
    • SFTP
    • Using SCP in Android
    • SFTP delete file

This paper briefly introduces the implementation of SCP operation in android with SSH Library ganymed-ssh2.

SSH

SSH is a protocol designed to provide security for remote login sessions and other network services. In short, it is a network protocol. It is the standard configuration of Linux. Used for communication between Linux devices.

SCP

SCP is a protocol that encrypts and copies files based on SSH. Use SSH for identity authentication to ensure the authenticity and reliability of data transmission.

SCP runs through TCP port 22 by default

Common syntax of SCP program:

//Copy files to host
scp SourceFile [email protected]:directory/TargetFile


//Copy files from host
scp [email protected]:directory/SourceFile TargetFile
scp -r [email protected]:directory/SourceFolder TargetFolder
//Copy files from host
scp [email protected]:directory/SourceFile TargetFile
scp -r [email protected]:directory/SourceFolder TargetFolder

SFTP

SFTP is also based on SSH secure file transfer protocol. Unlike FTP, FTP uses clear text to transmit user information based on TCP. Poor safety.

Using SCP in Android

  • Download ganymed-ssh2 jar package

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

Official download address 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 connection
      scpClient = connection.createSCPClient();
    } catch (IOException e) {
      e.printStackTrace();
      close();
    }
  }

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

  public boolean getIsAuthed(){
    return isAuthed;
  }

  //Copy files to server
  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");
                }
              }

SFTP delete file


  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");
          }
        }

The above is the details of how to realize SCP operation in Android. For more information about realizing SCP operation in Android, please pay attention to other relevant articles of developeppaer!

Recommended Today

IOS imitates various ways of wechat long press to identify QR code

Reference:https://github.com/nglszs/BCQRcode Mode 1: #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end ************** #import “ViewController.h” @interface ViewController () @end @implementation ViewController – (void)viewDidLoad { [super viewDidLoad]; self. Title = @ “QR code”; UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] Initwithtitle: @ “build” style:UIBarButtonItemStylePlain target:self action:@selector(backView)]; self.navigationItem.leftBarButtonItem = leftBtn; UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] Initwithtitle: @ “scan” style:UIBarButtonItemStylePlain target:self action:@selector(ScanView)]; […]