Server Hosting Secrets
January 21st in Programming, Tutorials by Claudiu Popescu .

Execute shell/bash command using C/C++

In this article I’ll explain how to execute a shell command using a c/c++ program.
There’s an advanced example included, reading line by line from a file and executing a command for each line.

There are a few options that you can use: execl, execlp, execle, execv, execvp, but in this example I’ll use system.
Let’s begin with a simple example:


#include <cstdlib>
#include <cstdio>
using namespace std;

int main()
{
    // declaration of record
    char record[1000];
    // prints the shell command into record
    sprintf(record,"/bin/ls");
    // this executes what record contains
    system(record);
    return 0;
}

A short description of include headers:
cstdlib - includes functions involving memory allocation, process control, conversions, and so on
cstdio - contains macro definitions, constants, and declarations of functions and types
using namespace std – is a collection of classes and functions, which are written in the core language and part of the C++ ISO Standard itself

How to compile this under linux, first of all you need GCC installed, compile it like this if you do have it installed:


g++ -o filename filename.cpp

Now let’s see a more complex example:

#include <cstdlib>
#include <cstdio>
// required in order to open a file
#include <fstream>
using namespace std;

int main()
{
  // declaration of line as a string
  string line;
  // opens iplist for reading
  ifstream myfile ("/home/iplist");
  char record[1000];

  // if the file was successfully opened "continue"
  if (myfile.is_open())
  {
    // reading from file until reaching end of file EOF
    while (! myfile.eof() )
    {
      // get line by line from file and store it in the variable line
      getline (myfile,line);
      // stores the to be executed command into "record", %s is replaced by the content of "line"
      sprintf(record,"/sbin/iptables -A INPUT -s %s -j ACCEPT", line.c_str());
      // executes the actual command
      system(record);
    }
    // after we are done with the file we need to close it
    myfile.close();
  }
// in case the file can't be opened, the program prints this message on a new line
else printf("Unable to open file\n");
return 0;
}

What is this program doing?
It starts by opening the file “/home/iplist” for reading.
Next, if the file is opened it will start executing the while loop, else it will print “Unable to open file” on a new line.
The while loop will execute the shell command for every line in the file.
Let’s say that you have the file /home/iplist containing this lines:


192.168.0.2
10.10.2.4

Your program will execute the following commands:


iptables -A INPUT -s 192.168.0.2 -j ACCEPT
iptables -A INPUT -s 10.10.2.4 -j ACCEPT
VN:F [1.9.3_1094]
Rating: 10.0/10 (1 vote cast)
Execute shell/bash command using C/C++, 10.0 out of 10 based on 1 rating
The Short URL of this blog posting is http://tyn.li/A
Twitter this blog posting

One Comment

Leave A Comment.





Trackbacks