//============================================================================ // Name : ExecuterPipe.cpp // Author : SaEeD // Description : Command Executer class to execute command and get its output pipe //============================================================================ #include <iostream> #include <string> #include <cstdio> using namespace std; //Executer Class class Executer { public: Executer(const string &str): command(str){}; string getOutput(); void getCommand(); private: string Execute(const string &command); string command; }; void Executer::getCommand() { cout << "[+]Input command is: " << command << endl; } string Executer::Execute(const string &str) { // man page for popen in Linux FILE* pipe = popen(str.c_str(), "r"); if (!pipe) return "ERROR"; char buffer[128]; string result = ""; while(!feof(pipe)) { if(fgets(buffer, 128, pipe) != NULL) result += buffer; } pclose(pipe); return result; } string Executer::getOutput() { return Execute(command); } // End of Executer Class int main(int argc, char *argv[]) { if(argc !=2) { cerr << "Usage: " << argv[0] << " <System Command>" << endl; return 1; } Executer *run = new Executer(argv[1]); run->getCommand(); cout << "[===== Command Output =====]" << endl; cout << run->getOutput() << endl; cout << "[===== End of Command Output =====]" << endl; return 0; }