1

I need to copy a file from my windows machine with cygwin to my raspberry pi using python. I know I can use programs like filezilla, but my project needs to do this programatically.

I know there are modules that allow me to SSH in python, but I'm new to raspberry pi and python and I'm having issues installing the modules from command line. Would someone be willing to give me a little guidance on installing the right modules like paramiko or similar to get it to work on my pi?

Example code to get me up and running would also be fantastic, but I know there are other threads on the code, so you don't need to waste time on that, unless you feel so inclined. Thank you for the help in advance.

2 Answers 2

1

If your runtime machine have scp installed. You can simply launch scp command from python, it's the simpler. But scp will ask you to give a password each time, that's why I recommend the folowing approach:

1) Generate a RSA keypair on your runtime machine: launch that on bash and follow instructions, it's up to you to decide if you use a passphrase:

ssh-keygen

You will now have .pub file where you decided to store the key. add the content of this .pub in the following file of your raspberry pi:

~/.ssh/authorized_keys

Obviously, create the .ssh folder and the file if they don't exists. if authorized_keys exists, append the .pub content at his end after a new line so you won't override it current content. Once this is done, your raspberry pi will trust your computer and won't ask for a password when someone connect with that key you generated (so keep it secret!). If you prefer type your password each time your script is runned, then just don't add this key to authorized_keys.

now in your python script:

import os
cmd = os.popen('scp <USER>@<YOUR_MACHINE_ADDRESS>:<LOCATION_OF_THE_REMOTE_FILE> <LOCATION_TO_STORE_IT>')
cmd.read()

That will copy from your raspberry pi to your computer, if you wants to do the opposite, switch the two arguments of scp.

Obviously, it's up to you to create logical structures around the command to determine wich argument you will give to it and in wich case you execute it. Know that using os.popen(), the program will block until the command has finished, and that the command won't be executed untill cmd.read() is called

If you don't like this method, you may have a look here and the links provided in this question

Sign up to request clarification or add additional context in comments.

1 Comment

Great thank you. is OS built into python? The one issue I have is while this will work for my testing on windows, the machine I'll ultimately be using for the project I won't be able to access past enabling SSH on my network.
0

You may consider ftplib from python libraries. Then you will be able to copy file both using GUI client like Filezila and programmatically.

from ftplib import FTP

On the Raspberry side, you must start an FTP server (ftpd, vsftpd, etc.), they are legion on Debian. On the client side, you can use a GUI client or make you own program like in python!

If you need a encrypted solution, you may use TLS or SSL over FTP.

2 Comments

This solves my main issue of being able to get a module to run on my pi. Most of the modules I've tried to install for this like paramiko I've tried to install in the command line, and run into issues. Which modules would you recommend for TLS or SSL?
The module ftplib allows you to use SSL certification, it is built-in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.