添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

libssh2 API reference


Table of Contents

1. Session operations
libssh2_session_init_ex - initialize a libssh2 session handle
libssh2_banner_set - set the SSH prococol banner for the local client
libssh2_session_method_pref - set preferred key exchange method
libssh2_session_methods - return the currently active algorithms
libssh2_session_callback_set - set a callback function
libssh2_session_startup - start ssh2 on a socket
libssh2_session_disconnect_ex - disconnect a session
libssh2_session_abstract - return a pointer to a session's abstract pointer
libssh2_session_free - close all channels in a session, and free all associated memory
libssh2_session_last_error - return the last error code
2. Authentication
libssh2_hostkey_hash - return a hash of the remote host's key
libssh2_userauth_password_ex - authenticate a session with username and password
libssh2_userauth_publickey_fromfile - authenticate a session with a public key, read from a file
libssh2_userauth_list - list the authentication methods supported by a server
libssh2_userauth_authenticated - return authentication status
3. Channel operations
libssh2_channel_open_ex - establish a generic session channel
libssh2_channel_setenv_ex - set an environment variable on the channel
libssh2_channel_request_pty_ex - request a terminal on a channel
libssh2_channel_process_startup - request a shell on a channel
libssh2_channel_read_ex - read from a channel
libssh2_channel_write_ex - write to a channel
libssh2_channel_handle_extended_data - set extended data handling mode
libssh2_channel_flush_ex - flush a channel
libssh2_channel_set_blocking - enable or disable blocking on a channel
libssh2_channel_eof - check a channel's EOF status
libssh2_channel_send_eof - send EOF to remote server
libssh2_channel_close - close a channel
libssh2_channel_free - free all resources associated with a channel
libssh2_channel_direct_tcpip_ex - Tunnel a TCP connection through an SSH session
libssh2_channel_forward_listen_ex - set up a forwarded TCP port
libssh2_channel_forward_cancel - cancel a forwarded TCP port
libssh2_channel_forward_accept - accept a connection on a TCP port
4. SCP
libssh2_scp_recv - request a remote file via SCP
libssh2_scp_send_ex - Send a file via SCP
5. SFTP
libssh2_sftp_init - start an SFTP session
libssh2_sftp_shutdown - shut down an SFTP session
libssh2_sftp_open_ex - open and possibly create a file on a remote host
libssh2_sftp_read - read from an SFTP file handle
libssh2_sftp_readdir - read an entry from an SFTP directory
libssh2_sftp_write - write data to an SFTP file handle
libssh2_sftp_fstat_ex - get or set attributes on a file handle
libssh2_sftp_seek - set the read/write position indicator to a position within a file
libssh2_sftp_tell - get the current read/write position indicator for a file
libssh2_sftp_close_handle - close filehandle
libssh2_sftp_unlink_ex - delete a file
libssh2_sftp_rename_ex - rename a file
libssh2_sftp_mkdir_ex - create a directory
libssh2_sftp_rmdir_ex - remove a directory
libssh2_sftp_stat_ex - get or set attributes on a file or symbolic link
libssh2_sftp_symlink_ex - read or set a symbolic link
libssh2_sftp_realpath - resolve a filename's path
libssh2_sftp_last_error - return the last SFTP-specific error code
6. Symbols
7. About this document

Table of Contents

libssh2_session_init_ex - initialize a libssh2 session handle
libssh2_banner_set - set the SSH prococol banner for the local client
libssh2_session_method_pref - set preferred key exchange method
libssh2_session_methods - return the currently active algorithms
libssh2_session_callback_set - set a callback function
libssh2_session_startup - start ssh2 on a socket
libssh2_session_disconnect_ex - disconnect a session
libssh2_session_abstract - return a pointer to a session's abstract pointer
libssh2_session_free - close all channels in a session, and free all associated memory
libssh2_session_last_error - return the last error code

Name

libssh2_session_init_ex — initialize a libssh2 session handle

Synopsis

#include <libssh2.h> LIBSSH2_SESSION * libssh2_session_init_ex( LIBSSH2_ALLOC_FUNC *allocfunc, LIBSSH2_FREE_FUNC *freefunc, LIBSSH2_REALLOC_FUNC *reallocfunc void *data); LIBSSH2_SESSION * libssh2_session_init();

Description

The libssh2_session_init_ex() function initializes an SSH2 session and returns a session handle of type LIBSSH2_SESSION . If provided, libssh2 will use the allocfunc , freefunc and reallocfunc parameters for all internal memory management calls. In this case, the data parameter may optionally be passed to the provided callbacks. As a convenience, the libssh2_session_init() macro is provided, which uses the system's memory management.



Name

libssh2_session_method_pref — set preferred key exchange method

Description

This function sets preferred methods for key exchange and related algorithms for session . method_type denotes the type of preference to set, and prefs contains a comma-separated list of methods relevant to that preference. method_type may be one of the following:

LIBSSH2_METHOD_KEX

Key exchange method names. Supported values:

LIBSSH2_METHOD_HOSTKEY

Public key algorithms. Supported values:

LIBSSH2_METHOD_CRYPT_CS

Encryption algorithm from client to server. Supported algorithms:

LIBSSH2_METHOD_CRYPT_SC

Encryption algorithm from server to client. See LIBSSH2_METHOD_CRYPT_CS for supported algorithms.

LIBSSH2_METHOD_MAC_CS

Message Authentication Code (MAC) algorithms from client to server. Supported values:

LIBSSH2_METHOD_MAC_SC

Message Authentication Code (MAC) algorithms from server to client. See LIBSSH2_METHOD_MAC_SC for supported algorithms.

LIBSSH2_METHOD_COMP_CS

Compression methods from client to server. Supported values:

LIBSSH2_METHOD_COMP_SC

Compression methods from server to client. See LIBSSH2_METHOD_COMP_CS for supported compression methods.

If successful, 0 is returned. In case of failure, -1 is returned and libssh2_session_last_error() must be used to find out the error that occurred.







Table of Contents

libssh2_hostkey_hash - return a hash of the remote host's key
libssh2_userauth_password_ex - authenticate a session with username and password
libssh2_userauth_publickey_fromfile - authenticate a session with a public key, read from a file
libssh2_userauth_list - list the authentication methods supported by a server
libssh2_userauth_authenticated - return authentication status

Name

libssh2_hostkey_hash — return a hash of the remote host's key

Description

This function returns a hash of the remote host's key . A session with the remote host should be established beforehand, by calling libssh2_session_startup() . The hashtype can be determined by passing one of the following:

If an error occurs, NULL will be returned. The returned string is not NULL-terminated. The length of the returned string is either 16 bytes in case of MD5, or 20 bytes when SHA1 is used. The returned string is static storage, and must not be freed.




Table of Contents

libssh2_channel_open_ex - establish a generic session channel
libssh2_channel_setenv_ex - set an environment variable on the channel
libssh2_channel_request_pty_ex - request a terminal on a channel
libssh2_channel_process_startup - request a shell on a channel
libssh2_channel_read_ex - read from a channel
libssh2_channel_write_ex - write to a channel
libssh2_channel_handle_extended_data - set extended data handling mode
libssh2_channel_flush_ex - flush a channel
libssh2_channel_set_blocking - enable or disable blocking on a channel
libssh2_channel_eof - check a channel's EOF status
libssh2_channel_send_eof - send EOF to remote server
libssh2_channel_close - close a channel
libssh2_channel_free - free all resources associated with a channel
libssh2_channel_direct_tcpip_ex - Tunnel a TCP connection through an SSH session
libssh2_channel_forward_listen_ex - set up a forwarded TCP port
libssh2_channel_forward_cancel - cancel a forwarded TCP port
libssh2_channel_forward_accept - accept a connection on a TCP port


Name

libssh2_channel_request_pty_ex — request a terminal on a channel

Synopsis

#include <libssh2.h> int libssh2_channel_request_pty_ex( LIBSSH2_CHANNEL , char *terminaltype, int terminaltype_len char *modes, int modes_len, int width, int height, int width_px, int height_px); int libssh2_channel_request_pty( LIBSSH2_CHANNEL , char *terminaltype);



Name

libssh2_channel_read_ex — read from a channel

Synopsis

#include <libssh2.h> int libssh2_channel_read_ex( LIBSSH2_CHANNEL *channel, int stream_id, char *buf, size_t buflen); int libssh2_channel_read( LIBSSH2_CHANNEL *channel, char *buf, size_t buflen); int libssh2_channel_read_stderr( LIBSSH2_CHANNEL *channel, char *buf, size_t buflen);

Description

libssh2_channel_read_ex() reads up to buflen bytes from channel , and stores these in buf . The stream_id can be either 0 to read from the channel's standard input, or SSH_EXTENDED_DATA_STDERR , for the channel's standard error. The number of bytes read is returned. As a convenience, the macros libssh2_channel_read() and libssh2_channel_read_stderr() are provided. These call libssh2_channel_read_ex() with stream_id set to the channel's standard output and standard error, respectively.


Name

libssh2_channel_write_ex — write to a channel

Synopsis

#include <libssh2.h> int libssh2_channel_write_ex( LIBSSH2_CHANNEL *channel, int stream_id, char *buf, size_t buflen); int libssh2_channel_write( LIBSSH2_CHANNEL *channel, char *buf, size_t buflen); int libssh2_channel_write_stderr( LIBSSH2_CHANNEL *channel, char *buf, size_t buflen);

Description

libssh2_channel_write_ex() writes buflen bytes from buf to a channel . The stream_id can be either 0 to write to the channel's standard output, or SSH_EXTENDED_DATA_STDERR , for the channel's standard error. As a convenience, the macros libssh2_channel_write() and libssh2_channel_write_stderr() are provided. These call libssh2_channel_write_ex() with stream_id set to the channel's standard output and standard error, respectively. Returns the number of bytes written if successful, or -1 in case of error. In this case, the errorcode can be retrieved with libssh2_session_last_error() .



Name

libssh2_channel_flush_ex — flush a channel

Synopsis

#include <libssh2.h> int libssh2_channel_flush_ex( LIBSSH2_CHANNEL *channel, int streamid); int libssh2_channel_flush( LIBSSH2_CHANNEL *channel); int libssh2_channel_flush_stderr( LIBSSH2_CHANNEL *channel);

Description

This function flushes all queued data on channel . stream_id may be either 0 for the channel's stdout, or SSH_EXTENDED_DATA_STDERR for stderr. As a convenience, the libssh2_channel_flush() and libssh2_channel_flush_stderr() macros are provided, which set stream_id to the respective values. On success, the total number of bytes flushed is returned. In case of error, -1 is returned, and the error can be retrieved with libssh2_session_last_error() .









Table of Contents

libssh2_scp_recv - request a remote file via SCP
libssh2_scp_send_ex - Send a file via SCP

Table of Contents

libssh2_sftp_init - start an SFTP session
libssh2_sftp_shutdown - shut down an SFTP session
libssh2_sftp_open_ex - open and possibly create a file on a remote host
libssh2_sftp_read - read from an SFTP file handle
libssh2_sftp_readdir - read an entry from an SFTP directory
libssh2_sftp_write - write data to an SFTP file handle
libssh2_sftp_fstat_ex - get or set attributes on a file handle
libssh2_sftp_seek - set the read/write position indicator to a position within a file
libssh2_sftp_tell - get the current read/write position indicator for a file
libssh2_sftp_close_handle - close filehandle
libssh2_sftp_unlink_ex - delete a file
libssh2_sftp_rename_ex - rename a file
libssh2_sftp_mkdir_ex - create a directory
libssh2_sftp_rmdir_ex - remove a directory
libssh2_sftp_stat_ex - get or set attributes on a file or symbolic link
libssh2_sftp_symlink_ex - read or set a symbolic link
libssh2_sftp_realpath - resolve a filename's path
libssh2_sftp_last_error - return the last SFTP-specific error code


Name

libssh2_sftp_open_ex — open and possibly create a file on a remote host

Synopsis

#include <libssh2.h> #include <libssh2_sftp.h> LIBSSH2_SFTP_HANDLE *libssh2_sftp_open_ex( LIBSSH2_SFTP *sftp, char *filename, int filename_len, unsigned long flags, long mode, int open_type); LIBSSH2_SFTP_HANDLE *libssh2_sftp_open( LIBSSH2_SFTP *sftp, char *filename, unsigned long flags, long mode); LIBSSH2_SFTP_HANDLE *libssh2_sftp_opendir( LIBSSH2_SFTP *sftp, char *path);










Name

libssh2_sftp_rename_ex — rename a file

Synopsis

#include <libssh2.h> #include <libssh2_sftp.h> int libssh2_sftp_rename_ex( LIBSSH2_SFTP *sftp, char *source_filename, int source_filename_len, char *dest_filename, int dest_filename_len, long flags); int libssh2_sftp_rename( LIBSSH2_SFTP , char *source_filename, char *dest_filename);




Name

libssh2_sftp_stat_ex — get or set attributes on a file or symbolic link

Synopsis

#include <libssh2.h> #include <libssh2_sftp.h> int libssh2_sftp_stat_ex( LIBSSH2_SFTP *sftp, char *path, int path_len, int stat_type, LIBSSH2_SFTP_ATTRIBUTES *attrs); int libssh2_sftp_stat( LIBSSH2_SFTP *sftp, char *path, LIBSSH2_SFTP_ATTRIBUTES *attrs); int libssh2_sftp_lstat( LIBSSH2_SFTP *sftp, char *path, LIBSSH2_SFTP_ATTRIBUTES *attrs); int libssh2_sftp_setstat( LIBSSH2_SFTP *sftp, char *path, LIBSSH2_SFTP_ATTRIBUTES *attrs);


Name

libssh2_sftp_symlink_ex — read or set a symbolic link

Synopsis

#include <libssh2.h> #include <libssh2_sftp.h> int libssh2_sftp_symlink_ex( LIBSSH2_SFTP *sftp, const char *path, int path_len, char *target, int target_len, int link_type); int libssh2_sftp_symlink( LIBSSH2_SFTP *sftp, char *path, char *target); int libssh2_sftp_readlink( LIBSSH2_SFTP *sftp, char *path, char *target, int max_target_len);


Data types.

LIBSSH2_SESSION

libssh2 session handle

LIBSSH2_CHANNEL

libssh2 channel handle

LIBSSH2_LISTENER

libssh2 port forwarding handle

LIBSSH2_SFTP

libssh2 SFTP handle

LIBSSH2_SFTP_HANDLE

libssh2 SFTP file handle

LIBSSH2_SFTP_ATTRIBUTES

libssh2 SFTP file attribute structure

unsigned long flags; unsigned long filesize; unsigned long uid, gid; unsigned long permissions; unsigned long atime, mtime;

If the flags parameter is anded with one of the following symbols, the respective field in the LIBSSH2_SFTP_ATTRIBUTES structure is meaningful.

Constants.

SSH protocol disconnect codes.

Function prototypes.

Error codes.

LIBSSH2_ERROR_SOCKET_NONE

The socket is invalid.

LIBSSH2_ERROR_BANNER_NONE

No banner was received from the remote host.

LIBSSH2_ERROR_BANNER_SEND

Unable to send banner to remote host.

LIBSSH2_ERROR_INVALID_MAC

LIBSSH2_ERROR_KEX_FAILURE

Encryption key exchange with the remote host failed.

LIBSSH2_ERROR_ALLOC

An internal memory allocation call failed.

LIBSSH2_ERROR_SOCKET_SEND

Unable to send data on socket.

LIBSSH2_ERROR_KEY_EXCHANGE_FAILURE

LIBSSH2_ERROR_TIMEOUT

LIBSSH2_ERROR_HOSTKEY_INIT

LIBSSH2_ERROR_HOSTKEY_SIGN

LIBSSH2_ERROR_DECRYPT

LIBSSH2_ERROR_SOCKET_DISCONNECT

The socket was disconnected.

LIBSSH2_ERROR_PROTO

An invalid SSH protocol response was received on the socket.

LIBSSH2_ERROR_PASSWORD_EXPIRED

LIBSSH2_ERROR_FILE

LIBSSH2_ERROR_METHOD_NONE

LIBSSH2_ERROR_PUBLICKEY_UNRECOGNIZED

The username/public key combination was invalid.

LIBSSH2_ERROR_PUBLICKEY_UNVERIFIED

The username/public key combination was invalid, or the signature for the supplied public key was invalid.

LIBSSH2_ERROR_CHANNEL_OUTOFORDER

LIBSSH2_ERROR_CHANNEL_FAILURE

LIBSSH2_ERROR_CHANNEL_REQUEST_DENIED

LIBSSH2_ERROR_CHANNEL_UNKNOWN

LIBSSH2_ERROR_CHANNEL_WINDOW_EXCEEDED

LIBSSH2_ERROR_CHANNEL_PACKET_EXCEEDED

LIBSSH2_ERROR_CHANNEL_CLOSED

LIBSSH2_ERROR_CHANNEL_EOF_SENT

LIBSSH2_ERROR_SCP_PROTOCOL

LIBSSH2_ERROR_ZLIB

LIBSSH2_ERROR_SOCKET_TIMEOUT

LIBSSH2_ERROR_SFTP_PROTOCOL

An invalid SFTP protocol response was received on the socket, or an SFTP operation caused an errorcode to be returned by the server. In the last case, libssh2_sftp_last_error() will return one of the following error codes:

LIBSSH2_ERROR_REQUEST_DENIED

The remote server refused the request.

LIBSSH2_ERROR_METHOD_NOT_SUPPORTED

The requested method is not supported.

LIBSSH2_ERROR_INVAL

The requested method type was invalid.