0

I'd like to create a script to copy some files from my Windows computer to the Raspberry Pi over local network and run this on the Pi.

As I understand I have to run an SSH server on the PC in order to connect the Pi to it, for that I was trying to use Windows's OpenSSH but I'm not even sure that it works properly because I couldn't ssh into it from a terminal.

And I'm a bit confused how to make the copy from a script and not from a terminal.

So, my question is, can I use OpenSSH as a server on my PC, or otherwise what options do I have, and how to make the copy from the Pi. If you could give me some pointers or examples, that would be great!

Thanks in advance!

Razero
  • 125
  • 1
  • 2
  • 5
  • One suggestion is to use serial communication through USB/TTL adapter. Both PC and Rpi can run python program to talk in serial (9,600 baud N81 etc) You can try in steps: (1) Window python program loop back to test local read/write are OK, (2) same for Rpi , (3) Use Windows puTTY to talk to Rpi python, (4) vice versa, (5) the real thing: Win python talk to Rpi python using serial 9,600, N81, ... – tlfong01 Apr 26 '19 at 14:41
  • You might also read my answer to this question - https://raspberrypi.stackexchange.com/questions/96184/serial-to-arduino-totally-non-responsive – tlfong01 Apr 26 '19 at 14:51
  • Unfortunately I don't have the option to use serial, I can only connect the two over local network – Razero Apr 26 '19 at 21:36
  • Ah, sorry, I did not read your post carefully. @Heath Raftery already gave an excellent answer. I was about to suggest other things like Bluetooth or other in ISM band. But not sure if you can consider that. – tlfong01 Apr 27 '19 at 01:15

1 Answers1

0

Sure, that will work, and there are lots of other options too. The best depends on your particular needs.

The three most suitable protocols to choose from are:

  1. SSH - great for interactive work where you type commands and look at the output. Overkill for a file transfer script but it includes a simpler protocol called SFTP which is more suitable. Very well supported and easy to setup outside of Windows. Bit trickier on Windows.
  2. FTP - specifically designed for file transfer over a network. Very well supported and easy to setup outside of Windows. Bit trickier on Windows.
  3. SMB - specifically designed to share files with Windows. Native support on Windows and not too bad elsewhere.

Then, given these are all Client-Server protocols, you have two directions to chose from:

  1. Windows server, Pi client - means you need to install and configure a server on Windows, which is usually a bit trickier. But means all the action initiation (the script) can happen on the Pi.
  2. Pi server, Windows client - the Pi already has the servers, so means the client install/config is easier on Windows. But means you need to initiate the action on the Windows machine, which you may not want to do.

Finally, you need the script. Assuming the Pi is the client, there's really no need for Python. But you can use it if you like. There are two main options:

  1. Wrap the built-in Pi's clients (eg. ssh, sftp, ftp, samba) in your favourite scripting language.
  2. Use a library written for your favourite scripting language (eg. for Python: pysftp, paramiko, ftplib, pysmb)

Depending on your particular requirements, option 1 is probably the easiest if you're running a native script on the Pi and option 2 if you want to use Python. Here's what it might look like as a native expect script:

#!/usr/bin/expect
export PW="your_ssh_password"
expect -c 'spawn sftp your_ssh_username@windows.ip.address; 
expect "*Password: ";
send "$env(PW)\r";
expect "sftp>";
send "cd /Users/user \r";
expect "sftp>";
send "get file.txt \r";
expect "sftp>";
send "bye \r"'

or as a Python script, using pysftp:

import pysftp

cnopts = pysftp.CnOpts()
cnopts.hostkeys = None

# Make connection to sFTP
with pysftp.Connection(windows.ip.address,
                       username=your_ssh_username,
                       password=your_ssh_password,
                       cnopts = cnopts
                       ) as sftp:
    sftp.get('/Users/user/file.txt', '/local/path/file.txt')
    print(file) ## None

sftp.close()

Plenty of options to streamline this depending on your needs. See how you go and feel free to ask a more specific question.

Heath Raftery
  • 265
  • 1
  • 8
  • Thank you! Very nice and detailed answer, and I think your second example is just what I need. I'll mark it as a solution as soon as I've tried it, but I believe it'll work for me – Razero Apr 27 '19 at 09:15