From 93f487e4ed1f8b0eeb84a2c370220403f8c1d385 Mon Sep 17 00:00:00 2001 From: suzhe Date: Tue, 5 Dec 2023 19:47:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20'test'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/test b/test index 9daeafb..d8178a7 100644 --- a/test +++ b/test @@ -1 +1,94 @@ test +#include +#include + +int main() { + // 定义目标服务器的信息 + const char *hostname = "your_server_hostname"; + const char *username = "your_username"; + const char *password = "your_password"; + + // 初始化SSH会话 + ssh_session session = ssh_new(); + if (session == NULL) { + fprintf(stderr, "Error creating SSH session\n"); + return 1; + } + + // 设置SSH连接参数 + ssh_options_set(session, SSH_OPTIONS_HOST, hostname); + ssh_options_set(session, SSH_OPTIONS_USER, username); + + // 连接到SSH服务器 + int rc = ssh_connect(session); + if (rc != SSH_OK) { + fprintf(stderr, "Error connecting to SSH server: %s\n", ssh_get_error(session)); + ssh_free(session); + return 1; + } + + // 使用密码进行身份验证 + rc = ssh_userauth_password(session, NULL, password); + if (rc != SSH_AUTH_SUCCESS) { + fprintf(stderr, "Error authenticating with password: %s\n", ssh_get_error(session)); + ssh_disconnect(session); + ssh_free(session); + return 1; + } + + // 打开通道 + ssh_channel channel = ssh_channel_new(session); + if (channel == NULL) { + fprintf(stderr, "Error creating channel: %s\n", ssh_get_error(session)); + ssh_disconnect(session); + ssh_free(session); + return 1; + } + + // 打开会话 + rc = ssh_channel_open_session(channel); + if (rc != SSH_OK) { + fprintf(stderr, "Error opening channel session: %s\n", ssh_get_error(session)); + ssh_channel_free(channel); + ssh_disconnect(session); + ssh_free(session); + return 1; + } + + // 执行交互性命令 + rc = ssh_channel_request_pty(channel); + if (rc != SSH_OK) { + fprintf(stderr, "Error requesting pty: %s\n", ssh_get_error(session)); + ssh_channel_free(channel); + ssh_disconnect(session); + ssh_free(session); + return 1; + } + + // 打开 shell + rc = ssh_channel_shell(channel); + if (rc != SSH_OK) { + fprintf(stderr, "Error opening shell: %s\n", ssh_get_error(session)); + ssh_channel_free(channel); + ssh_disconnect(session); + ssh_free(session); + return 1; + } + + // 从键盘获取输入并发送到 SSH 服务器 + char buffer[256]; + while (fgets(buffer, sizeof(buffer), stdin) != NULL) { + ssh_channel_write(channel, buffer, strlen(buffer)); + } + + // 关闭 shell 通道 + ssh_channel_send_eof(channel); + ssh_channel_close(channel); + + // 释放资源 + ssh_channel_free(channel); + ssh_disconnect(session); + ssh_free(session); + + return 0; +} \ No newline at end of file