ssh.h /* ssh_t Using libssh */ #ifndef __LIBSSH_CLASS_TYPE_H__ #define __LIBSSH_CLASS_TYPE_H_ #include <libssh/libssh.h> #include <libssh/ssh2.h> #include <libssh/sftp.h> #include <libssh/callbacks.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/time.h> #include <dirent.h> #include <unistd.h> #include <stdlib.h> #include <stdint.h> #include <fcntl.h> #include <errno.h> #include <pthread.h> #include <string> #include <fstream> class ssh_t { public: ssh_t(); virtual ~ssh_t(); /** ssh_options_set */ int set_host(const std::string& host){return ssh_options_set(m_session, SSH_OPTIONS_HOST, host.c_str());} int set_port(int port){return ssh_options_set(m_session, SSH_OPTIONS_PORT, &port);} int set_user(const std::string& user){return ssh_options_set(m_session, SSH_OPTIONS_USER, user.c_str());} int set_path(const std::string& path){return ssh_options_set(m_session, SSH_OPTIONS_SSH_DIR, path.c_str());} int set_timeout(int t){return ssh_options_set(m_session, SSH_OPTIONS_TIMEOUT, &t);} int set_ssh2(int on){return ssh_options_set(m_session, SSH_OPTIONS_SSH2, &on);} int set_ssh1(int on){return ssh_options_set(m_session, SSH_OPTIONS_SSH1, &on);} /** session */ int connect(const std::string& host, int port, const std::string& user, const std::string& password) {set_host(host); set_user(user); set_port(port); if(ssh_connect(m_session))return -1; return authenticate(password);} void disconnect() {if(m_session){::ssh_disconnect(m_session);}} int get_status()const {return ssh_get_status(m_session);} const char* get_error()const {return ssh_get_error(m_session);} int get_error_code()const {return ssh_get_error_code(m_session);} /** new functions */ int exec(const std::string& cmdline, int fd_stdout=-1, int fd_stderr=-1); int exec(const std::string& cmdline, std::ostream* strm_stdout=NULL, std::ostream* strm_stderr=NULL); int upload(const std::string& local, const std::string& remote_path, std::string& remote_name); int download(const std::string& remote, const std::string& local_path, std::string& local_name); /** scp2 not supposed! */ int scp_upload(const std::string& local, const std::string& remote_path, const std::string& remote_name=""); private: int authenticate(const std::string& password); int authenticate_kbdint(const std::string& password); /** channel */ int duplicate_from_channel(ssh_channel channel, int fd, int is_stderr); int duplicate_from_channel(ssh_channel channel, std::ostream& strm, int is_stderr); /** sftp */ int upload_file(sftp_session sftp, const std::string& local, struct stat& st, const std::string& remote); int upload_symlink(sftp_session sftp, const std::string& local, struct stat& st, const std::string& remote); int upload_dir(sftp_session sftp, const std::string& local, struct stat& st, const std::string& remote); int download_file(sftp_session sftp, const std::string& remote, sftp_attributes attrs, const std::string& local); int download_symlink(sftp_session sftp, const std::string& remote, sftp_attributes attrs, const std::string& local); int download_dir(sftp_session sftp, const std::string& remote, sftp_attributes attrs, const std::string& local); /** scp */ private: ssh_session m_session; sftp_session m_sftp_session; char* m_buf; const static uint32_t m_buflen = 1024*16; }; #endif //__LIBSSH_CLASS_TYPE_H_ ssh.cpp #include "ssh.h" static pthread_once_t once_ssh_init = PTHREAD_ONCE_INIT; void do_ssh_init(){ssh_init();} ssh_t::ssh_t():m_session(ssh_new()), m_buf(new char[m_buflen]) { pthread_once(&once_ssh_init, do_ssh_init); /** default options */ set_port(36000); set_ssh2(1); set_ssh1(1); set_timeout(30);