Linux C 呼叫外部命令,並讀取相關執行結果

Linux C 呼叫外部命令,並讀取相關執行結果

Linux C/C++ 呼叫外部命令,並讀取相關執行結果


GITHUB: https://github.com/jash-git/Jash_LinuxC/tree/master/Linux%20C%E5%91%BC%E5%8F%AB%E5%A4%96%E9%83%A8%E5%91%BD%E4%BB%A4_%E4%B8%A6%E8%AE%80%E5%8F%96%E7%9B%B8%E9%97%9C%E5%9F%B7%E8%A1%8C%E7%B5%90%E6%9E%9C


https://github.com/jash-git/Jash_LinuxC/tree/master/Linux_C%20popen(%E5%8F%96%E5%BE%97PID%E5%92%8C%E5%8F%96%E5%BE%97%E5%91%BC%E5%8F%AB%E5%A4%96%E9%83%A8%E7%A8%8B%E5%BC%8F%E7%9A%84%E7%B5%90%E6%9E%9C)


code:

/*
透過 Linux C 呼叫外部命令,並讀取相關執行結果
*/
#include <stdio.h>
int main(int argc, char *argv[])
{
	FILE *pp;
	char cbuffer[1280];
	pp = popen("ifconfig ","r");
	
	cbuffer[0]='\0';
	while (fgets(cbuffer,sizeof(cbuffer),pp) != NULL)
	{
		printf("%s",cbuffer);
		cbuffer[0]='\0';
	}
	pclose(pp);

	printf("\n");
	return 0;
}
/*
	http://webcache.googleusercontent.com/search?q=cache:6_N6So_JZgMJ:blog.163.com/ky_147258/blog/static/14288004920118685640778/+&cd=9&hl=zh-TW&ct=clnk&lr=lang_zh-CN%7Clang_zh-TW
	linux 獲取進程ID以及popen用法等
	掃描ps命令的輸出: 
*/
/*
pidof :查找運行程式的進程ID/通過進程名查看進程ID

語 法
pidof[必要參數][選擇參數][程式]
功 能
pidof 命令:可以顯示正在運行的進程ID
參數
必要參數
-s 單次選中
-c 只返回進程ID
-x 同時返回腳本

選擇參數
-o<進程號>
範例2: 每次只返回一個進程號 

root@snail-hnlinux:~# pidof sshd -s
31327
*/
#include <stdio.h>  
#include <string.h>  
#include <sys/types.h>  
   
#define PS_COMMAND "ps -ax"  
#define SCAN(buf) (sscanf(buf,"%d %s %s %s %s",pid,s[0],s[1],s[2],cmd) == 5)  
   
/* Returns number of pid matching process name, or -1 on error */  
int lookup_pid(char *name, pid_t *pids, int n)  
{  
  FILE *fp;  
  char buf[256], cmd[200], s[3][32];  
  pid_t *pid;  
   
  if ((fp = popen(PS_COMMAND, "r")) == NULL)  
    return -1;  
   
  pid = pids;  
  while (n > 0 && fgets(buf, sizeof(buf), fp))  
    if (SCAN(buf) && strstr(cmd, name)) /* improve on this simple name check */  
      pid++, n--;  
   
  fclose(fp);  
  return pid - pids;  
}  
   
int main(int argc, char **argv)  
{  
  char *process;  
  pid_t pids[8];  
  int n, i;  
   
  if (argc != 2) {  
    printf("Usage: %s process_name  ", argv[0]);  
    return 1;  
  }  
  process = argv[1];  
   
  if ((n = lookup_pid(process, pids, 8)) == -1) {  
    printf("Error reading pid of %s  ", process);  
    return 2;  
  }  
   
  printf("%d %s  ", n, process);  
  for (i = 0; i < n; i++)  
    printf("%d  ", pids[ i]);  
  return 0;  
}
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define READ 0
#define WRITE 1

pid_t
popen2(const char *command, int *infp, int *outfp)
{
    int p_stdin[2], p_stdout[2];
    pid_t pid;

    if (pipe(p_stdin) != 0 || pipe(p_stdout) != 0)
        return -1;

    pid = fork();

    if (pid < 0)
        return pid;
    else if (pid == 0)
    {
        close(p_stdin[WRITE]);
        dup2(p_stdin[READ], READ);
        close(p_stdout[READ]);
        dup2(p_stdout[WRITE], WRITE);

        execl("/bin/sh", "sh", "-c", command, NULL);
        perror("execl");
        exit(1);
    }

    if (infp == NULL)
        close(p_stdin[WRITE]);
    else
        *infp = p_stdin[WRITE];

    if (outfp == NULL)
        close(p_stdout[READ]);
    else
        *outfp = p_stdout[READ];

    return pid;
}

int main(int argc, char **argv)
{
    int infp, outfp;
    char buf[128];

    if (popen2("sort", &infp, &outfp) <= 0)
    {
        printf("Unable to exec sort\n");
        exit(1);
    }

    write(infp, "Z\n", 2);
    write(infp, "D\n", 2);
    write(infp, "A\n", 2);
    write(infp, "C\n", 2);
    close(infp);

    *buf = '\0';
    read(outfp, buf, 128);

    printf("buf = '%s'\n", buf);

    return 0;
}

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *