void clSSH::DoOpenChannel() throw(clException) { if(m_channel) return; m_channel = ssh_channel_new(m_session); if(!m_channel) { throw clException(ssh_get_error(m_session)); } int rc = ssh_channel_open_session(m_channel); if(rc != SSH_OK) { throw clException(ssh_get_error(m_session)); } rc = ssh_channel_request_pty(m_channel); if(rc != SSH_OK) { throw clException(ssh_get_error(m_session)); } rc = ssh_channel_change_pty_size(m_channel, 80, 24); if(rc != SSH_OK) { throw clException(ssh_get_error(m_session)); } rc = ssh_channel_request_shell(m_channel); if(rc != SSH_OK) { throw clException(ssh_get_error(m_session)); } }
void SSH::openShell() { if (_bShellOpen) { return; } if (!_bChanOpen) { openChannel(); } _logger->debug("Opening SSH shell"); int rc; rc = ssh_channel_request_pty(_channel); if (rc != SSH_OK) { throw SSHException("Failed to request PTY from remote host", SSHErrorCode::E_PTY_REQUEST_FAILED); } rc = ssh_channel_change_pty_size(_channel, 80, 24); if (rc != SSH_OK) { throw SSHException("Failed to change PTY size", SSHErrorCode::E_PTY_SIZE_FAILED); } rc = ssh_channel_request_shell(_channel); if (rc != SSH_OK) { throw SSHException("Failed to request remote shell", SSHErrorCode::E_SHELL_REQUEST_FAILED); } _bShellOpen = true; }
static void shell(ssh_session session){ ssh_channel channel; struct termios terminal_local; int interactive=isatty(0); channel = ssh_channel_new(session); if(interactive){ tcgetattr(0,&terminal_local); memcpy(&terminal,&terminal_local,sizeof(struct termios)); } if(ssh_channel_open_session(channel)){ printf("error opening channel : %s\n",ssh_get_error(session)); return; } chan=channel; if(interactive){ ssh_channel_request_pty(channel); sizechanged(); } if(ssh_channel_request_shell(channel)){ printf("Requesting shell : %s\n",ssh_get_error(session)); return; } if(interactive){ cfmakeraw(&terminal_local); tcsetattr(0,TCSANOW,&terminal_local); setsignal(); } signal(SIGTERM,do_cleanup); select_loop(session,channel); if(interactive) do_cleanup(0); }
static ssh_channel run_capture(ssh_session sshs, const char* iface, const char* cfilter, const guint32 count) { char* cmdline = NULL; ssh_channel channel; int ret = 0; channel = ssh_channel_new(sshs); if (!channel) return NULL; if (ssh_channel_open_session(channel) != SSH_OK) goto error; if (ssh_channel_request_pty(channel) != SSH_OK) goto error; if (ssh_channel_change_pty_size(channel, 80, 24) != SSH_OK) goto error; if (ssh_channel_request_shell(channel) != SSH_OK) goto error; if (!check_ios_version(channel)) goto error; if (ssh_channel_printf(channel, "terminal length 0\n") == EXIT_FAILURE) goto error; if (ssh_channel_printf(channel, "monitor capture buffer %s max-size 9500\n", WIRESHARK_CAPTURE_BUFFER) == EXIT_FAILURE) goto error; if (ssh_channel_printf(channel, "monitor capture buffer %s limit packet-count %u\n", WIRESHARK_CAPTURE_BUFFER, count) == EXIT_FAILURE) goto error; if (cfilter) { gchar* multiline_filter; gchar* chr; if (ssh_channel_printf(channel, "configure terminal\n") == EXIT_FAILURE) goto error; if (ssh_channel_printf(channel, "ip access-list ex %s\n", WIRESHARK_CAPTURE_ACCESSLIST) == EXIT_FAILURE) goto error; multiline_filter = g_strdup(cfilter); chr = multiline_filter; while((chr = g_strstr_len(chr, strlen(chr), ",")) != NULL) { chr[0] = '\n'; g_debug("Splitting filter into multiline"); } ret = ssh_channel_write(channel, multiline_filter, (uint32_t)strlen(multiline_filter)); g_free(multiline_filter); if (ret == SSH_ERROR) goto error; if (ssh_channel_printf(channel, "\nend\n") == EXIT_FAILURE) goto error; if (ssh_channel_printf(channel, "monitor capture buffer %s filter access-list %s\n", WIRESHARK_CAPTURE_BUFFER, WIRESHARK_CAPTURE_ACCESSLIST) == EXIT_FAILURE) goto error; } if (ssh_channel_printf(channel, "monitor capture point ip cef %s %s both\n", WIRESHARK_CAPTURE_POINT, iface) == EXIT_FAILURE) goto error; if (ssh_channel_printf(channel, "monitor capture point associate %s %s \n", WIRESHARK_CAPTURE_POINT, WIRESHARK_CAPTURE_BUFFER) == EXIT_FAILURE) goto error; if (ssh_channel_printf(channel, "monitor capture point start %s\n", WIRESHARK_CAPTURE_POINT) == EXIT_FAILURE) goto error; if (read_output_bytes(channel, -1, NULL) == EXIT_FAILURE) goto error; if (wait_until_data(channel, count) == EXIT_FAILURE) goto error; if (read_output_bytes(channel, -1, NULL) == EXIT_FAILURE) goto error; cmdline = g_strdup_printf("show monitor capture buffer %s dump\n", WIRESHARK_CAPTURE_BUFFER); if (ssh_channel_printf(channel, cmdline) == EXIT_FAILURE) goto error; if (read_output_bytes(channel, (int)strlen(cmdline), NULL) == EXIT_FAILURE) goto error; g_free(cmdline); return channel; error: g_free(cmdline); g_warning("Error running ssh remote command"); read_output_bytes(channel, -1, NULL); ssh_channel_close(channel); ssh_channel_free(channel); return NULL; }
bool SSH_Socket::onEnter() { // For testing and getting debugging output //int verb = SSH_LOG_PROTOCOL; int rc; // Setup new SSH Shell session = ssh_new(); if(session == nullptr) { SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Closed Session", "User has closed the program.", nullptr); TheSocketHandler::Instance()->setActive(false); return false; } std::cout << "Connecting to: " << host << ":" << port << std::endl; std::cout << "User: "******"Starting up SSH Connection, this can take few moments" << std::endl; // SSH Connect rc = ssh_connect(session); if(rc != SSH_OK) { std::cout << "Error: ssh_connect: " << host << " " << ssh_get_error(session) << std::endl; SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Closed Session", "Unable to Connect to Server!", nullptr); TheSocketHandler::Instance()->setActive(false); return false; } // Verify Server is a known host. rc = verify_knownhost(); if(rc < 0) { std::cout << "Error: verify_knownhost: " << host << " " << ssh_get_error(session) << " " << rc << std::endl; SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Closed Session", "User has closed the program.", nullptr); TheSocketHandler::Instance()->setActive(false); return false; } //if ((ssh_userauth_password(session, NULL, password) == SSH_AUTH_SUCCESS) rc = authenticate_console(); if(rc != SSH_AUTH_SUCCESS) { std::cout << "Error: authenticate: " << host << " " << ssh_get_error(session) << " " << rc << std::endl; TheSocketHandler::Instance()->setActive(false); return false; } // Setup Channel for Socket Communications sshChannel = ssh_channel_new(session); if(sshChannel == nullptr) { std::cout << "Error: ssh_channel_new: " << host << " " << ssh_get_error(session) << " " << rc << std::endl; TheSocketHandler::Instance()->setActive(false); return false; } // Open A shell Session rc = ssh_channel_open_session(sshChannel); if(rc != SSH_OK) { std::cout << "Error: ssh_channel_open_session: " << host << " " << ssh_get_error(session) << " " << rc << std::endl; TheSocketHandler::Instance()->setActive(false); return false; } /* Don't need this when we use the PTY size, it does both! // Setup sockets or "channels" within the session if(ssh_channel_request_pty(sshChannel)) { printf("\r\n SSH Error, Request for PTY Failed. %s: %s - %i \r\n", host, ssh_get_error(session), rc); SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, "Closed Session", "User has closed the program.", NULL); onExit(); }*/ // Set the term and pty size of the terminal. // Only After the Shell has been initialized. if(ssh_channel_request_pty_size(sshChannel, "ansi", 80, 25)) { std::cout << "Error: ssh_channel_request_pty_size: " << host << " " << ssh_get_error(session) << " " << rc << std::endl; // Not an error to exit the connection on. //return 0; } // Now request a shell with the pty to get read/write if(ssh_channel_request_shell(sshChannel)) { std::cout << "Error: ssh_channel_request_shell: " << host << " " << ssh_get_error(session) << " " << rc << std::endl; TheSocketHandler::Instance()->setActive(false); return false; } return true; }
int channel_request_shell(ssh_channel channel){ return ssh_channel_request_shell(channel); }