Getting all output from terminal in C

前端 未结 2 608
礼貌的吻别
礼貌的吻别 2021-01-25 15:58

I am currently working on a ssh program and I want to be able to have full control over the terminal via networking. My question is, if I send a command to the server to run in

2条回答
  •  猫巷女王i
    2021-01-25 16:11

    You want the "popen" function. Here's an example of running the command ls /etc and outputting to the console.

    #include 
    #include 
    
    
    int main( int argc, char *argv[] )
    {
    
      FILE *fp;
      int status;
      char path[1035];
    
      /* Open the command for reading. */
      fp = popen("/bin/ls /etc/", "r");
      if (fp == NULL) {
        printf("Failed to run command\n" );
        exit;
      }
    
      /* Read the output a line at a time - output it. */
      while (fgets(path, sizeof(path), fp) != NULL) {
        printf("%s", path);
      }
    
      /* close */
      pclose(fp);
    
      return 0;
    }
    
    
    

提交回复
热议问题