Execute shell commands in the program to automatically obtain all the online IP addresses and open ports in the LAN (Linux), shelllinux
Preface
The question is a bit long. It is actually the following: first, let the Shell command be executed in the program, and then pass the execution result to the variable, so that subsequent transactions can be processed, the second is to use the nmap command to obtain all the online IP addresses and open ports in the current LAN, and finally process the nmap results to obtain only the IP addresses and ports.
The powerful functions of Linux Command lines are believed to be known to all users of Linux. A command can be implemented only when you use hundreds or even thousands of lines of code in windows. Therefore, if you use shell commands, it can be said that it is twice the result with half the effort.
To make full use of shell results, it is necessary to obtain the result returned by the command that can be processed.
Execute Shell commands in the program
It mainly uses 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 pipeline, calls fork () to create a child process, and executes a shell to run a command to start a process. This pipeline must be closed by the pclose () function, not the fclose () function. The pclose () function closes the standard I / O stream, waits for the command execution to finish, and then returns to the shell's termination state. If the shell cannot be executed, the termination status returned by pclose () is the same as the shell executed exit.
The type parameter can only be one of read or write, and the resulting return value (standard I / O stream) also has a read-only or write-only type corresponding to the type. If the type is "r", the file pointer is connected to the standard output of the command; if the type is "w", the file pointer is connected to the standard input of the command.
The command parameter is a pointer to a shell command string ending in NULL. This line of commands will be passed to bin / sh with the -c flag, and the shell will execute the command.
The return value of popen () is a standard I / O stream and must be terminated by pclose. As mentioned earlier, this stream is unidirectional (can only be used for reading or writing). Writing to this stream is equivalent to writing the standard input of the command. The standard output of the command is the same as the process of calling popen (); on the contrary, reading data from the stream is equivalent to reading the standard output of the command The input and the process of calling popen () are the same.
return value:
If the call to fork () or pipe () fails, or the memory cannot be allocated, it will return NULL, otherwise it returns the standard I / O stream. popen () did not set the errno value for memory allocation failure. If an error occurs when calling fork () or pipe (), errno is set to the corresponding error type. If the type parameter is invalid, errno will return EINVAL.
Instructions:
// execute shell command
// Execute a shell command, the output result is stored line by line in resvec, and return the number of lines
int32_t myexec (const char * cmd, vector <string> & resvec) {
resvec.clear ();
FILE * pp = popen (cmd, "r"); // Create a pipeline
if (! pp) {
return -1;
}
char tmp [1024]; // Set an appropriate 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 line breaks
}
resvec.push_back (tmp);
}
pclose (pp); // Close the pipeline
return resvec.size ();
}
Use nmap to get all online IPs and open ports
Powerful nmap, there are too many specific introductions online, attach the official website http://nmap.org/, use the following command
nmap -sT 192.168.1.0/24
Scan the class C network segment to get the result graph and format. Viewing the format can facilitate the subsequent processing of the results:
If it is a class B network segment, change to:
nmap -sT 192.168. *. * / 24
Process the results
The goal of processing is to directly obtain the IP address and corresponding port number, save it in a vector array, and then facilitate subsequent processing. The processing code is 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 processing speed is relatively slow. Follow up to see if it can improve the speed. Exchanges are welcome. Although these network identification technologies are very useful for system developers, they also give malicious hackers many opportunities, so there are advantages and disadvantages.
Please indicate the source for reprint: http://blog.csdn.net/luoyun614/article/details/42967695