当前位置:网站首页>c语言执行外部程序的几种方法
c语言执行外部程序的几种方法
2022-08-06 00:36:00 【qq_33808440】
一 vfork+exec
1.1 代码
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argv, char * args [])
{
printf("父进程pid:%d\n",getpid());
char * ips[2] = {
"192.168.1.1","192.168.1.251"};
int n = 0;
for(n=0;n<2;n++)
{
char ip[16] = {
0};
memcpy(ip,ips[n],strlen(ips[n]));
pid_t pid = vfork();
if(pid == 0)//子进程
{
printf("子进程pid:%d\n",getpid());
int res = execl("/bin/nmap","nmap","-sV","-p","1-1000",ip,NULL);
if(res == -1)
perror("execl");
else
printf("子进程pid:%d执行完execl\n",getpid());
}
else if(pid>0) //父进程
{
int status = 0;
pid_t pid = wait(&status);
printf("子进程%d退出了!",pid);
printf("a=%d\n",WIFEXITED(status));
printf("b=%d\n",WEXITSTATUS(status));
}
}
return 0;
}
二 system
2.1 代码
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(int argv, char * args [])
{
printf("父进程pid:%d\n",getpid());
char * ips[2] = {
"192.168.1.1","192.168.1.251"};
int n = 0;
for(n=0;n<2;n++)
{
char ip[16] = {
0};
memcpy(ip,ips[n],strlen(ips[n]));
char cmd[1024] = "0";
sprintf(cmd,"%s %s","nmap -sV -p 1-1000",ip);
printf("%s\n",cmd);
int res = system(cmd);
printf("res=%d\n",res);
}
return 0;
}
三 popen
头文件:stdio.h
函数:FILE *popen(const char *command, const char *type)
函数功能:popen()会调用fork()产生子进程,然后从子进程中调用/bin/sh -c来执行参数command的指令。参数type可使用“r”代表读取,“w”代表写入。如果type是"r"则文件指针连接到command的标准输出,读取命令的标准输出;如果type是"w"则文件指针连接到command的标准输入,写入命令的标准输入。依照此type值,popen()会建立管道连到子进程的标准输出设备或标准输入设备,然后返回一个文件指针。随后进程便可利用此文件指针来读取子进程的标准输出设备或是写入到子进程的标准输入设备中
返回值:若成功则返回标准I/O流文件指针(FILE *),否则返回NULL,错误原因存于errno中
函数:int pclose(FILE * stream);
函数功能:函数功能:pclose()用来关闭由popen所建立的管道及文件指针。参数stream为先前由popen()所返回的文件指针
返回值:若成功返回shell的终止状态(即子进程的终止状态),若出错返回-1,错误原因存于errno中
3.1 代码
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
int main(int argv, char * args [])
{
printf("父进程pid:%d\n",getpid());
char * ips[2] = {
"192.168.1.1","192.168.1.251"};
int n = 0;
FILE *fp = NULL;
for(n=0;n<2;n++)
{
char ip[16] = {
0};
memcpy(ip,ips[n],strlen(ips[n]));
char cmd[1024] = "0";
sprintf(cmd,"%s %s","nmap -sV -p 1-1000",ip);
printf("%s\n",cmd);
fp = popen(cmd, "r");
if(NULL == fp)
{
perror("popen");
return -1;
}
printf("执行nmap成功\n");
int rv=-1;
char result_buf[1024];
if( (rv=fread(result_buf,1,sizeof(result_buf),fp))<0 )
{
printf("Read from fp failure:%s\n",strerror(errno));
return -2;
}
printf("result_buf=%s\n",result_buf);
int rc = 0;
rc = pclose(fp); //关闭文件指针
if(-1 == rc)
{
printf("close file point failure.\n");
return -3;
}
}
return 0;
}
边栏推荐
- AS build菜单显示不全解决方法
- eBay, Amazon, Lazada, Shopee, AliExpress, Meikeduo and other cross-border e-commerce platforms, what conditions do I need to meet to evaluate the self-supporting account?How to optimize listing?
- Day3: Multiple-choice questions required for the interview
- 1467. 两个盒子中球的颜色数相同的概率 数学+DFS
- 【u-boot】如何自定义u-boot的命令
- Horizontal Federated Learning - Gradient Safe Aggregation (2)
- Day6: Multiple-choice questions required for the interview
- redis面试题
- Facial Expression Recognition---Study Notes
- Day4:面试必考选择题
猜你喜欢

【u-boot】如何自定义u-boot的命令

网页使用微信扫码登录

【kali-漏洞利用】(3.2)Metasploit基础(下):MSF终端利用过程

How to view code comments in Idea Modified by?

物理 NFT 有什么特别之处?

Vernacular Machine Learning - Convolutional Neural Network CNN

解决mysql语句MAX()函数中出现的问题

正则表达式完整入门教程,含在线练习

(3.2) Metasploit kali - the exploit 】 【 basis (under) : MSF terminal using process

wsa无法安装如何解决
随机推荐
Viola-Jones检测器(VJ)---学习笔记
横向联邦学习-梯度安全聚合(二)
从零开始用C#做产品:私人日记(14)Sqlite封装
如何对齐微信小程序胶囊按钮?
为啥强烈禁止使用Calendar?
现阶段VR全景市场如何?如何开拓市场新客户?
【LeetCode】209. 长度最小的子数组
Idea如何查看代码注释 修改者?
物理 NFT 有什么特别之处?
树状数组解题报告
k线图怎么看止损位置
Day6:面试必考选择题
程序员的浪漫:七夕准备好表白了吗
window.postMessage() Cross-domain communication between parent and child pages
ERP仓库管理系统查询(十)
手把手教你CSP系列之object-src
【数学模型】层次分析
1467. 两个盒子中球的颜色数相同的概率 数学+DFS
将children数组转为一位数组
Code Casual Recording Notes_Dynamic Programming_494 Objectives and