如何在Android 中實(shí)現(xiàn)scp操作
本文簡(jiǎn)單介紹用SSH庫(kù)ganymed-ssh2在Android中實(shí)現(xiàn)scp操作。
SSH
SSH是專為遠(yuǎn)程登錄會(huì)話和其他網(wǎng)絡(luò)服務(wù)提供安全性的協(xié)議,簡(jiǎn)單的說(shuō)就是一種網(wǎng)絡(luò)協(xié)議。是linux的標(biāo)準(zhǔn)配置。用于linux設(shè)備之間的通訊。
SCP
SCP是一種基于SSH完成加密拷貝文件的協(xié)議。使用SSH進(jìn)行身份認(rèn)證確保數(shù)據(jù)傳輸?shù)恼鎸?shí)性和可靠性。
SCP默認(rèn)通過(guò)TCP端口22運(yùn)行
SCP程序常用語(yǔ)法:
// 復(fù)制文件到主機(jī) scp SourceFile user@host:directory/TargetFile // 從主機(jī)復(fù)制文件 scp user@host:directory/SourceFile TargetFile scp -r user@host:directory/SourceFolder TargetFolder
// 從主機(jī)復(fù)制文件 scp user@host:directory/SourceFile TargetFile scp -r user@host:directory/SourceFolder TargetFolder
SFTP
SFTP也是基于SSH安全文件傳輸協(xié)議。不同于基于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>
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; } // 拷貝文件到服務(wù)器 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 刪除文件
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"); } }
以上就是如何在Android 中實(shí)現(xiàn)scp操作的詳細(xì)內(nèi)容,更多關(guān)于在Android 中實(shí)現(xiàn)scp操作的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Vue-CLI 3 scp2自動(dòng)部署項(xiàng)目至服務(wù)器的方法
- Python調(diào)用scp向服務(wù)器上傳文件示例
- windows通過(guò)秘鑰使用ssh和scp的方法
- python執(zhí)行scp命令拷貝文件及文件夾到遠(yuǎn)程主機(jī)的目錄方法
- linux采用scp命令拷貝文件到本地,拷貝本地文件到遠(yuǎn)程服務(wù)器的方法
- bash腳本中將密碼傳遞給ssh/scp命令方法詳解
- python Pexpect 實(shí)現(xiàn)輸密碼 scp 拷貝的方法
- Linux使用scp命令進(jìn)行文件遠(yuǎn)程拷貝詳解
- scp 將數(shù)據(jù)從一臺(tái)linux服務(wù)器復(fù)制到另一臺(tái)linux服務(wù)器
相關(guān)文章
android dialog根據(jù)彈窗等級(jí)排序顯示的示例代碼
這篇文章主要介紹了android dialog根據(jù)彈窗等級(jí)排序顯示,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10Kotlin?RecyclerView滾動(dòng)控件詳解
RecyclerView是Android一個(gè)更強(qiáng)大的控件,其不僅可以實(shí)現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實(shí)現(xiàn)數(shù)據(jù)縱向滾動(dòng),也可以實(shí)現(xiàn)橫向滾動(dòng)(ListView做不到橫向滾動(dòng))。接下來(lái)講解RecyclerView的用法2022-12-12Android編程操作聯(lián)系人的方法(查詢,獲取,添加等)
這篇文章主要介紹了Android編程操作聯(lián)系人的方法,包括針對(duì)聯(lián)系人的查詢,獲取,添加等操作,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-01-01Android無(wú)需root實(shí)現(xiàn)apk的靜默安裝
這篇文章主要介紹了Android無(wú)需root實(shí)現(xiàn)apk的靜默安裝 的相關(guān)資料,需要的朋友可以參考下2016-01-01Flutter 利用CustomScrollView實(shí)現(xiàn)滑動(dòng)效果
我們可以使用ListView將幾個(gè)GridView組合在一起實(shí)現(xiàn)了不同可滑動(dòng)組件的粘合,但是這里必須要設(shè)置禁止 GridView 的滑動(dòng),防止多個(gè)滑動(dòng)組件的沖突。這種方式寫起來(lái)不太方便,事實(shí)上 Flutter 提供了 CustomScrollView 來(lái)粘合多個(gè)滑動(dòng)組件,并且可以實(shí)現(xiàn)更有趣的滑動(dòng)效果。2021-06-06Android開(kāi)發(fā)之軟鍵盤用法實(shí)例分析
這篇文章主要介紹了Android開(kāi)發(fā)之軟鍵盤用法,實(shí)例分析了Android軟鍵盤的實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-05-05Android設(shè)置Activity背景為透明style的簡(jiǎn)單方法(必看)
下面小編就為大家?guī)?lái)一篇Android設(shè)置Activity背景為透明style的簡(jiǎn)單方法(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10