Skip to content

SCP Examples

Andrew Lambert edited this page Nov 26, 2022 · 4 revisions

Secure Copy Protocol

SCP is a protocol for reading and writing files on a server over SSH. SCP is considered a legacy protocol, and lacks many of the features of its replacement SFTP. SCP operations are performed using the SSH.SCPStream class (a subclass of SSH.Channel.)

Download

This example downloads a file over SCP:

  Dim session As SSH.Session = SSH.Connect("ssh.example.com", 22, "username", "password")
  Dim download As New SSH.SCPStream(session, "/home/user/file.txt")
  Dim local As BinaryStream = BinaryStream.Create(SpecialFolder.Desktop.Child("file.txt"))
  Do Until download.EOF
    If download.PollReadable() Then
      local.Write(download.Read(download.BytesReadable))
    End If
  Loop
  download.Close
  local.Close

Upload

This example uploads a file over SCP. The size of the upload must be specified in advance and must be correct:

  Dim session As SSH.Session = SSH.Connect("ssh.example.com", 22, "username", "password")
  Dim local As FolderItem = SpecialFolder.Desktop.Child("file.txt")
  Dim upload As New SSH.SCPStream(session, "/home/user/" + local.Name, local.Permissions, local.Length, 0, 0)
  Dim stream As BinaryStream = BinaryStream.Open(local)
  Do Until stream.EOF
    If upload.PollWriteable() Then
      upload.Write(stream.Read(upload.BytesWriteable))
    End If
  Loop
  upload.Close
  stream.Close
Clone this wiki locally