Advertisement
Guest User

Untitled

a guest
Mar 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. import paramiko as pmko
  2. import os
  3. import shutil
  4. import subprocess
  5.  
  6.  
  7. def list_files(directory, filetype, ssh):
  8. """
  9. This will scan a directory for the filetype,
  10. which is passed as `.jpg`, or `.mp3`, etc. and return
  11. a list of those files.
  12. """
  13. print("listing all photos in file.")
  14. distantFiles = list()
  15. filePath = '/storage/emulated/0/' + directory
  16. filePattern = '"*' + filetype + '"'
  17. rawcommand = 'find {path} -name {pattern}'
  18. command = rawcommand.format(path=filePath, pattern=filePattern)
  19.  
  20. stdin, stdout, stderr = ssh.exec_command(command)
  21. filelist = stdout.read().splitlines()
  22. for afile in filelist:
  23. (head, filename) = os.path.split(afile)
  24. distantFiles.append(filename)
  25. return distantFiles
  26.  
  27. def connect_to_phone():
  28. host = "192.168.0.105"
  29. port = 2222
  30. username = "logintest"
  31. password = "supersecretpassword"
  32.  
  33. print("Starting connection")
  34. ssh = pmko.SSHClient()
  35. ssh.set_missing_host_key_policy(pmko.AutoAddPolicy())
  36. ssh.connect(host, port=port, username=username,
  37. password=password)
  38. sftp = ssh.open_sftp()
  39. print("Connected!")
  40. return sftp, ssh
  41.  
  42.  
  43. def main():
  44. sftp, ssh = connect_to_phone()
  45. files = list_files("DCIM/camera", ".jpg", ssh)
  46. directory = '/storage/emulated/0/' + 'DCIM/camera/'
  47. for file in files:
  48. _file = file.decode('utf-8')
  49. print(_file)
  50. #shutil.copy2(directory + _file, '/media/pi/usb/')
  51. sftp.put(directory+_file, '/media/pi/usb') #no such file or directory '/storage/emulated/0/DCIM/camera/[whatever].jpg
  52. quit()
  53.  
  54.  
  55. if __name__ == "__main__":
  56. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement