Linux C++ 执行 bash命令

171次阅读
没有评论

共计 2116 个字符,预计需要花费 6 分钟才能阅读完成。

有利用c++写简单bash命令,比如结合http或者其他的,这里分析一个示例,用到了libhv 这个高性能库,主要是用到executeCMD这个方法来执行bash命令

/*
 * sample http server
 * more detail see examples/httpd
 *
 */

#include "HttpServer.h"
#include "hssl.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <cstring>
using namespace std;
/*
 * #define TEST_HTTPS 1
 *
 * @build   ./configure --with-openssl && make clean && make
 *
 * @server  bin/http_server_test 8080
 *
 * @client  curl -v http://127.0.0.1:8080/ping
 *          curl -v https://127.0.0.1:8443/ping --insecure
 *          bin/curl -v http://127.0.0.1:8080/ping
 *          bin/curl -v https://127.0.0.1:8443/ping
 *
 */
#define TEST_HTTPS 0
int executeCMD(const char *cmd,char *result)
{
    char buf_ps[512];
    char ps[512]={0};
    int len=0;
    FILE *ptr;
    strcpy(ps, cmd);
    if((ptr=popen(ps, "r"))!=NULL)
    {
        while(fgets(buf_ps, 512, ptr)!=NULL)
        {
            strcat(result, buf_ps);
            if(strlen(result)>512)
                break;
        }
        pclose(ptr);
        ptr = NULL;
    }
    else
    {
        printf("popen %s error\n", ps);
    }
}
int main(int argc, char** argv) {
    HV_MEMCHECK;

    int port = 0;
    if (argc > 1) {
        port = atoi(argv[1]);
    }
    if (port == 0) port = 8080;

    HttpService router;
    router.GET("/add", [](HttpRequest* req, HttpResponse* resp) {
        char result[1024] = {0};
        string scmd;
        scmd = string ("sudo iptables-save | grep ") + string(req->client_addr.ip);
        executeCMD( scmd.c_str(), result);
        if(result[0] != '\0'){
            return resp->String("IP already exists");
        }else{
            scmd = string("sudo iptables -I INPUT -s ") + string(req->client_addr.ip) +  string(" -p tcp --dport 443 -j ACCEPT");
            executeCMD( scmd.c_str(), result);
            executeCMD( "sudo iptables-save ", result);
            return resp->String("Added successfully");
        }

        //system("sudo iptables -I INPUT -s 123.4.5.6 -p tcp --dport 443 -j ACCEPT");
        //resp->json["origin"] = req->client_addr.ip;
        return 200;
    });



    http_server_t server;
    server.service = &router;
    server.port = port;
#if TEST_HTTPS
    server.https_port = 8443;
    hssl_ctx_init_param_t param;
    memset(&param, 0, sizeof(param));
    param.crt_file = "cert/server.crt";
    param.key_file = "cert/server.key";
    param.endpoint = HSSL_SERVER;
    if (hssl_ctx_init(&param) == NULL) {
        fprintf(stderr, "hssl_ctx_init failed!\n");
        return -20;
    }
#endif

    // uncomment to test multi-processes
    // server.worker_processes = 4;
    // uncomment to test multi-threads
    // server.worker_threads = 4;

#if 1
    http_server_run(&server);
#else
    // test http_server_stop
    http_server_run(&server, 0);
    hv_sleep(10);
    http_server_stop(&server);
#endif
    return 0;
}

 

正文完
 
admin
版权声明:本站原创文章,由 admin 2022-04-05发表,共计2116字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)
验证码