Objective
The topic said a bit long, in fact, is the following several things, the first is that the shell command can be executed within the program, and then pass the execution results to the variable, so that the subsequent transaction processing, the second is to use the Nmap command to get all the online IP and open ports in the current LAN, Finally, the results of nmap are processed to obtain only the IP and port results.
The power of the Linux command line it is believed that the people who use Linux know that a command equals you in Windows with hundreds of lines or even thousands of lines of code to achieve, so if the good use of shell commands, can be said to be more than a multiplier.
To make full use of the results of the shell, it is necessary to get the results of the command that can be processed.
Executing shell commands inside the program
The main function is to use the following functions:
function Definition:
#include <stdio.h>
FILE * popen(const char *command , const char *type );
int pclose(FILE *stream);
function Description:
The Popen () function creates a child process by creating a pipe, calling fork (), and executes a shell to run the command to open a process. This pipe must be closed by the Pclose () function instead of the fclose () function. The Pclose () function closes the standard I/O stream, waits for the command execution to end, and then returns the Shell's terminating state. If the shell cannot be executed, the terminating state returned by Pclose () is the same as the shell has executed exit.
The type parameter can only be read or write, and the resulting return value (standard I/O stream) also has a read-only or write-only type corresponding to type. If type is "R" then the file pointer is connected to the standard output of command, and if type is "W" then the file pointer is connected to the standard input of command.
The command parameter is a pointer to a null-terminated shell command string. This line of command will be passed to bin/sh and use the-c flag and the shell will execute the command.
The return value of Popen () is a standard I/O stream that must be terminated by Pclose. As mentioned earlier, this flow is unidirectional (can only be used for reading or writing). Writing to this stream is equivalent to writing the standard input to the command, the standard output of the command is the same as the process of calling Popen (), and, conversely, reading the data from the stream is equivalent to reading the standard output of the command, the standard input of the command and the process of calling Popen ().
return Value:
If the call to fork () or pipe () fails, or if memory cannot be allocated, it returns NULL, otherwise the standard I/O stream is returned. Popen () did not set errno value for memory allocation failure. If an error occurs when the fork () or pipe () is called, errno is set to the appropriate error type. If the type parameter is not valid, errno will return einval.
How to use:
// execute shell command
// Execute a shell command, the output is stored line by line in resvec, and the number of lines is returned
int32_t myexec (const char * cmd, vector <string> & resvec) {
resvec.clear ();
FILE * pp = popen (cmd, "r"); // establish a pipeline
if (! pp) {
return -1;
}
char tmp [1024]; // Set a suitable length to store each line of output
while (fgets (tmp, sizeof (tmp), pp)! = NULL) {
if (tmp [strlen (tmp)-1] == '\ n') {
tmp [strlen (tmp)-1] = '\ 0'; // Remove newlines
}
resvec.push_back (tmp);
}
pclose (pp); // Close the pipe
return resvec.size ();
}
Use Nmap to get all online IP and open ports
Strong Nmap, the specific introduction of the Internet too much, attach the official website http://nmap.org/, use the following command
Nmap-st 192.168.1.0/24
To scan the class C segment, get the result map, format, view format can be convenient for subsequent processing of the results:
If it is a Class B network segment, then change to:
Nmap-st 192.168.*.*/24
Process the results
The main goal of the processing is to directly obtain the IP address and the corresponding port number, save to the vector array, and then facilitate the subsequent processing, processing code as follows:
int32_t get_ip_port( vector<string> input,map<string,vector<string> > &result)
{
result.clear();
vector<string>::iterator it=input.begin();
int flag=0;
while(it!=input.end())
{
if((*it)[0]!='\0')
{
flag++;
if(flag==1)
{
//get IP address and push_back a pair to reslut
vector<string> tmp;
const char* IP=new char(24);
IP=it->data()+21;
result.insert(pair<string,vector<string> >(IP,tmp));
}
else if(flag==2||3)
{
//skip
}
else
{
//get port information
map<string,vector<string> >::iterator itt=result.end();
char Port[5];
const char* p=new char(5);
p=it->data();
for(int i=0;i<5;i++)
{
if((*p)!='/')
{
Port[i]=*p;
p++;
}
else
break;
}
itt->second.push_back(Port);
}
++it;
}
else
{
flag=0;
++it;
}
}
return 1;
}
At present, the speed of processing is relatively slow, follow-up to see if the speed can be improved, welcome to communicate. Although these network identification technology is very useful to the developers of the system, but also gives the malicious hackers a lot of opportunities, so there are pros and cons.
Reprint Please specify the Source: http://blog.csdn.net/luoyun614/article/details/42967695
In-Program execution shell command automatically obtains all the online IP and Open ports (Linux) within the LAN