Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C++; 我的任务是帮助从C++中的Linux机器收集网络数据。我是C++新手,需要一些建议。我需要使用这个shell脚本并尽可能地模拟它。基本上,我需要转储机器的所有网络细节——netstat或ss、ifconfig等_C++_Linux - Fatal编程技术网

在C++; 我的任务是帮助从C++中的Linux机器收集网络数据。我是C++新手,需要一些建议。我需要使用这个shell脚本并尽可能地模拟它。基本上,我需要转储机器的所有网络细节——netstat或ss、ifconfig等

在C++; 我的任务是帮助从C++中的Linux机器收集网络数据。我是C++新手,需要一些建议。我需要使用这个shell脚本并尽可能地模拟它。基本上,我需要转储机器的所有网络细节——netstat或ss、ifconfig等,c++,linux,C++,Linux,上一个shell脚本片段: get_networkdata(){ #All Active Network Connections { if netstat -pvWanoee &> /dev/null then # gnu netstat -pvWanoee elif netstat -pvTanoee &> /dev/null then

上一个shell脚本片段:

get_networkdata(){
    #All Active Network Connections
    {
        if netstat -pvWanoee &> /dev/null
        then
            # gnu
            netstat -pvWanoee
        elif netstat -pvTanoee &> /dev/null
    then
            # redhat/centos
            netstat -pvTanoee
    else
        netstat -anp
        fi
    } > "$SAVETO/netstat.txt"

    #Network Configuration
    {
        echo  -e "\n</etc/network/interfaces>";cat /etc/network/interfaces
        echo  -e "\n<ifconfig -a>";ifconfig -a
        echo  -e "\n</etc/resolv.conf>";cat /etc/resolv.conf
        echo  -e "\n<ip addr>"; ip addr
        echo  -e "\n<ip link>";ip link
        echo  -e "\n<netstat -anp>;"netstat -anp
        if lsof -i -n -P &> /dev/null
        then
            echo  -e "\n<lsof -i -n -P>";lsof -i -n -P
        fi
        echo  -e "\n<ss -ap>";ss -ap
        echo  -e "\n<route -n>";route -n # "netstat -nr"; "ip route"
        echo  -e "\n<ip neigh>";ip neigh
        echo  -e "\n</etc/hostname>";cat /etc/hostname
        echo  -e "\n<cat /etc/hosts>";cat /etc/hosts
        echo  -e "\n<cat /etc/hosts.allow>";cat /etc/hosts.allow
        echo  -e "\n<cat /etc/hosts.deny>";cat /etc/hosts.deny
    } > "$SAVETO/netinfo.txt"

    #SSH Folder Copy
    tar -zcvf "$SAVETO/ssh_folder.tar.gz" /etc/ssh/* 2> /dev/null
}
get_networkdata(){
#所有活动网络连接
{
如果netstat-pvWanoee&>/dev/null
然后
#gnu
netstat-pvWanoee
elif netstat-pvTanoee&>/dev/null
然后
#红帽/森托斯
netstat-pvTanoee
其他的
netstat-anp
fi
}>“$SAVETO/netstat.txt”
#网络配置
{
echo-e“\n”cat/etc/网络/接口
echo-e“\n”ifconfig-a
echo-e“\n”;cat/etc/resolv.conf
echo-e“\n”ip地址
echo-e“\n”ip链路
echo-e“\n;”netstat-anp
如果lsof-i-n-P&>/dev/null
然后
echo-e“\n”lsof-i-n-P
fi
回声-e“\n”ss-ap
echo-e“\n”路由-n#“netstat-nr”;“ip路由”
回音-e“\n”ip neigh
echo-e“\n”cat/etc/hostname
echo-e“\n”cat/etc/hosts
echo-e“\n”cat/etc/hosts.allow
echo-e“\n”cat/etc/hosts.deny
}>“$SAVETO/netinfo.txt”
#SSH文件夹副本
tar-zcvf“$SAVETO/ssh_folder.tar.gz”/etc/ssh/*2>/dev/null
}

这是我在C++中尝试过的。在创建文本文件时,它是有效的,但我觉得我在缩短或没有有效地完成它。任何建议都将不胜感激

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>

    using namespace std;

    string GetStdoutfromCommand(string cmd) {

        string data;
        FILE * stream;
        const int max_buffer = 256;
        char buffer[max_buffer];
        cmd.append (" 2>&1" );

        stream = popen(cmd.c_str(), "r");
        if (stream) {

            while (!feof(stream))
            if (fgets(buffer, max_buffer, stream) !=nullptr) data.append(buffer);
            pclose(stream);
        }

        return data;
    }

    int main () {

        string net_devices = GetStdoutfromCommand("ifconfig -a >network_devices.txt");
        string ss = GetStdoutfromCommand("ss -a -pl >netstats.txt");
        string ip_route = GetStdoutfromCommand("ip route >ip_route.txt");

        return 0;
    }
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
字符串getstdoutfrom命令(字符串cmd){
字符串数据;
文件*流;
const int max_buffer=256;
字符缓冲区[最大缓冲区];
cmd.append(“2>&1”);
stream=popen(cmd.c_str(),“r”);
如果(流){
而(!feof(stream))
if(fgets(buffer,max_buffer,stream)!=nullptr)data.append(buffer);
pclose(流);
}
返回数据;
}
int main(){
字符串net_devices=GetStdoutfromCommand(“ifconfig-a>network_devices.txt”);
字符串ss=GetStdoutfromCommand(“ss-a-pl>netstats.txt”);
字符串ip_route=GetStdoutfromCommand(“ip route>ip_route.txt”);
返回0;
}

< P>我建议使用三个重要事实:1)Linux是开放源代码,2)它的大部分调用及其系统调用都是用C编写的,而3)C可以编译为C++。 因此,如果您以上面的代码为例,如果您想更有效地执行ifconfig,请从您拥有的开始,然后用原始源代码的一部分替换ifconfig代码,可以在此处找到:


看看/proc文件系统。
但是要注意,它会随着Linux版本的不同而变化。

Beautiful。这正是我希望得到的反馈。干杯