TShopping

 找回密碼
 註冊
搜索
查看: 1847|回復: 0
打印 上一主題 下一主題

[教學] JCIFS 介紹

[複製鏈接]
跳轉到指定樓層
1#
發表於 2020-7-20 23:01:23 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
介紹
JCIFS是java-based存取網路芳鄰通訊協定的API。

之所以接觸這個API,是因為好奇Windows管理工具psexec,能將Local程式放到Remote並執行的做法。後來看到類似工具paexec程式碼,與經過自己的測試,發現原理是將程式複製到遠端的管理共享資料夾,再透過WMI呼叫外部程式。

接下來將說明我有使用的API。

如何做?
首先至link下載library。接下來就是寫寫寫。

建立檔案(Create File)
大部分的操作都可以透過SmbFile類別完成。而與遠端的機器認證,是透過NTLM通訊協定
  1. private static NtlmPasswordAuthentication createAuth(String aUser, String aPasswd){
  2.                 StringBuffer sb = new StringBuffer(aUser);
  3.                 sb.append(':').append(aPasswd);
  4.                 return new NtlmPasswordAuthentication(sb.toString());
  5.         }

  6.         public static SmbFile createSmbFile(String aUser, String aPasswd, String aTarget) throws IOException {
  7.                 NtlmPasswordAuthentication auth = createAuth(aUser, aPasswd);
  8.                 return new SmbFile(aTarget, auth);
  9.         }
複製代碼

如果有網域,可以使用
  1. new NtlmPasswordAuthentication(domain, user, passwd);
複製代碼

在建立完SmbFile物件後,剩下其實就和操作一般檔案無異。
  1. public static void createFile(String aUser, String aPasswd, String aTarget, String aContent) throws IOException {
  2.                 SmbFile sFile = createSmbFile(aUser, aPasswd, aTarget);
  3.                 SmbFileOutputStream sfos = null;
  4.                 try {
  5.                         sfos = new SmbFileOutputStream(sFile);
  6.                         sfos.write(aContent.getBytes());
  7.                 } finally {
  8.                         close(sfos);
  9.                 }
  10.         }
複製代碼


Delete File
刪除檔案:
  1. public static void deleteFile(String aUser, String aPasswd, String aTarget) throws IOException {
  2.                 SmbFile sFile = createSmbFile(aUser, aPasswd, aTarget);
  3.                 sFile.delete();
  4.         }
複製代碼

Exist
確認檔案是否存在:
  1. public static boolean exists(String aUser, String aPasswd, String aTarget) throws IOException {
  2.                 SmbFile sFile = createSmbFile(aUser, aPasswd, aTarget);
  3.                 return sFile.exists();
  4.         }
複製代碼

Copy File
複製本地檔案到遠端:
  1. public static void copyFileTo(String aUser, String aPasswd, String aSource, String aTarget) throws IOException {
  2.                 SmbFile sFile = createSmbFile(aUser, aPasswd, aTarget);
  3.                 SmbFileOutputStream sfos = null;
  4.                 FileInputStream fis = null;
  5.                 try {
  6.                         sfos = new SmbFileOutputStream(sFile);
  7.                         fis = new FileInputStream(new File(aSource));

  8.                         byte[] buf = new byte[1024];
  9.                         int len;
  10.                         while(( len = fis.read(buf) )> 0 ){
  11.                                 sfos.write(buf, 0, len);
  12.                         }
  13.                 } finally {
  14.                         close(sfos);
  15.                         close(fis);
  16.                 }
  17.         }
複製代碼

複製遠端檔案到本地:
  1. public static void copyFileFrom(String aUser, String aPasswd, String aSource, String aTarget) throws IOException {
  2.                 SmbFile sFile = createSmbFile(aUser, aPasswd, aSource);
  3.                 SmbFileInputStream sfis = null;
  4.                 FileOutputStream fos = null;
  5.                 try {
  6.                         sfis = new SmbFileInputStream(sFile);
  7.                         fos = new FileOutputStream(new File(aTarget));

  8.                         byte[] buf = new byte[1024];
  9.                         int len;
  10.                         while(( len = sfis.read(buf) )> 0 ){
  11.                                 fos.write(buf, 0, len);
  12.                         }
  13.                 } finally {
  14.                         close(sfis);
  15.                         close(fos);
  16.                 }
  17.         }
複製代碼

Test
簡單的寫了對檔案建立與複製的單元測試,這是對某台Win7機器的管理共享做存取。

  1. public class SmbFileUtilTest {

  2.         private String targetFolder = "smb://192.168.1.25/admin$/";
  3.         private String user = "administrator";
  4.         private String passwd = "123456";

  5.         private String remoteTmpFile = null;
  6.         private String localTmpFile = null;

  7.         @After
  8.         public void tearDown() throws IOException{
  9.                 if( remoteTmpFile != null ){
  10.                         SmbFileUtil.deleteFile(user, passwd, remoteTmpFile);
  11.                         assertFalse(SmbFileUtil.exists(user, passwd, remoteTmpFile));
  12.                 }
  13.                 if( localTmpFile != null ){
  14.                         new File(localTmpFile).delete();
  15.                 }
  16.         }

  17.         @Test
  18.         public void copyFile() throws IOException{
  19.                 remoteTmpFile = targetFolder+"temp.txt";
  20.                 localTmpFile = "temp.tmp";
  21.                 SmbFileUtil.copyFileTo(user, passwd, "libs/jcifs-1.3.18.jar", remoteTmpFile);
  22.                 assertTrue(SmbFileUtil.exists(user, passwd, remoteTmpFile));

  23.                 SmbFileUtil.copyFileFrom(user, passwd, remoteTmpFile,  localTmpFile);
  24.                 File localFile = new File(localTmpFile);
  25.                 assertTrue(localFile.exists());

  26.                 assertEquals(new File("libs/jcifs-1.3.18.jar").length(), localFile.length());
  27.         }

  28.         @Test
  29.         public void createFile() throws IOException{
  30.                 remoteTmpFile = targetFolder+"temp.txt";
  31.                 SmbFileUtil.createFile(user, passwd , remoteTmpFile, "test");
  32.                 assertTrue(SmbFileUtil.exists(user, passwd, remoteTmpFile));
  33.         }
  34. }
複製代碼

Summary
要將檔案複製到遠端,也可以透過
  • 在本地建立網芳或Samba Server,讓遠端由本地複製檔案。
  • 使用echo在遠端建立wget的VB Script,從本地的http server去wget檔案。


不管是以上兩種方法,或是透過管理共享,都是有可能因為使用者權限與安全性設定而失敗。只能見招拆招了。

Reference



 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-20 07:46 , Processed in 0.245055 second(s), 22 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表