metalx1000

Python SMBclient Samba

Jun 14th, 2020
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. #Notes on Python smbclient for Samba
  2. #more notes https://github.com/jborean93/smbprotocol/blob/master/examples/high-level/file-management.py
  3.  
  4. #install
  5. sudo apt-get install gcc python-dev libkrb5-dev python-pip
  6. pip install smbprotocol[kerberos]
  7.  
  8. #useage
  9. import os,smbclient
  10.  
  11. server="192.168.1.101"
  12. user="username"
  13. password="password"
  14. folder="folder"
  15.  
  16. #login
  17. smbclient.register_session(server, username=user, password=password)
  18.  
  19. #list option
  20. dir(smbclient)
  21.  
  22. #mkdir
  23. smbclient.mkdir(r"\\\\"+server+"\\"+folder+"\\newdir")
  24.  
  25. #remove dir
  26. smbclient.removedirs(r"\\\\"+server+"\\"+folder+"\\newdir")
  27.  
  28. #list dir
  29. smbclient.listdir(r"\\\\"+server+"\\"+folder)
  30.  
  31. #scan directory
  32. d=smbclient.scandir(r"\\\\"+server+"\\"+folder)
  33. for item in d:
  34.   print(item)
  35.  
  36. #write to file
  37. with smbclient.open_file(r"\\\\"+server+"\\"+folder+"\\scan.txt",mode="w") as fd:
  38.   fd.write(u"scan")
  39.  
  40. #read a file
  41. with smbclient.open_file(r"\\\\"+server+"\\"+folder+"\\scan.txt",mode="rb") as fd:
  42.   fd.read()
Add Comment
Please, Sign In to add comment