Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.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
选择执行bash命令的编程语言 对于我的学校项目,我必须制作一个C++程序。该程序必须使用Linux操作系统上已安装的应用程序。为了熟悉从C++应用程序中管理其他进程,我想制作一个程序,设置一个WLAN接口来监视模式。到目前为止,我编写的代码相当长,似乎效_C++_Linux_Bash_Output - Fatal编程技术网

选择执行bash命令的编程语言 对于我的学校项目,我必须制作一个C++程序。该程序必须使用Linux操作系统上已安装的应用程序。为了熟悉从C++应用程序中管理其他进程,我想制作一个程序,设置一个WLAN接口来监视模式。到目前为止,我编写的代码相当长,似乎效

选择执行bash命令的编程语言 对于我的学校项目,我必须制作一个C++程序。该程序必须使用Linux操作系统上已安装的应用程序。为了熟悉从C++应用程序中管理其他进程,我想制作一个程序,设置一个WLAN接口来监视模式。到目前为止,我编写的代码相当长,似乎效,c++,linux,bash,output,C++,Linux,Bash,Output,选择执行bash命令的编程语言 对于我的学校项目,我必须制作一个C++程序。该程序必须使用Linux操作系统上已安装的应用程序。为了熟悉从C++应用程序中管理其他进程,我想制作一个程序,设置一个WLAN接口来监视模式。到目前为止,我编写的代码相当长,似乎效率不高。有没有办法使我的代码更紧凑、更高效?在程序的最后,我想执行iwconfig来检查wlan是否真的处于监视模式。最好的方法是什么 #include <unistd.h> #include <iostream> #i

选择执行bash命令的编程语言 对于我的学校项目,我必须制作一个C++程序。该程序必须使用Linux操作系统上已安装的应用程序。为了熟悉从C++应用程序中管理其他进程,我想制作一个程序,设置一个WLAN接口来监视模式。到目前为止,我编写的代码相当长,似乎效率不高。有没有办法使我的代码更紧凑、更高效?在程序的最后,我想执行iwconfig来检查wlan是否真的处于监视模式。最好的方法是什么

#include <unistd.h>
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/wait.h>
using namespace std;

int main(int argc, char *argv[])
{
    string wlan = "wlan1";
    pid_t ifconfigDown;
    ifconfigDown = fork();
    if(ifconfigDown == 0)//ifconfig
    {
        execl("/sbin/ifconfig", "ifconfig", wlan.c_str(), "down",(char*)0 );
    }
    else //parent
    {
        usleep(500000);
        pid_t iwconfigMode;
        iwconfigMode = fork();
        if(iwconfigMode == 0)//ifconfig
        {
            execl("/sbin/iwconfig","iwconfig",wlan.c_str(),"mode","monitor",(char*)0 );
        }
        else//parent
        {
            usleep(500000);
            pid_t ifconfigUp;
            ifconfigUp = fork();
            if(ifconfigUp == 0)//ifconfig
            {
                execl("/sbin/ifconfig", "ifconfig", wlan.c_str(), "up", (char*)0 );
            }
            else//parent
            {
                usleep(500000);
                pid_t iwconfig;
                iwconfig = fork();
                if(iwconfig == 0)//iwconfig
                {
                    execl("/sbin/iwconfig", "iwconfig", (char*)0 );
                    //check if wlan1 is in monitor mode
                }
            }
        }
        waitpid(-1, NULL, 0);
        return 0;
    }
}


    string rPopenEnd (string cmd)
    {
         FILE *fp = popen(cmd.c_str(), "r");
        if (fp == NULL)
        {
            return "ERROR";
        }
        else
        {
            uint16_t line_size = 20;
            char line[line_size];
            string result;
            while (fgets(line, line_size, fp))
                 result += line;

            wait(NULL);
            return result;
        }
}

int main(int argc, char *argv[])
{
    rPopenEnd("iwconfig");
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
字符串wlan=“wlan1”;
pid\u t ifconfiguredown;
ifconfigDown=fork();
if(ifconfigDown==0)//ifconfig
{
execl(“/sbin/ifconfig”,“ifconfig”,wlan.c_str(),“down”,“char*)0);
}
else//parent
{
美国LEEP(500000);
pid_t iwconfig模式;
iwconfigMode=fork();
if(iwconfigMode==0)//ifconfig
{
execl(“/sbin/iwconfig”、“iwconfig”、wlan.c_str()、“mode”、“monitor”、(char*)0);
}
else//parent
{
美国LEEP(500000);
pid\u t ifconfigUp;
ifconfigUp=fork();
if(ifconfigUp==0)//ifconfig
{
execl(“/sbin/ifconfig”,“ifconfig”,wlan.c_str(),“up”,“char*)0);
}
else//parent
{
美国LEEP(500000);
pid_t iwconfig;
iwconfig=fork();
如果(iwconfig==0)//iwconfig
{
execl(“/sbin/iwconfig”,“iwconfig”,“char*)0);
//检查wlan1是否处于监视模式
}
}
}
waitpid(-1,NULL,0);
返回0;
}
}
字符串RPOPENND(字符串cmd)
{
文件*fp=popen(cmd.c_str(),“r”);
如果(fp==NULL)
{
返回“错误”;
}
其他的
{
uint16线尺寸=20;
字符行[行大小];
字符串结果;
while(fgets(线条、线条尺寸、fp))
结果+=行;
等待(空);
返回结果;
}
}
int main(int argc,char*argv[])
{
rpopennd(“iwconfig”);
}

不要长时间睡眠,只需调用等待子进程完成即可。对于最后一个命令,您可以使用,这样您就可以读取命令的输出。除了用于从
C/C++
启动程序之外,我认为在这种情况下使用
C/C++
不是一个好主意。@JoachimPileborg Wait确实是一个更好的选择。我现在已经实现了这一点。popen的问题是直接向控制台输出数据。我不想那样。这能修好吗?@skyking很有可能。我一直有很多麻烦。什么样的程序语言适合我的要求。在进入wlan接口的监视器模式后,我想用流式输出执行命令,如top。这将使读写程序变得更加复杂。如果您使用
popen
“r”
模式,它应该将子进程的所有标准输出通过管道传输到父进程。而不是休眠一段时间,只需调用以等待子进程完成即可。对于最后一个命令,您可以使用,这样您就可以读取命令的输出。除了用于从
C/C++
启动程序之外,我认为在这种情况下使用
C/C++
不是一个好主意。@JoachimPileborg Wait确实是一个更好的选择。我现在已经实现了这一点。popen的问题是直接向控制台输出数据。我不想那样。这能修好吗?@skyking很有可能。我一直有很多麻烦。什么样的程序语言适合我的要求。在进入wlan接口的监视器模式后,我想用流式输出执行命令,如top。这将使读写程序变得更加复杂。如果您使用
popen
“r”
模式,它应该将子进程的所有标准输出通过管道传输到父进程。