void getClient(char * serverAddress) { int sockFd = makeSocketOrDie(); struct addrinfo * serverInfo = getServerInfo(serverAddress); printf("Connecting to server\n"); connectOrDie(sockFd, serverInfo); printf("Connected to server. Making LIBSSH2 session\n"); LIBSSH2_SESSION * session = makeSession(); libssh2_session_set_blocking(session, 1); libssh2_session_set_timeout(session, 5000); printf("Made session, handshaking\n"); int result = libssh2_session_handshake(session, sockFd); //const char * fingerprint = libssh2_hostkey_hash(session, LIBSSH_HOSTKEY_HASH_SHA1); //TODO: Match the fingerprint against something. if (result) { char * errorMessage; libssh2_session_last_error(session, &errorMessage, NULL, 0); fprintf(stderr, "Error %s handshaking\n", errorMessage); exit(EXIT_FAILURE); } printf("Handshake completed, making SFTP Session\n"); libssh2_userauth_password(session, NETID, PWD); LIBSSH2_SFTP * sftpSession = makeSFTPSession(session); printf("Started SFTP - Downloading file\n"); LIBSSH2_SFTP_HANDLE * fileHandle = libssh2_sftp_open(sftpSession, serverFilePath, LIBSSH2_FXF_READ, 0); readFile(session, sftpSession, fileHandle); libssh2_sftp_shutdown(sftpSession); libssh2_session_disconnect(session, "Done.\n"); libssh2_session_free(session); freeaddrinfo(serverInfo); close(sockFd); }
bool RunCommand(const char *aCommand) { if (!mSessionOK) { return false; } LIBSSH2_CHANNEL *channel = libssh2_channel_open_session(mSession); if (channel == NULL) { strcpy(mErrorMessage, "Could not open SSH2 channel"); return false; } #ifdef libssh2_session_set_timeout libssh2_session_set_timeout(mTimeout); #endif if (libssh2_channel_exec(channel, aCommand) == -1) { strcpy(mErrorMessage, "Could not execute SSH2 command"); return false; } std::string response; char buf[1025]; ssize_t size = 1024; while (size == 1024) { size = libssh2_channel_read(channel, buf, 1024); buf[size] = '\0'; response += buf; } if (mResponse) { delete [] mResponse; } mResponse = new char[response.size()+1]; strcpy(mResponse, response.c_str()); libssh2_channel_close(channel); mExitCode = libssh2_channel_get_exit_status(channel); libssh2_channel_free(channel); return true; }
static size_t php_ssh2_channel_stream_read(php_stream *stream, char *buf, size_t count TSRMLS_DC) { php_ssh2_channel_data *abstract = (php_ssh2_channel_data*)stream->abstract; ssize_t readstate; LIBSSH2_SESSION *session; stream->eof = libssh2_channel_eof(abstract->channel); libssh2_channel_set_blocking(abstract->channel, abstract->is_blocking); session = (LIBSSH2_SESSION *)zend_fetch_resource(NULL TSRMLS_CC, abstract->session_rsrc, PHP_SSH2_SESSION_RES_NAME, NULL, 1, le_ssh2_session); #ifdef PHP_SSH2_SESSION_TIMEOUT if (abstract->is_blocking) { libssh2_session_set_timeout(session, abstract->timeout); } #endif readstate = libssh2_channel_read_ex(abstract->channel, abstract->streamid, buf, count); #ifdef PHP_SSH2_SESSION_TIMEOUT if (abstract->is_blocking) { libssh2_session_set_timeout(session, 0); } #endif if (readstate == LIBSSH2_ERROR_EAGAIN) { readstate = 0; } if (readstate < 0) { char *error_msg = NULL; if (libssh2_session_last_error(session, &error_msg, NULL, 0) == readstate) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failure '%s' (%ld)", error_msg, readstate); } stream->eof = 1; readstate = 0; } return readstate; }
void CLibssh2::create_session(bool nonblocking) { int socket_fd = socket(AF_INET, SOCK_STREAM, 0); if (-1 == socket_fd) { THROW_SYSCALL_EXCEPTION(strerror(errno), errno, "socket"); } try { struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(_port); addr.sin_addr.s_addr = inet_addr(_ip.c_str()); if (-1 == connect(socket_fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in))) { THROW_SYSCALL_EXCEPTION(strerror(errno), errno, "connect"); } // 创建ssh2会话 LIBSSH2_SESSION* session = libssh2_session_init(); if (nonblocking) // 设置为非阻塞 libssh2_session_set_blocking(session, 0); else if (_timeout_seconds > 0) libssh2_session_set_timeout(session, _timeout_seconds*1000); _session = session; _socket_fd = socket_fd; } catch (...) { close(socket_fd); throw; } }
static int ssh2_drive_session(eventer_t e, int mask, void *closure, struct timeval *now) { int i; const char *fingerprint; ssh2_check_info_t *ci = closure; struct timeval diff; int timeout_ms = 10; /* 10ms, gets set below */ if(ci->state == WANT_CLOSE) { noit_check_t *check = ci->check; ssh2_log_results(ci->self, ci->check); ssh2_cleanup(ci->self, ci->check); eventer_remove_fd(e->fd); e->opset->close(e->fd, &mask, e); check->flags &= ~NP_RUNNING; return 0; } switch(mask) { case EVENTER_ASYNCH_WORK: if(eventer_set_fd_blocking(e->fd)) { ci->timed_out = 0; ci->error = strdup("socket error"); return 0; } ci->session = libssh2_session_init(); #define set_method(a,b) do { \ int rv; \ if(ci->methods.a && \ (rv = libssh2_session_method_pref(ci->session, b, ci->methods.a)) != 0) { \ ci->timed_out = 0; \ ci->error = strdup((rv == LIBSSH2_ERROR_METHOD_NOT_SUPPORTED) ? \ #a " method not supported" : "error setting " #a); \ return 0; \ } \ } while(0) set_method(kex, LIBSSH2_METHOD_KEX); set_method(hostkey, LIBSSH2_METHOD_HOSTKEY); set_method(crypt_cs, LIBSSH2_METHOD_CRYPT_CS); set_method(crypt_sc, LIBSSH2_METHOD_CRYPT_SC); set_method(mac_cs, LIBSSH2_METHOD_MAC_CS); set_method(mac_sc, LIBSSH2_METHOD_MAC_SC); set_method(comp_cs, LIBSSH2_METHOD_COMP_CS); set_method(comp_sc, LIBSSH2_METHOD_COMP_SC); if(compare_timeval(*now, e->whence) < 0) { sub_timeval(e->whence, *now, &diff); timeout_ms = diff.tv_sec * 1000 + diff.tv_usec / 1000; } #if LIBSSH2_VERSION_NUM >= 0x010209 libssh2_session_set_timeout(ci->session, timeout_ms); #endif if (libssh2_session_startup(ci->session, e->fd)) { ci->timed_out = 0; ci->error = strdup("ssh session startup failed"); return 0; } fingerprint = libssh2_hostkey_hash(ci->session, LIBSSH2_HOSTKEY_HASH_MD5); for(i=0;i<16;i++) { snprintf(ci->fingerprint + (i*2), 3, "%02x", (unsigned char)fingerprint[i]); } ci->fingerprint[32] = '\0'; ci->timed_out = 0; return 0; break; case EVENTER_ASYNCH_CLEANUP: if(ci->session) { libssh2_session_disconnect(ci->session, "Bye!"); libssh2_session_free(ci->session); ci->session = NULL; } ci->state = WANT_CLOSE; break; default: abort(); } return 0; }
bool SFtpFileEngine::sftpConnect() { SFtpConnectionCache* cache = SFtpConnectionCache::getInstance(); const SFtpConnectionCache::SFtpConnection *conn = cache->findConnection(_url); if (conn) { _sock = conn->sock; _session = conn->session; _sftp_session = conn->sftp_session; return true; } struct hostent *host; host = gethostbyname(_url.host().toLocal8Bit().constData()); if (!host) return false; _sock = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(_port); sin.sin_addr.s_addr = *reinterpret_cast<u_long *>(host->h_addr); if (::connect(_sock, reinterpret_cast<struct sockaddr *>(&sin), sizeof(struct sockaddr_in)) != 0) { closesocket(_sock); return false; } _session = libssh2_session_init(); if(!_session) { closesocket(_sock); return false; } libssh2_session_set_blocking(_session, 1); if (libssh2_session_handshake(_session, _sock)) { libssh2_session_free(_session); closesocket(_sock); return false; } if (libssh2_userauth_password(_session, _textCodec->fromUnicode(_userName).data(), _textCodec->fromUnicode(_password).data())) { libssh2_session_disconnect(_session, "Abnormal Shutdown"); libssh2_session_free(_session); closesocket(_sock); return false; } _sftp_session = libssh2_sftp_init(_session); if (!_sftp_session) { libssh2_session_disconnect(_session, "Abnormal Shutdown"); libssh2_session_free(_session); closesocket(_sock); return false; } libssh2_session_set_blocking(_session, 1); libssh2_session_set_timeout(_session, 30 * 1000); cache->addConnection(_url, _sock, _session, _sftp_session); return true; }