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; }
/* Procedures */ SCM_DEFINE (guile_ssh_channel_open_session, "channel-open-session", 1, 0, 0, (SCM channel), "\ Open a new session and mark the channel CHANNEL as opened port.\n\ Return value is undefined.\ ") #define FUNC_NAME s_guile_ssh_channel_open_session { struct channel_data *data = _scm_to_channel_data (channel); int res; GSSH_VALIDATE_CHANNEL_DATA (data, channel, FUNC_NAME); res = ssh_channel_open_session (data->ssh_channel); if (res != SSH_OK) { ssh_session session = ssh_channel_get_session (data->ssh_channel); guile_ssh_session_error1 (FUNC_NAME, session, channel); } SCM_SET_CELL_TYPE (channel, SCM_CELL_TYPE (channel) | SCM_OPN); return SCM_UNDEFINED; } #undef FUNC_NAME /* Run a shell command CMD without an interactive shell. */ SCM_DEFINE (guile_ssh_channel_request_exec, "channel-request-exec", 2, 0, 0, (SCM channel, SCM cmd),
int Connector::startVNCSession(string hours, string minutes, string seconds) { std::string command = "/usr/sara/bin/rvs_vnc " + m_hours + ":" + m_minutes + ":" + m_seconds ; // creates a new channel channel = ssh_channel_new(sshSession); if (channel == NULL) { output->append("ERROR: Unable to set up channel between host and client...") ; exit(-1) ; } // links the channel to the current ssh session int response = ssh_channel_open_session(channel); if (response != SSH_OK) { ssh_channel_free(channel); output->append("ERROR: Unable to set up channel between host and client...") ; exit(-1) ; } // Executes the command output->append("A new channel has been opened.") ; response = ssh_channel_request_exec(channel, command.c_str()); if (response != SSH_OK){ output->append("ERROR: Unable to execute the command on the host...") ; exit(-1) ; } // Reading the result of the command and printing it to the status bar char buffer[8192]; unsigned int nbytes; nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); while (nbytes > 0) { if (fwrite(buffer, 1, nbytes, stdout) != nbytes) { ssh_channel_close(channel); ssh_channel_free(channel); output->append("ERROR: buffer to small to read all data") ; break ; } nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0); } if (nbytes < 0) { ssh_channel_close(channel); ssh_channel_free(channel); output->append("ERROR: buffer to small to read all data") ; } /*Check whether the new VNC session is queued */ std::string bufferOutput = buffer ; size_t found = bufferOutput.find("v42-") ; std::string strNode (bufferOutput, found + 4, 2) ; QString strNode2 = QString::fromStdString(strNode) ; QString status = QString("Connection to node %1").arg(strNode2) ; output->append(status) ; int node = atoi(strNode.c_str()); //int node = strNode.toInt() ; // Closes the channel ssh_channel_send_eof(channel); ssh_channel_close(channel); output->append("Loading VNC session...") ; qApp->processEvents() ; return node ; }
static void *nogvl_open_session(void *ptr) { struct nogvl_channel_args *args = ptr; args->rc = ssh_channel_open_session(args->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_open_session(ssh_channel channel){ return ssh_channel_open_session(channel); }
int send_cmd_to_ssh(ssh_channel chanusr, ssh_channel chansvr, char *buff) { int rc; unsigned int nbytes; char buffer[256]; char* ligne; if (chansvr == NULL) { return SSH_ERROR; } if(ssh_channel_is_open(chansvr) != SSH_OK) { rc = ssh_channel_open_session(chansvr); if (rc != SSH_OK) { ssh_channel_free(chansvr); chansvr = NULL; return rc; } } rc = ssh_channel_request_exec(chansvr, buff); //rc = ssh_channel_request_exec(chansvr, "ls"); //ssh_channel_write(chansvr, "ls", 2); if (rc != SSH_OK) { ssh_channel_close(chansvr); ssh_channel_free(chansvr); return rc; } memset(buffer, 0, 256); nbytes = ssh_channel_read(chansvr, buffer, sizeof(buffer) - 1, 0); while (nbytes > 0) { ligne = strtok(buffer,"\n"); while (ligne) { ssh_channel_write(chanusr, ligne, strlen(ligne)); ligne = strtok(NULL,"\n"); if (ligne != NULL) ssh_channel_write(chanusr, "\r\n", 2); } //ssh_channel_write(chanusr, buffer, nbytes); /*if (write(fd, buffer, nbytes) != nbytes) { ssh_channel_close(chansvr); ssh_channel_free(chansvr); return SSH_ERROR; } */ memset(buffer, 0, 256); nbytes = ssh_channel_read(chansvr, buffer, sizeof(buffer) - 1, 0); } return rc; }