Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import paramiko as pmko
- import os
- import shutil
- import subprocess
- def list_files(directory, filetype, ssh):
- """
- This will scan a directory for the filetype,
- which is passed as `.jpg`, or `.mp3`, etc. and return
- a list of those files.
- """
- print("listing all photos in file.")
- distantFiles = list()
- filePath = '/storage/emulated/0/' + directory
- filePattern = '"*' + filetype + '"'
- rawcommand = 'find {path} -name {pattern}'
- command = rawcommand.format(path=filePath, pattern=filePattern)
- stdin, stdout, stderr = ssh.exec_command(command)
- filelist = stdout.read().splitlines()
- for afile in filelist:
- (head, filename) = os.path.split(afile)
- distantFiles.append(filename)
- return distantFiles
- def connect_to_phone():
- host = "192.168.0.105"
- port = 2222
- username = "logintest"
- password = "supersecretpassword"
- print("Starting connection")
- ssh = pmko.SSHClient()
- ssh.set_missing_host_key_policy(pmko.AutoAddPolicy())
- ssh.connect(host, port=port, username=username,
- password=password)
- sftp = ssh.open_sftp()
- print("Connected!")
- return sftp, ssh
- def main():
- sftp, ssh = connect_to_phone()
- files = list_files("DCIM/camera", ".jpg", ssh)
- directory = '/storage/emulated/0/' + 'DCIM/camera/'
- for file in files:
- _file = file.decode('utf-8')
- print(_file)
- #shutil.copy2(directory + _file, '/media/pi/usb/')
- sftp.put(directory+_file, '/media/pi/usb') #no such file or directory '/storage/emulated/0/DCIM/camera/[whatever].jpg
- quit()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement