c++ Linux Befehl ausführen und Ausgabe abfangen

Gast #3753170
Lesenswert?
• ▲
▼
Hallo,
ist es möglich, auf einer Linux-Maschine, in einem in C++ geschriebenen 
Programm einen Terminal-Befehl auszuführen und die Ausgabe abzufangen 
und in einem char* oder string zu speichern?

Das ganze soll etwas so aussehen:
1
//Anfang
2
char* ausgabe = unbekanntefunktion("linuxbefehl");
3
//ausgabe verarbeiten.

Das Programm soll dann warten, bis der Befehl zuende ausgeführt wurde.

Ich würde mich über Hilfe sehr freuen!
Gast #3762031
Lesenswert?
• ▲
▼
ich hab mal mit smartctl so geredet:

(License is GPL-3 or later)
1
/** Try to get temperature from smartctl of a given hdd.
2
 *
3
 * \returns the temp or -1 on error. */
4
int GetTempFromSmartCtrl(struct hd_info *hd)
5
{
6
        int pipe_fd[2];
7
        int status;
8
        int temperature = -1;
9
        char buffer[200];
10
        pid_t cpid;
11

12
        if (-1 == pipe(pipe_fd))
13
                return -1;
14

15
        cpid = fork();
16
        if (cpid == -1) {
17
                close(pipe_fd[0]);
18
                close(pipe_fd[1]);
19
                return -1;
20
        }
21

22
        if (cpid == 0) {
23
                /** child process */
24
                // D(printf("up to start smartctl -A %s\n",hd->device));
25
                close(pipe_fd[0]);      // close read end... wer only write to the pipe
26
                if (-1 == dup2(pipe_fd[1], STDOUT_FILENO)) {
27
                        printf("Syscall dup2 failed with Error %s\n",
28
                               strerror(errno));
29
                        _exit(-errno);
30
                }
31

32
                /* execute programm to get parameter */
33
                execlp("smartctl", "smartctl", "-A", hd->device, NULL);
34

35
                printf("Error: execlp. Error %s\n", strerror(errno));
36
                _exit(1);
37
        } else {
38
                int count = 0;
39
                struct smart_infos sm;
40
                /** parent process */
41
                close(pipe_fd[1]);      // close write end... wer only read from the pipe
42
                /** read out data received by child */
43
                /* for starting, just print it out to stdout. */
44

45
                while ((count = readaline(buffer, pipe_fd[0], 199)) > 0) {
46
                        if (parse_smart_line(buffer, &sm)) {
47
                                if (sm.id == 194)
48
                                        temperature = sm.raw_value;
49
                                if (sm.id == 4)
50
                                        startstop = sm.raw_value;
51
                        }
52
                }
53

54
                wait(&status);  // wait for child, catch the zombie...
55

56
                close(pipe_fd[0]);      // close other pipe end.
57

58
                // D(printf("Temp of %s is %d \n",hd->device, temperature));
59
                return temperature;
60

61
        }
62
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren