简体   繁体   English

树莓派上带有python的SSH / SCP(或类似版本)

[英]SSH/SCP (Or similar) with python on raspberry pi

I need to copy a file from my windows machine with cygwin to my raspberry pi using python. 我需要使用python将文件从cygwin的Windows机器复制到树莓派。 I know I can use programs like filezilla, but my project needs to do this programatically. 我知道我可以使用诸如filezilla之类的程序,但是我的项目需要以编程方式执行此操作。

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. 我知道有一些模块可以让我在python中进行SSH,但是我对树莓派和python还是陌生的,并且在从命令行安装模块时遇到了问题。 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? 有人愿意在安装正确的模块(如paramiko或类似模块)上给我一些指导,使其在我的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. 谢谢您的帮助。

If your runtime machine have scp installed. 如果您的运行时计算机已安装了scp。 You can simply launch scp command from python, it's the simpler. 您可以简单地从python启动scp命令,这很简单。 But scp will ask you to give a password each time, that's why I recommend the folowing approach: 但是scp每次都会要求您提供密码,这就是为什么我建议采用以下方法的原因:

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: 1)在您的运行时计算机上生成一个RSA密钥对:在bash上启动它并按照说明进行操作,由您决定是否使用密码:

ssh-keygen

You will now have .pub file where you decided to store the key. 现在,您将在其中决定要存储密钥的.pub文件中。 add the content of this .pub in the following file of your raspberry pi: 在树莓派的以下文件中添加此.pub的内容:

~/.ssh/authorized_keys

Obviously, create the .ssh folder and the file if they don't exists. 显然,创建.ssh文件夹和文件(如果它们不存在)。 if authorized_keys exists, append the .pub content at his end after a new line so you won't override it current content. 如果authorized_keys存在,请在.pub内容的末尾添加新行,以免覆盖其当前内容。 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. 如果您希望每次运行脚本时都输入密码,则不要将此密钥添加到authorized_keys中。

now in your python script: 现在在您的python脚本中:

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. 这将从树莓派复制到您的计算机,如果要执行相反的操作,请切换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 知道使用os.popen() ,该程序将阻塞直到命令完成,并且直到cmd.read()才会执行该命令

If you don't like this method, you may have a look here and the links provided in this question 如果您不喜欢此方法,则可以在此处查看此问题中提供的链接

You may consider ftplib from python libraries . 您可以考虑使用python库中的 ftplib Then you will be able to copy file both using GUI client like Filezila and programmatically. 然后,您将能够使用GUI客户端(例如Filezila)和以编程方式复制文件。

from ftplib import FTP

On the Raspberry side, you must start an FTP server ( ftpd , vsftpd , etc.), they are legion on Debian. 在Raspberry方面,您必须启动FTP服务器( ftpdvsftpd等),它们是Debian上的一员。 On the client side, you can use a GUI client or make you own program like in python! 在客户端,您可以使用GUI客户端,也可以像在python中一样使自己的程序成为现实!

If you need a encrypted solution, you may use TLS or SSL over FTP. 如果需要加密的解决方案,则可以在FTP上使用TLS或SSL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM