Scp Chilkat2 Perl Reference Documentation
Chilkat2::Scp
Current Version: 11.7.0
Markdown version of this page for AI tools (v 11.7.0) · all AI docs
Chilkat.Scp
Attach
Transfer local files to remote paths or download remote files to the
local filesystem using SCP.
Upload or download binary data, encoded data, text, or
Synchronize directory trees and use filtering options to include or
exclude selected files.
Set remote UNIX permissions, pass environment variables, and control
transfer-related behavior required by the remote server.
Monitor progress, scale progress percentages, abort active transfers, and
run supported operations as asynchronous tasks.
For an extended overview, see
Scp Class Overview.
Transfer files, text, and binary data over SCP using an existing SSH connection.
Chilkat.Scp is the Chilkat class for SCP file transfer over an
already-established Chilkat.Ssh connection. It supports
local-file upload and download, in-memory binary transfer, encoded binary
transfer, text transfer with explicit character sets, BinData
transfer, directory-tree synchronization, sync filtering, remote UNIX
permission overrides, environment variables, progress scaling, abort support,
and asynchronous task integration.
Uses an SSH connection
Scp to an authenticated Chilkat.Ssh
session rather than opening a separate network connection.
Upload and download files
Memory-based transfers
Chilkat.BinData without requiring temporary files.
Directory synchronization
Remote file options
Progress and async support
Chilkat.Ssh, initialize
Chilkat.Scp with that SSH connection, then upload, download, or
synchronize the desired files or in-memory data. Use Scp for
SCP-style transfers over SSH; use Chilkat.SFtp when the remote
server expects the SFTP protocol instead.
Object Creation
use Chilkat2;
my $scp = Chilkat2::Scp->new;
# or set properties (and RaiseError) at creation:
# my $scp = Chilkat2::Scp->new(VerboseLogging => 1, RaiseError => 1);Creates the object. The optional pairs set writable properties (and RaiseError) in order, as if set one by one; an unknown or read-only name croaks. use Chilkat2; loads every class; the Chilkat2/Scp.pm file holds only this class's documentation (perldoc Chilkat2::Scp).
Perl frees the object when its last reference goes (undef $scp, or the variable going out of scope); there is nothing to close. Objects returned by methods are independent and freed the same way. The class may be subclassed (use parent -norequire, 'Chilkat2::Scp';): an object is a blessed hash reference whose hash is free for the subclass's own fields.
When false (the default), a method that fails returns false (or undef), and the reason is in LastErrorText. When true, it dies with a Chilkat2::Error instead, as DBI does with its RaiseError. Objects returned by a method inherit the value of the object that returned them.
Errors
A Chilkat2::Error stringifies to the failed call's full LastErrorText, and has the methods text, class ('Chilkat2::Scp'), method (the method name) and object. Methods that look something up and find nothing return undef without dying (each such method says so), methods that answer a question (HasMember, IsUnlocked, ...) return a plain boolean, and property reads and writes never fail for Chilkat reasons.
Misuse always croaks, with your file and line, whatever RaiseError is: a wrong number of arguments, a reference or undef where a string or number is expected, an object of the wrong class, an out-of-range integer, a string containing a NUL character, a wide character in a byte string, or a write to a read-only property.
Strings are Perl character strings in both directions (length, regexes and :encoding layers see characters); binary data (BinData) is a byte string. Unlocking: call Chilkat2::Global->new->UnlockBundle('...') once at startup.
Properties
AbortCurrent
my $boolVal = $scp->AbortCurrent;
$scp->AbortCurrent($boolVal);
Set to 1 to request cancellation of the method currently executing on this object. Long-running network and file-transfer operations check this property periodically; a method that completes quickly may finish before the request is observed.
Both synchronous and asynchronous operations can be cancelled. To cancel a synchronous call, set this property from another thread on the same Scp object. Chilkat resets the property to 0 after the cancellation is processed. If no method is running, a previously set value is cleared when the next method begins.
LastMethodSuccess where applicable, and LastErrorText to determine the final outcome.DebugLogFilePath
my $strVal = $scp->DebugLogFilePath;
$scp->DebugLogFilePath($strVal);
If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.
Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.
Possible causes of hangs include:
- A timeout property set to 0, indicating an infinite timeout.
- A hang occurring within an event callback in the application code.
- An internal bug in the Chilkat code causing the hang.
HeartbeatMs
my $intVal = $scp->HeartbeatMs;
$scp->HeartbeatMs($intVal);
Specifies the interval, in milliseconds, between AbortCheck event callbacks during supported long-running operations. The callback allows the application to report activity or request cancellation before the operation completes.
The default value is 0, which disables periodic AbortCheck callbacks.
LastErrorHtml
my $strVal = $scp->LastErrorHtml;
Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastErrorText
my $strVal = $scp->LastErrorText;
Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
LastErrorXml
my $strVal = $scp->LastErrorXml;
Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.
topLastMethodSuccess
my $boolVal = $scp->LastMethodSuccess;
$scp->LastMethodSuccess($boolVal);
Indicates the success or failure of the most recent method call: 1 means success, 0 means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.
PercentDoneScale
my $intVal = $scp->PercentDoneScale;
$scp->PercentDoneScale($intVal);
Specifies the value that represents 100% completion in PercentDone event callbacks. The default is 100, which reports progress in whole-percent units. For example, when the scale is 1000, a callback value of 453 represents 45.3% completion.
The value is limited to the range 10 through 100000. Increasing the scale provides finer numeric granularity for operations whose progress can be measured.
SendEnv
my $strVal = $scp->SendEnv;
$scp->SendEnv($strVal);
Specifies environment variables to send for each SCP upload or download. Set the property to a JSON object whose member names are environment-variable names and whose values are strings.
{
"LCS_PASSWORD": "myPassword",
"SOME_ENV_VAR": "some_value"
}
SyncedFiles
my $strVal = $scp->SyncedFiles;
$scp->SyncedFiles($strVal);
Contains the local paths of the files transferred by the most recent call to SyncTreeUpload or SyncTreeDownload. Paths are listed one per line.
- After an upload, each line is the full local path of a source file that was uploaded.
- After a download, each line is the full local path of a destination file that was downloaded.
SyncMustMatch
my $strVal = $scp->SyncMustMatch;
$scp->SyncMustMatch($strVal);
Specifies a semicolon-separated list of wildcard patterns for filenames that are eligible for transfer by SyncTreeUpload and SyncTreeDownload. A file is considered only when its filename matches at least one pattern.
For example, *.xml;*.txt;*.csv limits synchronization to XML, text, and CSV files. The default empty string imposes no include restriction.
SyncMustMatchDir and SyncMustNotMatchDir.SyncMustMatchDir
my $strVal = $scp->SyncMustMatchDir;
$scp->SyncMustMatchDir($strVal);
Specifies a semicolon-separated list of wildcard patterns for directory names that may be traversed by SyncTreeUpload and SyncTreeDownload. When this property is nonempty, synchronization descends only into directories whose names match at least one pattern.
For example, a*;b*;c* permits traversal into directories whose names begin with a, b, or c. The default empty string permits traversal into all directories, subject to SyncMustNotMatchDir.
SyncMustNotMatch
my $strVal = $scp->SyncMustNotMatch;
$scp->SyncMustNotMatch($strVal);
Specifies a semicolon-separated list of wildcard patterns for filenames that must be skipped by SyncTreeUpload and SyncTreeDownload.
For example, *.tmp;*.bak;*.log excludes temporary, backup, and log files. The default empty string excludes no files.
SyncMustNotMatchDir to prevent synchronization from entering selected directories and their subtrees.SyncMustNotMatchDir
my $strVal = $scp->SyncMustNotMatchDir;
$scp->SyncMustNotMatchDir($strVal);
Specifies a semicolon-separated list of wildcard patterns for directory names that must not be traversed by SyncTreeUpload and SyncTreeDownload. A matching directory and everything below it are skipped.
For example, temp*;cache*;.git prevents traversal into temporary, cache, and Git metadata directories. The default empty string excludes no directories.
UncommonOptions
my $strVal = $scp->UncommonOptions;
$scp->UncommonOptions($strVal);
Provides a comma-separated list of specialized compatibility options. The default is the empty string, which is appropriate for normal SCP transfers.
| Keyword | Behavior |
|---|---|
FilenameOnly | Uses only the filename, rather than the complete target path, in the remote scp -t command. This option exists for compatibility with systems such as certain LANCOM routers. |
ProtectFromVpn | On Android, attempts to route the SCP connection outside an installed or active VPN. |
UnixPermOverride
my $strVal = $scp->UnixPermOverride;
$scp->UnixPermOverride($strVal);
Specifies an octal UNIX permission mode to apply to files uploaded by SCP, overriding the permissions derived from the local source file. For example, set this property to 0644 to grant read/write permission to the owner and read-only permission to the group and others.
The default is the empty string, which causes the uploaded file's remote permissions to be based on the local file's permissions.
0600, 0644, or 0755. The remote SSH account must still have permission to create or replace the file, and server-side policy may restrict the final mode.VerboseLogging
my $boolVal = $scp->VerboseLogging;
$scp->VerboseLogging($boolVal);
If set to 1, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is 0. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.
Version
my $strVal = $scp->Version;
Methods
DownloadBd
# $bd is a Chilkat2::BinData
# $success is true or false
my $success = $scp->DownloadBd($remotePath, $bd);
Downloads the file at $remotePath and appends its exact bytes to the existing contents of the BinData object in $bd.
An absolute remote path begins with /. A relative path is interpreted relative to the authenticated SSH user's home directory.
$bd before downloading. Clear the BinData object first when the downloaded file should replace, rather than follow, its current contents.Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
DownloadBinaryEncoded
# $encoding is a string
# $str is a string or undef
my $str = $scp->DownloadBinaryEncoded($remotePath, $encoding);
Downloads the file at $remotePath and returns its bytes encoded as text using the binary encoding named by $encoding, such as base64 or hex.
An absolute remote path begins with /. A relative path is interpreted relative to the authenticated SSH user's home directory.
$encoding selects a text representation of arbitrary bytes; it is not a character set and does not decode the remote file as text. Use DownloadString when the remote file contains text that must be decoded with a charset.Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
DownloadFile
# $localPath is a string
# $success is true or false
my $success = $scp->DownloadFile($remotePath, $localPath);
Downloads the file at $remotePath from the SSH server and writes its bytes to the local filesystem path in $localPath.
An absolute remote path begins with /. A relative remote path is interpreted relative to the authenticated SSH user's home directory. The application must have permission to create or replace the local destination file.
Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
DownloadString
# $charset is a string
# $str is a string or undef
my $str = $scp->DownloadString($remotePath, $charset);
Downloads the file at $remotePath, decodes its bytes using the character encoding named by $charset, and returns the resulting text.
An absolute remote path begins with /. A relative path is interpreted relative to the authenticated SSH user's home directory.
$charset describes how the remote file’s bytes represent characters, for example utf-8, windows-1252, or iso-8859-1. Using the wrong charset can produce incorrect characters. Use a binary download method for files that are not text.Returns undef on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
SyncTreeDownload
# $localRoot is a string
# $mode is an integer
# $bRecurse is a boolean
# $success is true or false
my $success = $scp->SyncTreeDownload($remoteRoot, $localRoot, $mode, $bRecurse);
Synchronizes files from the remote directory tree rooted at $remoteRoot to the local directory rooted at $localRoot. $bRecurse controls recursion: set it to 1 to descend into subdirectories, or 0 to process only files directly in the root directory.
An absolute remote root begins with /; a relative remote root is interpreted relative to the authenticated SSH user's home directory. A relative local root is interpreted relative to the application's current working directory.
| Mode | Files transferred |
|---|---|
0 | Download every eligible file. |
1 | Download files that do not already exist locally. |
2 | Download files that are missing locally or whose remote last-modified time is newer than the local file. |
3 | Download only files whose remote last-modified time is newer. A remote file that has no local counterpart is not downloaded. |
5 | Download files that are missing locally or whose size differs from the local file. |
6 | Download files that are missing locally, differ in size, or have a newer remote last-modified time. |
The filename and directory filters in SyncMustMatch, SyncMustNotMatch, SyncMustMatchDir, and SyncMustNotMatchDir are applied during traversal. After completion, SyncedFiles lists the full local paths of files actually downloaded.
2, 3, and 6 compare last-modified times. Incorrect server timestamps or clock differences between systems can affect which file is considered newer.Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
SyncTreeUpload
# $remoteBaseDir is a string
# $mode is an integer
# $bRecurse is a boolean
# $success is true or false
my $success = $scp->SyncTreeUpload($localBaseDir, $remoteBaseDir, $mode, $bRecurse);
Synchronizes files from the local directory tree rooted at $localBaseDir to the remote directory rooted at $remoteBaseDir. $bRecurse controls recursion: set it to 1 to descend into subdirectories, or 0 to process only files directly in the local base directory.
A relative local base directory is interpreted relative to the application's current working directory. An absolute remote base directory begins with /; a relative remote base directory is interpreted relative to the authenticated SSH user's home directory.
| Mode | Files transferred |
|---|---|
0 | Upload every eligible file. |
1 | Upload files that do not already exist on the remote server. |
2 | Upload files that are missing remotely or whose local last-modified time is newer than the remote file. |
3 | Upload only files whose local last-modified time is newer. A local file that has no remote counterpart is not uploaded. |
4 | Upload files that are missing remotely or whose size differs from the remote file. |
5 | Upload files that are missing remotely, differ in size, or have a newer local last-modified time. |
The filename and directory filters in SyncMustMatch, SyncMustNotMatch, SyncMustMatchDir, and SyncMustNotMatchDir are applied during traversal. After completion, SyncedFiles lists the full local paths of files actually uploaded.
2, 3, and 5 compare last-modified times. Incorrect server timestamps or clock differences between systems can affect which file is considered newer.Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
UploadBd
# $bd is a Chilkat2::BinData
# $success is true or false
my $success = $scp->UploadBd($remotePath, $bd);
Uploads all bytes currently contained in the BinData object in $bd to the remote file at $remotePath. The data is transferred without character decoding or other transformation.
An absolute remote path begins with /. A relative path is interpreted relative to the authenticated SSH user's home directory. The remote parent directory must already exist.
$bd and does not remove or modify those bytes.Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
UploadBinaryEncoded
# $encodedData is a string
# $encoding is a string
# $success is true or false
my $success = $scp->UploadBinaryEncoded($remotePath, $encodedData, $encoding);
Decodes the text in $encodedData using the binary encoding named by $encoding, then uploads the resulting bytes to the remote file at $remotePath. Typical encoding names include base64 and hex.
An absolute remote path begins with /. A relative path is interpreted relative to the authenticated SSH user's home directory. The remote parent directory must already exist.
$encoding is base64, Chilkat Base64-decodes $encodedData and transfers the decoded binary bytes. This method is for encoded binary representations, not character-set conversion.Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
UploadFile
# $remotePath is a string
# $success is true or false
my $success = $scp->UploadFile($localPath, $remotePath);
Uploads the local filesystem file at $localPath to the remote path in $remotePath. The file is transferred as raw bytes.
An absolute remote path begins with /. A relative remote path is interpreted relative to the authenticated SSH user's home directory. The remote parent directory must already exist, and the SSH account must have permission to create or replace the destination file.
Remote permissions are normally derived from the local file. Set UnixPermOverride when a specific remote UNIX mode is required.
UploadFile copies the file bytes without line-ending or character-set conversion.Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
UploadString
# $textData is a string
# $charset is a string
# $success is true or false
my $success = $scp->UploadString($remotePath, $textData, $charset);
Encodes the text in $textData using the character encoding named by $charset, then uploads the resulting bytes to the remote file at $remotePath.
An absolute remote path begins with /. A relative path is interpreted relative to the authenticated SSH user's home directory. The remote parent directory must already exist.
$charset controls how characters are converted to bytes, for example utf-8, windows-1252, or iso-8859-1. SCP itself has no text mode; any line-ending characters already present in $textData are encoded and transferred as part of the text.Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
UseSsh
# $success is true or false
my $success = $scp->UseSsh($sshConnection);
Associates this Scp object with the connected and authenticated Ssh object in $sshConnection. Subsequent SCP operations use that existing SSH session rather than opening a separate network connection.
Connection, proxy, timeout, host-key verification, algorithm, logging, and other transport-related settings belong to the Ssh object and therefore apply to the SCP operations performed through it. Keep the SSH object connected and available for as long as the Scp object is using it.
Scp performs SCP-style transfers over SSH and requires compatible SCP support on the remote system. Use Chilkat.SFtp when the server expects the SSH File Transfer Protocol subsystem or when directory listing and remote file-management features are needed.Returns true on success, false on failure; LastErrorText says why. With RaiseError on, a failure dies with a Chilkat2::Error instead (it stringifies to the LastErrorText); the object remains usable either way.
Events
All Chilkat methods are synchronous: the call returns when the work is done. During a long-running call, Chilkat2::Scp reports progress to handlers installed with on, which takes Event => CODE pairs (closures, so a handler can use the variables around it):
$scp->on(
PercentDone => sub { my ($pctDone) = @_; print "$pctDone%\n"; return 0 }, # a true return aborts
ProgressInfo => sub { my ($name, $value) = @_; print "$name: $value\n" },
);
$scp->HeartbeatMs(250); # the AbortCheck handler is called 4 times per secondInstalls the handlers given, replacing those installed before for the same events; undef instead of a CODE reference removes one, and $scp->on() with no arguments returns the installed handlers as a list of pairs. The event names are checked: a misspelled one croaks, and nothing is installed.
The AbortCheck handler is called at the interval set by the HeartbeatMs property (0, the default, disables it); PercentDone is called when an operation's completion percentage is known; ProgressInfo delivers named progress values. A true return from the AbortCheck or PercentDone handler aborts the running method, which then fails in the usual way (false or undef, or a Chilkat2::Error under RaiseError).
Handlers run during the method call, on the thread that called it. Inside a handler, other objects may be used, but calling a method of the same object (or its on) croaks. A die in a handler aborts the running method and is re-thrown from the method call; the object remains usable. A handler that uses $scp itself makes a reference cycle: weaken the captured copy (Scalar::Util) or remove the handler when done.
AbortCheck
$scp->on(AbortCheck => sub { ... });
Enables a method call to be aborted by triggering the AbortCheck event at intervals defined by the HeartbeatMs property. If HeartbeatMs is set to its default value of 0, no events will occur. For instance, set HeartbeatMs to 200 to trigger 5 AbortCheck events per second.
Example
my $deadline = time() + 30; # give up after 30 seconds
$scp->HeartbeatMs(250); # call the handler 4 times per second
$scp->on(AbortCheck => sub { return time() > $deadline });PercentDone
$scp->on(PercentDone => sub { ... });
This provides the percentage completion for any method involving network communications or time-consuming processing, assuming the progress can be measured as a percentage. This event is triggered only when it's possible and logical to express the operation's progress as a percentage. The pctDone argument will range from 1 to 100. For methods that finish quickly, the number of PercentDone callbacks may vary, but the final callback will have pctDone equal to 100. For longer operations, callbacks will not exceed one per percentage point (e.g., 1, 2, 3, ..., 98, 99, 100).
The PercentDone callback also acts as an AbortCheck event. For fast methods where PercentDone fires, an AbortCheck event may not trigger since the PercentDone callback already provides an opportunity to abort. For longer operations, where time between PercentDone callbacks is extended, AbortCheck callbacks enable more responsive operation termination.
To abort the operation, set the abort output argument to 1. This will cause the method to terminate and return a failure status or corresponding failure value.
Example
$scp->on(PercentDone => sub {
my ($pctDone) = @_; # 0 to 100
print "Percent done: $pctDone\n";
return 0; # return a true value to abort the method in progress
});ProgressInfo
$scp->on(ProgressInfo => sub { ... });
This event callback provides tag name/value pairs that detail what occurs during a method call. To discover existing tag names, create code to handle the event, emit the pairs, and review them. Most tag names are self-explanatory.
Note: Some Chilkat methods don't fire any ProgressInfo events.
Example
$scp->on(ProgressInfo => sub {
my ($name, $value) = @_;
print "$name: $value\n";
});