当前位置:首页 > 行业动态 > 正文

c写linux远程命令

在Linux中,可以使用 ssh命令实现远程连接,如 ssh username@hostname

在Linux系统中,远程执行命令通常需要借助SSH(Secure Shell)协议,以下是一些常见的C语言实现的示例代码,用于在Linux下远程执行命令。

使用libssh库

libssh是一个开源的SSH客户端库,可以用来在C语言中实现远程命令执行。

安装libssh

首先需要安装libssh库:

sudo apt-get install libssh-dev

示例代码

以下是一个使用libssh库的简单示例,用于远程执行命令并获取输出:

#include <libssh/libssh.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main() {
    ssh_session my_ssh_session;
    int rc;
    char *command = "ls -l";
    char buffer[256];
    int nbytes;
    my_ssh_session = ssh_new();
    if (my_ssh_session == NULL)
        exit(-1);
    ssh_options_set(my_ssh_session, SSH_OPTIONS_HOST, "remote_host");
    ssh_options_set(my_ssh_session, SSH_OPTIONS_USER, "username");
    rc = ssh_connect(my_ssh_session);
    if (rc != SSH_OK) {
        fprintf(stderr, "Error connecting: %s
", ssh_get_error(my_ssh_session));
        ssh_free(my_ssh_session);
        exit(-1);
    }
    rc = ssh_userauth_password(my_ssh_session, NULL, "password");
    if (rc != SSH_AUTH_SUCCESS) {
        fprintf(stderr, "Authentication failed: %s
", ssh_get_error(my_ssh_session));
        ssh_disconnect(my_ssh_session);
        ssh_free(my_ssh_session);
        exit(-1);
    }
    rc = ssh_channel_open_session(my_ssh_session);
    if (rc != SSH_OK) {
        fprintf(stderr, "Could not open session: %s
", ssh_get_error(my_ssh_session));
        ssh_disconnect(my_ssh_session);
        ssh_free(my_ssh_session);
        exit(-1);
    }
    rc = ssh_channel_request_exec(my_ssh_session, command);
    if (rc != SSH_OK) {
        fprintf(stderr, "Could not execute command: %s
", ssh_get_error(my_ssh_session));
        ssh_channel_close(my_ssh_session, "remote_host");
        ssh_channel_free(my_ssh_session);
        ssh_disconnect(my_ssh_session);
        ssh_free(my_ssh_session);
        exit(-1);
    }
    nbytes = ssh_channel_read(my_ssh_session, buffer, sizeof(buffer), 0);
    while (nbytes > 0) {
        write(1, buffer, nbytes);
        nbytes = ssh_channel_read(my_ssh_session, buffer, sizeof(buffer), 0);
    }
    if (nbytes < 0) {
        fprintf(stderr, "Error reading from channel: %s
", ssh_get_error(my_ssh_session));
    }
    ssh_channel_send_eof(my_ssh_session);
    ssh_channel_close(my_ssh_session, "remote_host");
    ssh_channel_free(my_ssh_session);
    ssh_disconnect(my_ssh_session);
    ssh_free(my_ssh_session);
    return 0;
}

使用popen函数

另一种简单的方法是使用C标准库中的popen函数,通过SSH命令来执行远程命令,这种方法不需要额外的库,但安全性较低,不适合生产环境。

示例代码

以下是一个使用popen函数的简单示例:

#include <stdio.h>
#include <stdlib.h>
int main() {
    FILE *fp;
    char path[1035];
    /* Open the command for reading. */
    fp = popen("ssh user@remote_host 'ls -l'", "r");
    if (fp == NULL) {
        printf("Failed to run command
" );
        exit(1);
    }
    /* Read the output a line at a time output it. */
    while (fgets(path, sizeof(path)-1, fp) != NULL) {
        printf("%s", path);
    }
    /* close */
    pclose(fp);
    return 0;
}

相关问答FAQs

Q1: 使用libssh库时,如何设置超时时间?

A1: 可以通过ssh_options_set函数设置超时时间,设置连接超时时间为10秒:

ssh_options_set(my_ssh_session, SSH_OPTIONS_TIMEOUT, &timeout);

其中timeout为一个整数,表示超时时间(以毫秒为单位)。

Q2: 使用popen函数执行远程命令时,如何避免密码明文出现在代码中?

A2: 可以使用SSH密钥认证来避免在代码中出现密码,首先需要在远程主机上配置无密码登录,然后在代码中使用类似以下的命令:

fp = popen("ssh -i /path/to/private/key user@remote_host 'ls -l'", "r");

其中/path/to/private/key是私钥文件的路径。

小编有话说

在使用C语言编写远程命令执行程序时,选择合适的库和方法非常重要,libssh库功能强大且安全,适合大多数场景;而popen方法则更加简单直接,但在安全性方面有所欠缺,无论选择哪种方法,都需要注意保护好用户的凭证信息,避免安全破绽。