* Pure-PHP implementation of SCP.
* The API for this library is modeled after the API from PHP's {@link http://php.net/book.ftp FTP extension}.
* Here's a short example of how to use this library:
* include 'Net/SSH2.php';
* $ssh = new Net_SSH2('www.domain.tld');
* if (!$ssh->login('username', 'password')) {
* $scp = new Net_SCP($ssh);
* $scp->put('abcd', str_repeat('x', 1024*1024));
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* @author Jim Wigginton <terrafrost@php.net>
* @copyright MMX Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
* Reads data from a local file.
define('NET_SCP_LOCAL_FILE', 1);
* Reads data from a string.
define('NET_SCP_STRING', 2);
* @see Net_SCP::_receive()
define('NET_SCP_SSH1', 1);
define('NET_SCP_SSH2', 2);
* Pure-PHP implementations of SCP.
* @author Jim Wigginton <terrafrost@php.net>
* Connects to an SSH server
* @param optional Integer $port
* @param optional Integer $timeout
public function __construct($ssh)
switch (strtolower(get_class($ssh))) {
$this->mode = NET_SCP_SSH2;
$this->packet_size = 50000;
$this->mode = NET_SCP_SSH1;
* Uploads a file to the SCP server.
* By default, Net_SCP::put() does not read from the local filesystem. $data is dumped directly into $remote_file.
* So, for example, if you set $data to 'filename.ext' and then do Net_SCP::get(), you will get a file, twelve bytes
* long, containing 'filename.ext' as its contents.
* Setting $mode to NET_SCP_LOCAL_FILE will change the above behavior. With NET_SCP_LOCAL_FILE, $remote_file will
* contain as many bytes as filename.ext does on your local filesystem. If your filename.ext is 1MB then that is how
* large $remote_file will be, as well.
* Currently, only binary mode is supported. As such, if the line endings need to be adjusted, you will need to take
* care of that, yourself.
* @param String $remote_file
* @param optional Integer $mode
* @param optional Callable $callback
public function put($remote_file, $data, $mode = NET_SCP_STRING, $callback = null)
if (!isset($this->ssh)) {
if (!$this->ssh->exec('scp -t "'.$remote_file.'"', false)) { // -t = to
$temp = $this->_receive();
if ($this->mode == NET_SCP_SSH2) {
$this->packet_size = $this->ssh->packet_size_client_to_server[NET_SSH2_CHANNEL_EXEC] - 4;
$remote_file = basename($remote_file);
if ($mode == NET_SCP_STRING) {
user_error("$data is not a valid file", E_USER_NOTICE);
$fp = @fopen($data, 'rb');
$this->_send('C0644 '.$size.' '.$remote_file."\n");
$temp = $this->_receive();
$temp = $mode & NET_SCP_STRING ? substr($data, $sent, $this->packet_size) : fread($fp, $this->packet_size);
if (is_callable($callback)) {
call_user_func($callback, $sent);
if ($mode != NET_SCP_STRING) {
* Downloads a file from the SCP server.
* Returns a string containing the contents of $remote_file if $local_file is left undefined or a boolean false if
* the operation was unsuccessful. If $local_file is defined, returns true or false depending on the success of the
* @param String $remote_file
* @param optional String $local_file
public function get($remote_file, $local_file = false)
if (!isset($this->ssh)) {
if (!$this->ssh->exec('scp -f "'.$remote_file.'"', false)) { // -f = from
if (!preg_match('#(?<perms>[^ ]+) (?<size>\d+) (?<name>.+)#', rtrim($this->_receive()), $info)) {
if ($local_file !== false) {
$fp = @fopen($local_file, 'wb');
while ($size < $info['size']) {
$data = $this->_receive();
// SCP usually seems to split stuff out into 16k chunks
if ($local_file === false) {
if ($local_file !== false) {
* Sends a packet to an SSH server
public function _send($data)
$this->ssh->_send_channel_packet(NET_SSH2_CHANNEL_EXEC, $data);
$data = pack('CNa*', NET_SSH1_CMSG_STDIN_DATA, strlen($data), $data);
$this->ssh->_send_binary_packet($data);
* Receives a packet from an SSH server
public function _receive()
return $this->ssh->_get_channel_packet(NET_SSH2_CHANNEL_EXEC, true);
if (!$this->ssh->bitmap) {
$response = $this->ssh->_get_binary_packet();
switch ($response[NET_SSH1_RESPONSE_TYPE]) {
case NET_SSH1_SMSG_STDOUT_DATA:
extract(unpack('Nlength', $response[NET_SSH1_RESPONSE_DATA]));
return $this->ssh->_string_shift($response[NET_SSH1_RESPONSE_DATA], $length);
case NET_SSH1_SMSG_STDERR_DATA:
case NET_SSH1_SMSG_EXITSTATUS:
$this->ssh->_send_binary_packet(chr(NET_SSH1_CMSG_EXIT_CONFIRMATION));
fclose($this->ssh->fsock);
user_error('Unknown packet received', E_USER_NOTICE);
* Closes the connection to an SSH server
$this->ssh->_close_channel(NET_SSH2_CHANNEL_EXEC, true);
$this->ssh->disconnect();