Thursday 7 January 2016

Setting up file transfer via WiFi

The home.sh program provides a method for copying files from the Pi (/home/camera by default) to
a remote location, usually via a WiFi connection. It is initiated by the pwr_butt script when a shutdown
has been requested but can be run independently. The program is still being written and may have issues.

This is a very basic setup guide to provide a quick and simple way to transfer files from the Pi to
another system. It is by no means the most secure, complete or only method possible.

Generate the SSH key
So we can connect to the remote host from the Pi without using a password we need to generate a key
that we add on the remote host to authenticate our Pi with.

On the Pi generate a key… (for the 'Enter …' questions you can just press Return).
[root@archdash ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:8vDo97GEEW+SdfkyyHlL6XsUOQT9mHVyxICnqTxhnHQ root@archpico3
The key's randomart image is:
.
.
.

 Now we copy this newly generated key to our remote destination host.
 [root@archdash ~]# ssh-copy-id root@remote_host
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@remote_host's password:

Number of key(s) added: 1

--- To do this manually ---
View the generated public key. We are going to copy this to a file on our remote destination host.
An important point is that this entry is all one line, no line breaks.
[root@archdash ~]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuhmmTfEMZDWafZS/jxfVH/L5Mu5f70y+uyOZ5ici8WJor1mg6aszFmnuZAcOXLebyAeCdJQ3VOtNee/jNwPT2AMm/vYWBb+Usmu7oMYl6MblcN81r8PySjIQ4Ahhh1l762WFI8NIHHJl8Zpc2L0ud24PclFEhnTtlur2NwfZUZdhz+vcDhbU8VAlv1bZWh0reXJVw7yrm2wF6VBdyQ0XsD/aXJjCwRTBcjHfxWg/+1ty4YlBeu00Ohhx8s9oEs+8L7JZRQVwR7t8OPfnv1iStDRODvB2Ad2PgEAshe91VuDLYIR9zM6a4coJ3u/MhUrnJsBjfF2vilXbKkT0yuRaz root@archpico3

On the remote destination host edit the authorized_keys file and add the key you generated earlier on the Pi.
[root@remote_host]# vi /root/.ssh/authorized_keys
(Add in the id_rsa.pub key you created on the Pi, remember this entry should be one complete line)
---

Try a quick test, create an empty file on the Pi and copy it with scp to the remote host.
[root@archdash touch /tmp/test
[root@archdash ~]# scp /tmp/test root@remote_host:/tmp/
test                                                                                                   100%    0     0.0KB/s   00:00  

If it worked correctly it should have copied the file without requesting for a password.

Rsync
I would recommend using rsync to transfer the files from the Pi to your remote location. If the transfer is
interrupted it can continue copying the files which do no match on the remote location. scp is also an
option but it will copy all files even if they were previously transferred successfully.

If you are using Raspbian rsync will already be installed. It will also need to be installed on your
destination host.

To install rsync on Arch Linux:-
[root@archdash ~]# pacman -S rsync
(rsync also needs to be installed on the destination host).

Configure WiFi
[+] Arch Linux configuration [-] Arch Linux configuration

To help negotiate a WiFi connection we first need to install the wpa_supplicant program.
[root@archdash ~]# pacman -S wpa_supplicant

Now create a profile for your WiFi connection. You can look in the /etc/netctl/examples directory and
choose one that best suits your environment. Here I am copying an example WPA profile for my setup.
I only needed to add my WiFi network name and my WPA key.
[root@archdash ~]# cd /etc/netctl
[root@archdash netctl]# cp ./examples/wireless-wpa ./
(if you want a static WiFi IP try ./examples/wireless-wpa-static)
[root@archdash netctl]# vi wireless-wpa
Description='A simple WPA encrypted wireless connection'
Interface=wlan0
Connection=wireless
Security=wpa
IP=dhcp
ESSID='my_wfi_router'
Key='my_wireless_passphrase
# Uncomment this if your ssid is hidden
#Hidden=yes
# Set a priority for automatic profile selection
#Priority=10

If you would like the WiFi to always start when the Pi boots up then enable it using the name of the
profile you created.
[root@archdash ~]# netctl enable wireless-wpa
You can start or stop the WiFi connection in the same manner.
[root@archdash ~]# netctl start wireless-wpa

[+] Raspbian configuration [-] Raspbian configuration

Edit the wpa_supplicant.conf file and add your WiFi setup information….
root@raspbiandash:~# vi /etc/wpa_supplicant/wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1

network={
ssid="wifi_router_name"
psk="wifi_passkey"

# Protocol type can be: RSN (for WP2) and WPA (for WPA1)
proto=RSN

# Key management type can be: WPA-PSK or WPA-EAP (Pre-Shared or Enterprise)
key_mgmt=WPA-PSK

# Pairwise can be CCMP or TKIP (for WPA2 or WPA1)
pairwise=TKIP

#Authorization option should be OPEN for both WPA1/WPA2 (in less commonly used are SHARED and LEAP)
auth_alg=OPEN
}

This should be all you need if you want a simple DHCP connection. Try restarting the network service
and see if the interface comes up.
root@raspbiandash:~# service networking restart
root@raspbiandash:~# ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr 00:e2:1d:e9:1b:b3  
          inet addr:192.168.1.99  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:325 errors:0 dropped:5 overruns:0 frame:0
          TX packets:127 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:63741 (62.2 KiB)  TX bytes:23941 (23.3 KiB)

If you wish to use a static IP then edit the /etc/network/interfaces file and add in your network
information (shown in bold).
root@raspbiandash:~# vi /etc/network/interfaces
------
auto lo
iface lo inet loopback

auto eth0
allow-hotplug eth0
iface eth0 inet manual

auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.1.99
netmask 255.255.255.0
gateway 192.168.1.254
broadcast 192.168.1.255 (optional)
network 192.168.1.0 (optional)
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

auto wlan1
allow-hotplug wlan1
iface wlan1 inet manual
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
------
Test the interface is up and running by restarting the network services or with a reboot.

No comments:

Post a Comment