PHP versions 4 and 5
Here are some examples of how to use this library:
login('username', 'password')) {
exit('Login Failed');
}
echo $ssh->exec('pwd');
echo $ssh->exec('ls -la');
?>
setPassword('whatever');
$key->loadKey(file_get_contents('privatekey'));
$ssh = new Net_SSH2('www.domain.tld');
if (!$ssh->login('username', $key)) {
exit('Login Failed');
}
echo $ssh->read('username@username:~$');
$ssh->write("ls -la\n");
echo $ssh->read('username@username:~$');
?>
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
THE SOFTWARE.
Constants
NET_SSH2_MASK_CONSTRUCTOR
= 1
NET_SSH2_MASK_LOGIN
= 2
NET_SSH2_MASK_SHELL
= 4
NET_SSH2_CHANNEL_EXEC
= 0
NET_SSH2_CHANNEL_SHELL
= 1
NET_SSH2_LOG_SIMPLE
= 1
Returns the message numbers
NET_SSH2_LOG_COMPLEX
= 2
Returns the message content
NET_SSH2_READ_SIMPLE
= 1
Returns when a string matching $expect exactly is found
NET_SSH2_READ_REGEX
= 2
Returns when a string matching the regular expression $expect is found
Closes and flushes a channel Net_SSH2 doesn't properly close most channels. For exec() channels are normally closed by the server
and for SFTP channels are presumably closed when the client disconnects. This functions is intended
for SCP more than anything.
Arguments
Name
Type
Description
Default
$client_channel
Integer
Return value
Type
Description
Boolean
Tags
Name
Description
access
private
_define_array(
)
:
n/a
Description
Define Array Takes any number of arrays whose indices are integers and whose values are strings and defines a bunch of
named constants from it, using the value as the name of the constant and the index as the value of the constant.
If any of the constants that would be defined already exists, none of the constants will be defined.
Return value
Type
Description
n/a
n/a
Tags
Name
Description
access
private
_disconnect(
Integer
$reason,
)
:
Boolean
Description
Disconnect
Arguments
Name
Type
Description
Default
$reason
Integer
Return value
Type
Description
Boolean
Tags
Name
Description
access
private
_filter(
$payload,
)
:
String
Description
Filter Binary Packets Because some binary packets need to be ignored...
Login via keyboard-interactive authentication See {@link http://tools.ietf.org/html/rfc4256 RFC4256} for details. This is not a full-featured keyboard-interactive authenticator.
Arguments
Name
Type
Description
Default
$username
String
$password
String
Return value
Type
Description
Boolean
Tags
Name
Description
access
private
_keyboard_interactive_process(
)
:
Boolean
Description
Handle the keyboard-interactive requests / responses.
Return value
Type
Description
Boolean
Tags
Name
Description
access
private
_send_binary_packet(
String
$data,
)
:
Boolean
Description
Sends Binary Packets See '6. Binary Packet Protocol' of rfc4253 for more info.
Execute Command If $block is set to false then Net_SSH2::_get_channel_packet(NET_SSH2_CHANNEL_EXEC) will need to be called manually.
In all likelihood, this is not a feature you want to be taking advantage of.
Arguments
Name
Type
Description
Default
$command
String
$block
n/a
true
Return value
Type
Description
String
Tags
Name
Description
access
public
getCompressionAlgorithmsClient2Server(
)
:
Array
Description
Return a list of the compression algorithms the server supports, when receiving stuff from the client.
Return value
Type
Description
Array
Tags
Name
Description
access
public
getCompressionAlgorithmsServer2Client(
)
:
Array
Description
Return a list of the compression algorithms the server supports, when sending stuff to the client.
Return value
Type
Description
Array
Tags
Name
Description
access
public
getEncryptionAlgorithmsClient2Server(
)
:
Array
Description
Return a list of the (symmetric key) encryption algorithms the server supports, when receiving stuff from the client.
Return value
Type
Description
Array
Tags
Name
Description
access
public
getEncryptionAlgorithmsServer2Client(
)
:
Array
Description
Return a list of the (symmetric key) encryption algorithms the server supports, when sending stuff to the client.
Return value
Type
Description
Array
Tags
Name
Description
access
public
getErrors(
)
:
String
Description
Returns all errors
Return value
Type
Description
String
Tags
Name
Description
access
public
getKexAlgorithms(
)
:
Array
Description
Return a list of the key exchange algorithms the server supports.
Return value
Type
Description
Array
Tags
Name
Description
access
public
getLanguagesClient2Server(
)
:
Array
Description
Return a list of the languages the server supports, when receiving stuff from the client.
Return value
Type
Description
Array
Tags
Name
Description
access
public
getLanguagesServer2Client(
)
:
Array
Description
Return a list of the languages the server supports, when sending stuff to the client.
Return value
Type
Description
Array
Tags
Name
Description
access
public
getLastError(
)
:
String
Description
Returns the last error
Return value
Type
Description
String
Tags
Name
Description
access
public
getLog(
)
:
String
Description
Returns a log of the packets that have been sent and received. Returns a string if NET_SSH2_LOGGING == NET_SSH2_LOG_COMPLEX, an array if NET_SSH2_LOGGING == NET_SSH2_LOG_SIMPLE and false if !defined('NET_SSH2_LOGGING')
Return value
Type
Description
String
or Array
Tags
Name
Description
access
public
getMACAlgorithmsClient2Server(
)
:
Array
Description
Return a list of the MAC algorithms the server supports, when receiving stuff from the client.
Return value
Type
Description
Array
Tags
Name
Description
access
public
getMACAlgorithmsServer2Client(
)
:
Array
Description
Return a list of the MAC algorithms the server supports, when sending stuff to the client.
Return value
Type
Description
Array
Tags
Name
Description
access
public
getServerHostKeyAlgorithms(
)
:
Array
Description
Return a list of the host key (public key) algorithms the server supports.
Return value
Type
Description
Array
Tags
Name
Description
access
public
getServerIdentification(
)
:
String
Description
Return the server identification.
Return value
Type
Description
String
Tags
Name
Description
access
public
getServerPublicHostKey(
)
:
Mixed
Description
Returns the server public host key. Caching this the first time you connect to a server and checking the result on subsequent connections
is recommended. Returns false if the server signature is not signed correctly with the public host key.
Returns the output of an interactive shell Returns when there's a match for $expect, which can take the form of a string literal or,
if $mode == NET_SSH2_READ_REGEX, a regular expression.
Execution Bitmap The bits that are set reprsent functions that have been called already. This is used to determine
if a requisite function has been successfully executed. If not, an error should be thrown.
Array
public
$channel_buffers
=
array()
Channel Buffers If a client requests a packet from one channel but receives two packets from another those packets should
be placed in a buffer
Array
public
$channel_extended_data_type_codes
=
array()
SSH_MSG_CHANNEL_EXTENDED_DATA's data_type_codes
Array
public
$channel_open_failure_reasons
=
array()
SSH_MSG_CHANNEL_OPEN_FAILURE 'reason codes', defined in RFC4254
Array
public
$channel_status
=
array()
Channel Status Contains the type of the last sent message
Array
public
$compression_algorithms_client_to_server
=
Compression Algorithms: Client to Server
Array
public
$compression_algorithms_server_to_client
=
Compression Algorithms: Server to Client
Object
public
$decrypt
=
false
Server to Client Encryption Object
Integer
public
$decrypt_block_size
=
8
Block Size for Client to Server Encryption
Array
public
$disconnect_reasons
=
array()
Disconnection Message 'reason codes' defined in RFC4253
Object
public
$encrypt
=
false
Client to Server Encryption Object
Integer
public
$encrypt_block_size
=
8
Block Size for Server to Client Encryption "Note that the length of the concatenation of 'packet_length',
'padding_length', 'payload', and 'random padding' MUST be a multiple
of the cipher block size or 8, whichever is larger. This constraint
MUST be enforced, even when using stream ciphers."
-- http://tools.ietf.org/html/rfc4253#section-6
Array
public
$encryption_algorithms_client_to_server
=
Encryption Algorithms: Client to Server
Array
public
$encryption_algorithms_server_to_client
=
Encryption Algorithms: Server to Client
String
public
$errors
=
array()
Error information
String
public
$exchange_hash
=
false
Exchange hash The current exchange hash
Object
public
$fsock
=
The Socket Object
Integer
public
$get_seq_no
=
0
Get Sequence Number See 'Section 6.4. Data Integrity' of rfc4253 for more info.
Object
public
$hmac_check
=
false
Server to Client HMAC Object
Object
public
$hmac_create
=
false
Client to Server HMAC Object
Integer
public
$hmac_size
=
false
Size of server to client HMAC We need to know how big the HMAC will be for the server to client direction so that we know how many bytes to read.
For the client to server side, the HMAC object will make the HMAC as long as it needs to be. All we need to do is
append it.
String
public
$identifier
=
'SSH-2.0-phpseclib_0.2'
The SSH identifier
Array
public
$interactiveBuffer
=
''
Interactive Buffer
Array
public
$kex_algorithms
=
Key Exchange Algorithms
Array
public
$languages_client_to_server
=
Languages: Client to Server
Array
public
$languages_server_to_client
=
Languages: Server to Client
Array
public
$mac_algorithms_client_to_server
=
MAC Algorithms: Client to Server
Array
public
$mac_algorithms_server_to_client
=
MAC Algorithms: Server to Client
Array
public
$message_log
=
array()
Message Log
Array
public
$message_number_log
=
array()
Message Number Log
Array
public
$message_numbers
=
array()
Message Numbers
Array
public
$packet_size_client_to_server
=
array()
Packet Size Maximum packet size indexed by channel
Integer
public
$send_seq_no
=
0
Send Sequence Number See 'Section 6.4. Data Integrity' of rfc4253 for more info.
Array
public
$server_channels
=
array()
Server Channels Maps client channels to server channels
Array
public
$server_host_key_algorithms
=
Server Host Key Algorithms
String
public
$server_identifier
=
''
Server Identifier
String
public
$server_public_host_key
=
Server Public Host Key
String
public
$session_id
=
false
Session identifer "The exchange hash H from the first key exchange is additionally
used as the session identifier, which is a unique identifier for
this connection."
-- http://tools.ietf.org/html/rfc4253#section-7.2
String
public
$signature
=
''
Server signature Verified against $this->session_id
String
public
$signature_format
=
''
Server signature format ssh-rsa or ssh-dss.
Array
public
$terminal_modes
=
array()
Terminal Modes
Integer
public
$window_size
=
2147483647
The Window Size Bytes the other party can send before it must wait for the window to be adjusted (0x7FFFFFFF = 4GB)
Array
public
$window_size_client_to_server
=
array()