Getting Started with SSH and PSCP for the Raspberry Pi

This page covers the control of a Raspberry Pi by another computer using SSH (Secure Shell) and also the use of SSH by a Raspberry Pi. (Some of it was first published on CodeShow).

It is sometimes inconvenient to connect a Pi to a monitor, for example if your SD Card is set up to use a different type of monitor or if your Pi is acting as a headless web server. It is easy to install PuTTY on your PC then use it to control your Pi:

Using PuTTY

Using PuTTY

The response is:

Response

Response

Once you enter the password you are presented with the Pi's command prompt and can issue commands as if you were using a keyboard connected directly to the Pi. For example, the command ls will list the contents of the home directory.

James Farnsworth demonstrated that you can control one Pi with another connected to the same network by using the inbuilt ssh command:

ssh pi@192.168.0.4

You may obtain a warning that the authenticity of host cannot be established. If so, accept the invitation to continue.

File transfer using PuTTY's pscp.exe

James demonstrated file transfer from a PC to a Raspberry Pi on the same network with this command:

"C:\Program Files\PuTTY\pscp.exe" .\Documents\testfromwindows.txt root@172.24.77.150:/var/www/thisisanexperiment.txt

After entering the password in response to

root@172.24.77.150's password:

the file testfromwindows.txt is copied to the web directory /var/www and renamed thisisanexperiment.txt.

You can also supply the password within the command, as we do in the following Lazarus demonstration. The application enables the user to browse for each file to upload. The Pascal code and the code of the form follow a screenshot of a successful upload of a student online game to the web server. (See the previous page for guidance on setting up the server). If you have many files to upload, use FileZilla.

Successful Upload

Successful Upload

Pascal Code of Upload Demo

unit uUpload;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, Process;

type
  TForm1 = class(TForm)
    btnSelectFile: TButton;
    Label1: TLabel;
    Memo1: TMemo;
    OpenDialog1: TOpenDialog;
    procedure btnSelectFileClick(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

procedure TForm1.btnSelectFileClick(Sender: TObject);
var
  Proc: TProcess;
begin
  with OpenDialog1 do
    begin
      if Execute then
        begin
          InitialDir := 'C:\';
          Proc := TProcess.Create(nil);
             try
               Proc.CommandLine := '"C:\Program Files (x86)\PuTTY\pscp.exe" -pw yourpassword ' + Filename + ' pi@192.168.0.4:/var/www';
               Proc.Options := [poUsePipes, poWaitOnExit];
               Proc.Execute;
               Memo1.Lines.LoadFromStream(Proc.Output);
             finally
               Proc.free;
             end;
        end;
    end;
end;

end.
    

Code of Form

object Form1: TForm1
  Left = 452
  Height = 159
  Top = 224
  Width = 470
  Caption = 'Upload to Pi Web Server'
  ClientHeight = 159
  ClientWidth = 470
  LCLVersion = '1.2.4.0'
  object btnSelectFile: TButton
    Left = 344
    Height = 25
    Top = 7
    Width = 75
    Caption = 'File Dialogue'
    OnClick = btnSelectFileClick
    TabOrder = 0
  end
  object Label1: TLabel
    Left = 17
    Height = 15
    Top = 7
    Width = 300
    Caption = 'Press button, select file,  click the Open button then wait '
    ParentColor = False
  end
  object Memo1: TMemo
    Left = 8
    Height = 98
    Top = 48
    Width = 456
    TabOrder = 1
  end
  object OpenDialog1: TOpenDialog
    left = 432
    top = 8
  end
end   

Programming - a skill for life!

Setting up the Raspberry Pi, installing Lazarus, use of the GPIO port (with and without PiFace), web servers and remote control